/* Read one file, creating the buffer if necessary. */ static bool readone(char *bname, char *path) { int rc; zbuff_t *was = Curbuff; if (cfindbuff(bname)) return true; if (!cmakebuff(bname, path)) return false; putpaw("Reading %s", lastpart(path)); rc = zreadfile(path); if (rc < 0) { cdelbuff(Curbuff); zswitchto(was); putpaw("Read Error %s", lastpart(path)); return false; } toggle_mode(0); if (rc > 0) putpaw("New File"); #ifdef R_OK else if (access(path, R_OK | W_OK) == EOF) Curbuff->bmode |= VIEW; #endif set_last_bufname(was); return true; }
/** * print file name. * * @param[in] level 0,1,2... * @param[in] path path of the file */ static const char * print_file_name(int level, const char *path) { STATIC_STRBUF(sb); char *target = (Fflag) ? "mains" : "_top"; int size = filesize(path); char tips[80]; message(" [%d] adding %s", ++src_count, removedotslash(path)); /* * We assume the file which has one of the following suffixes * as a candidate of include file. * * C: .h * C++: .hxx, .hpp, .H, .hh * PHP: .inc.php */ if (regexec(&is_include_file, path, 0, 0, 0) == 0) put_inc(lastpart(path), path, src_count); strbuf_clear(sb); if (table_flist) strbuf_puts(sb, fitem_begin); else if (!no_order_list) strbuf_puts(sb, item_begin); if (size > 1) snprintf(tips, sizeof(tips), "%s bytes", insert_comma(size)); else snprintf(tips, sizeof(tips), "%s byte", insert_comma(size)); strbuf_puts(sb, gen_href_begin_with_title_target(level == 0 ? SRCS: upperdir(SRCS), path2fid(path), HTML, NULL, tips, target)); if (Iflag) { const char *lang, *suffix, *text_icon; if ((suffix = locatestring(path, ".", MATCH_LAST)) != NULL && (lang = decide_lang(suffix)) != NULL && (strcmp(lang, "c") == 0 || strcmp(lang, "cpp") == 0 || strcmp(lang, "yacc") == 0)) text_icon = c_icon; else text_icon = file_icon; strbuf_puts(sb, gen_image(level == 0 ? CURRENT : PARENT, text_icon, removedotslash(path))); strbuf_puts(sb, quote_space); } strbuf_puts(sb, full_path ? removedotslash(path) : lastpart(path)); strbuf_puts(sb, gen_href_end()); if (table_flist) strbuf_puts(sb, fitem_end); else if (!no_order_list) strbuf_puts(sb, item_end); else strbuf_puts(sb, br); strbuf_putc(sb, '\n'); return (const char *)strbuf_value(sb); }
/** * print directory name. * * @param[in] level 0,1,2... * @param[in] path path of the directory * @param[in] count number of files in this directory */ static const char * print_directory_name(int level, const char *path, int count) { STATIC_STRBUF(sb); char tips[80]; if (count > 1) snprintf(tips, sizeof(tips), "%d files", count); else snprintf(tips, sizeof(tips), "%d file", count); path = removedotslash(path); strbuf_clear(sb); if (table_flist) strbuf_puts(sb, fitem_begin); else if (!no_order_list) strbuf_puts(sb, item_begin); strbuf_puts(sb, gen_href_begin_with_title(level == 0 ? "files" : NULL, path2fid(path), HTML, NULL, tips)); if (Iflag) { strbuf_puts(sb, gen_image(level == 0 ? CURRENT : PARENT, dir_icon, appendslash(path))); strbuf_puts(sb, quote_space); } strbuf_sprintf(sb, "%s/%s", lastpart(path), gen_href_end()); if (table_flist) strbuf_puts(sb, fitem_end); else if (!no_order_list) strbuf_puts(sb, item_end); else strbuf_puts(sb, br); strbuf_putc(sb, '\n'); return (const char *)strbuf_value(sb); }
void Zread_file(void) { struct buff *tbuff; struct mark *tmark; int rc = 1; if (get_findfile("Read File: ")) return; if ((tbuff = bcreate())) { putpaw("Reading %s", lastpart(Fname)); rc = breadfile(tbuff, Fname, NULL); if (rc == 0) { btoend(tbuff); if ((tmark = bcremark(tbuff))) { btostart(tbuff); bcopyrgn(tmark, Bbuff); bdelmark(tmark); } else error("Out of memory"); } bdelbuff(tbuff); } if (rc) error("Unable to read %s", Fname); }
bool findfile(char *path) { char tbname[BUFNAMMAX + 1]; zbuff_t *tbuff; zbuff_t *was = Curbuff; Arg = 0; /* limit name to BUFNAMMAX */ strncpy(tbname, lastpart(path), BUFNAMMAX); tbname[BUFNAMMAX] = '\0'; /* If is this file already in a buffer - use it. * At startup, we are done. */ foreachbuff(tbuff) if (tbuff->fname && strcmp(path, tbuff->fname) == 0) { zswitchto(tbuff); if (Initializing) return true; set_last_bufname(was); break; } if (!tbuff) { if (cfindbuff(tbname)) { /* Resolve buffer name collisions by creating * a unique name */ char *p; int i; i = strlen(tbname); p = &tbname[i < BUFNAMMAX - 3 ? i : BUFNAMMAX - 3]; /* We cannot use 100 here due to a bug in gcc 7.3.0 */ for (i = 0; i < 99; ++i) { strfmt(p, 4, ".%d", i); if (!cfindbuff(tbname)) break; } if (cfindbuff(tbname)) return false; } if (!readone(tbname, path)) return false; } if (!Initializing) { cswitchto(Curbuff); reframe(); } else if (!*Fname) strcpy(Fname, path); return true; }
bool filesave(void) { char path[PATHMAX + 1]; Arg = 0; if (Curbuff->fname == NULL) { *path = '\0'; if (getfname("File Name: ", path) == 0) Curbuff->fname = strdup(path); else return false; Curwdo->modeflags = INVALID; } putpaw("Writing %s", lastpart(Curbuff->fname)); return zwritefile(Curbuff->fname); }
int main(int argc, char *argv[]) { extern char *optarg; extern int optind; int arg; whoami = lastpart(argv[0]); dirsonly = recursive = timev = yield = f_links = f_mounts = 0; singlecolumn = -1; while ((arg = getopt(argc, argv, "1Cdrt:ysm")) != -1) { switch (arg) { case '1': if ((singlecolumn != -1) && (! singlecolumn)) { fprintf(stderr, "%s: -1 and -C options are mutually exclusive\n", whoami); usage(); exit(1); } singlecolumn = 1; break; case 'C': if ((singlecolumn != -1) && singlecolumn) { fprintf(stderr, "%s: -1 and -C options are mutually exclusive\n", whoami); usage(); exit(1); } singlecolumn = 0; break; case 'd': dirsonly++; break; case 'r': recursive++; break; case 't': timev = atoi(optarg); break; case 'y': yield++; break; case 's': f_links++; break; case 'm': f_mounts++; break; default: usage(); exit(1); } } if (singlecolumn == -1) singlecolumn = ! isatty(1); if (optind == argc) { char *cwd; cwd = "."; if (ls(&cwd, 1)) error("ls of ."); } else if (ls(&argv[optind], argc - optind)) error ("ls"); exit (error_occurred ? 1 : 0); }
int main(int argc, char** argv){ char *tapename; int currarg,used_entries,total_entries,filesize,offset,diff1,diff2,entries,entry; unsigned char endadd_low, endadd_high; FILE* desc; struct program_block program; for (currarg=1;currarg<argc;currarg++){ if (!lastpart(argv[currarg],".t64")){ tapename=(char*)malloc(strlen(argv[currarg])+5); sprintf(tapename,"%s.t64",argv[currarg]); } else{ tapename=(char*)malloc(strlen(argv[currarg])+1); sprintf(tapename,"%s",argv[currarg]); } printf("%s: ",tapename); desc=fopen(tapename,"rb"); if (desc==NULL){ printf("Cannot open file\n",tapename); goto end2; } if (detect_type(desc)!=t64){ printf("The file is not a T64\n"); goto end; } if ((entries=get_used_entries(desc))!=1){ printf("Sorry, has %d entries, not 1\n",entries); goto end; } total_entries=get_total_entries(desc); for(entry=1;entry<=total_entries;entry++) if (get_entry_info(entry,desc,&program.info,&offset)) break; printf("1 entry, name %s, ",program.info.name); diff1=program.info.end-program.info.start; filesize=file_size(desc); diff2=filesize-offset; if (diff1==diff2){ printf("File OK\n"); goto end; } printf("broken, fixing..."); program.info.end=program.info.start+diff2; endadd_low=program.info.end&255; endadd_high=program.info.end>>8; fclose(desc); desc=fopen(tapename,"r+b"); if (desc==NULL){ printf("failed: %s\n", strerror(errno)); goto end2; } fseek(desc,36+32*entry,SEEK_SET); fwrite(&endadd_low ,1,1,desc); fwrite(&endadd_high,1,1,desc); printf("Fixed\n"); end: fclose(desc); end2: free(tapename); } return 0; }
static int print_directory(int level, char *basedir) { const char *path; FILEOP *fileop = NULL; FILE *op = NULL; int flist_items = 0; int count = 0; if (level > 0) { char name[MAXPATHLEN]; snprintf(name, sizeof(name), "%s/files/%s.%s", distpath, path2fid(basedir), HTML); fileop = open_output_file(name, 0); op = get_descripter(fileop); print_directory_header(op, level, basedir); if (tree_view) { char *target = (Fflag) ? "mains" : "_top"; strbuf_puts(files, dir_begin); strbuf_puts(files, gen_href_begin_with_title_target("files", path2fid(basedir), HTML, NULL, NULL, target)); strbuf_puts(files, full_path ? removedotslash(basedir) : lastpart(basedir)); strbuf_puts(files, gen_href_end()); strbuf_puts_nl(files, dir_title_end); strbuf_puts_nl(files, "<ul>"); } } while ((path = getpath()) != NULL) { const char *p, *local = localpath(path, basedir); /* * Path is outside of basedir. */ if (local == NULL) { ungetpath(); /* read again by upper level print_directory(). */ break; } /* * Path is inside of basedir. */ else { char *slash = strchr(local, '/'); if (table_flist && flist_items++ % flist_fields == 0) PUT(fline_begin); /* * Print directory. */ if (slash) { int baselen = strlen(basedir); char *q, *last = basedir + baselen; int subcount; if (baselen + 1 + (slash - local) > MAXPATHLEN) { fprintf(stderr, "Too long path name.\n"); exit(1); } /* * Append new directory to the basedir. */ p = local; q = last; *q++ = '/'; while (p < slash) *q++ = *p++; *q = '\0'; /* * print tree for this directory. */ ungetpath(); /* read again by lower level print_directory(). */ subcount = print_directory(level + 1, basedir); PUT(print_directory_name(level, basedir, subcount)); count += subcount; /* * Shrink the basedir. */ *last = '\0'; } /* * Print file. */ else { const char *file_name = print_file_name(level, path); if (tree_view) { int size = filesize(path); char *target = (Fflag) ? "mains" : "_top"; char tips[80]; if (size > 1) snprintf(tips, sizeof(tips), "%s bytes", insert_comma(size)); else snprintf(tips, sizeof(tips), "%s byte", insert_comma(size)); strbuf_sprintf(files, "%s%s%s%s%s\n", file_begin, gen_href_begin_with_title_target(SRCS, path2fid(path), HTML, NULL, tips, target), full_path ? removedotslash(path) : lastpart(path), gen_href_end(), file_end); } PUT(file_name); if (filemap_file) fprintf(FILEMAP, "%s\t%s/%s.%s\n", removedotslash(path), SRCS, path2fid(path), HTML); count++; } if (table_flist && flist_items % flist_fields == 0) PUT(fline_end); } } if (flist_items % flist_fields != 0) PUT(fline_end); if (level > 0) { print_directory_footer(op, level, basedir); close_file(fileop); if (tree_view) { strbuf_puts_nl(files, "</ul>"); strbuf_puts_nl(files, dir_end); } } html_count++; return count; }
static bool zwritefile(char *fname) { char bakname[PATHMAX + 1]; struct mark smark; struct stat sbuf; int nlink = 1, rc; bool bak = false; /* If the file existed, check to see if it has been modified. */ if (Curbuff->mtime && stat(fname, &sbuf) == 0) { if (sbuf.st_mtime > Curbuff->mtime) { strconcat(PawStr, PAWSTRLEN, "WARNING: ", lastpart(fname), " has been modified. Overwrite? ", NULL); if (ask(PawStr) != YES) return false; } #ifndef __QNXNTO__ nlink = sbuf.st_nlink; #endif } #if HUGE_FILES if (Bbuff->fd != -1) { strconcat(PawStr, PAWSTRLEN, "WARNING: huge file ", lastpart(fname), " not yet read. Are you sure? ", NULL); if (ask(PawStr) != YES) return false; } #endif /* check for links and handle backup file */ make_bakname(bakname, sizeof(bakname), fname); if (nlink > 1) { strconcat(PawStr, PAWSTRLEN, "WARNING: ", lastpart(fname), " is linked. Preserve? ", NULL); switch (ask(PawStr)) { case YES: if (VAR(VBACKUP)) bak = cp(fname, bakname); break; case NO: if (VAR(VBACKUP)) bak = rename(fname, bakname); else unlink(fname); /* break link */ break; case ABORT: return false; } } else if (VAR(VBACKUP)) bak = rename(fname, bakname); bmrktopnt(Bbuff, &smark); rc = bwritefile(Bbuff, fname, file_mode()); bpnttomrk(Bbuff, &smark); if (rc) { if (stat(fname, &sbuf) == 0) Curbuff->mtime = sbuf.st_mtime; else Curbuff->mtime = -1; clrpaw(); } else { if (errno == EACCES) error("File is read only."); else error("Unable to write file."); if (bak) { if (sbuf.st_nlink) { cp(bakname, fname); unlink(bakname); } else rename(bakname, fname); } } return rc; }