void mshortname(int argc, char **argv, int type) { struct MainParam_t mp; int c; if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:h")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg); break; case 'h': usage(0); case '?': usage(1); } } if(optind == argc) { usage(0); } if (optind >= argc) usage(1); init_mp(&mp); mp.callback = print_short_name; mp.arg = NULL; mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR; exit(main_loop(&mp, argv + optind, argc - optind)); }
void mdel(int argc, char **argv, int deltype) { Arg_t arg; MainParam_t mp; int c,i; arg.verbose = 0; if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:vh")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg); break; case 'v': arg.verbose = 1; break; case 'h': usage(0); default: usage(1); } } if(argc == optind) usage(1); init_mp(&mp); mp.callback = del_file; mp.arg = (void *) &arg; mp.openflags = O_RDWR; arg.deltype = deltype; switch(deltype){ case 0: mp.lookupflags = ACCEPT_PLAIN; /* mdel */ break; case 1: mp.lookupflags = ACCEPT_DIR; /* mrd */ break; case 2: mp.lookupflags = ACCEPT_DIR | ACCEPT_PLAIN; /* mdeltree */ break; } mp.lookupflags |= NO_DOTS; for(i=optind;i<argc;i++) { int b,l; if(argv[i][0] && argv[i][1] == ':') b = 2; else b = 0; l = strlen(argv[i]+b); if(l > 1 && argv[i][b+l-1] == '/') argv[i][b+l-1] = '\0'; } exit(main_loop(&mp, argv + optind, argc - optind)); }
void mmd(int argc, char **argv, int type) { Arg_t arg; int c; /* get command line options */ init_clash_handling(& arg.ch); /* get command line options */ if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:D:oh")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg); break; case '?': usage(1); case 'o': handle_clash_options(&arg.ch, c); break; case 'D': if(handle_clash_options(&arg.ch, *optarg)) usage(1); break; case 'h': usage(0); default: usage(1); break; } } if (argc - optind < 1) usage(1); init_mp(&arg.mp); arg.mp.arg = (void *) &arg; arg.mp.openflags = O_RDWR; arg.mp.callback = createDirCallback; arg.mp.lookupflags = OPEN_PARENT | DO_OPEN_DIRS; exit(main_loop(&arg.mp, argv + optind, argc - optind)); }
void mshowfat(int argc, char **argv, int mtype) { Arg_t arg; int c, ret; /* get command line options */ init_clash_handling(& arg.ch); /* get command line options */ if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:h")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'h': usage(0); case '?': usage(1); break; } } if (argc - optind < 1) usage(1); /* only 1 file to copy... */ init_mp(&arg.mp); arg.mp.arg = (void *) &arg; arg.mp.callback = dos_showfat; arg.mp.unixcallback = unix_showfat; arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN; ret=main_loop(&arg.mp, argv + optind, argc - optind); exit(ret); }
void mattrib(int argc, char **argv, int type) { Arg_t arg; int view; int c; int concise; int replay; char *ptr; int wantUsage; arg.add = 0; arg.remove = 0xff; arg.recursive = 0; arg.doPrintName = 1; view = 0; concise = 0; replay = 0; wantUsage = 0; if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:/ahrsAHRSXp")) != EOF) { switch (c) { case 'h': wantUsage = 1; /* FALL THROUGH */ default: arg.remove &= ~letterToCode(c); break; case 'i': set_cmd_line_image(optarg, 0); break; case 'p': replay = 1; break; case '/': arg.recursive = 1; break; case 'X': concise = 1; break; case '?': usage(1); } } if(optind == argc && wantUsage) { usage(0); } for(;optind < argc;optind++) { switch(argv[optind][0]) { case '+': for(ptr = argv[optind] + 1; *ptr; ptr++) arg.add |= letterToCode(*ptr); continue; case '-': for(ptr = argv[optind] + 1; *ptr; ptr++) arg.remove &= ~letterToCode(*ptr); continue; } break; } if(arg.remove == 0xff && !arg.add) view = 1; if (optind >= argc) usage(1); init_mp(&arg.mp); if(view){ if(concise) { arg.mp.callback = concise_view_attrib; arg.doPrintName = (argc - optind > 1 || arg.recursive || strpbrk(argv[optind], "*[?") != 0); } else if (replay) { arg.mp.callback = replay_attrib; } else arg.mp.callback = view_attrib; arg.mp.openflags = O_RDONLY; } else { arg.mp.callback = attrib_file; arg.mp.openflags = O_RDWR; } if(arg.recursive) arg.mp.dirCallback = recursive_attrib; arg.mp.arg = (void *) &arg; arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR; if(arg.recursive) arg.mp.lookupflags |= DO_OPEN_DIRS | NO_DOTS; exit(main_loop(&arg.mp, argv + optind, argc - optind)); }
void mdir(int argc, char **argv, int type) { int ret; MainParam_t mp; int faked; int c; const char *fakedArgv[] = { "." }; concise = 0; recursive = 0; wide = all = 0; /* first argument */ if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:waXbfds/h")) != EOF) { switch(c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'w': wide = 1; break; case 'a': all = 1; break; case 'b': case 'X': concise = 1; /*recursive = 1;*/ break; case 's': case '/': recursive = 1; break; case 'f': fast = 1; break; case 'd': debug = 1; break; #if 0 case 't': /* test mode */ testmode = 1; break; #endif case 'h': usage(0); default: usage(1); } } /* fake an argument */ faked = 0; if (optind == argc) { argv = (char **)fakedArgv; argc = 1; optind = 0; } init_mp(&mp); currentDrive = '\0'; currentDir = 0; RootDir = 0; dirPath = 0; #if 0 if (testmode) { mp.lookupflags = ACCEPT_DIR | NO_DOTS; mp.dirCallback = test_directory; } else #endif if(recursive) { mp.lookupflags = ACCEPT_DIR | DO_OPEN_DIRS | NO_DOTS; mp.dirCallback = list_recurs_directory; } else { mp.lookupflags = ACCEPT_DIR | ACCEPT_PLAIN | DO_OPEN_DIRS; mp.dirCallback = list_non_recurs_directory; mp.callback = list_file; } mp.longname = global_longname; mp.shortname = global_shortname; ret=main_loop(&mp, argv + optind, argc - optind); leaveDirectory(ret); leaveDrive(ret); exit(ret); }
void mlabel(int argc, char **argv, int type) { char *newLabel; int verbose, clear, interactive, show; direntry_t entry; int result=0; char longname[VBUFSIZE]; char shortname[45]; ClashHandling_t ch; struct MainParam_t mp; Stream_t *RootDir; int c; int mangled; enum { SER_NONE, SER_RANDOM, SER_SET } set_serial = SER_NONE; long serial = 0; int need_write_boot = 0; int have_boot = 0; char *eptr; union bootsector boot; Stream_t *Fs=0; int r; struct label_blk_t *labelBlock; int isRo=0; int *isRop=NULL; init_clash_handling(&ch); ch.name_converter = label_name; ch.ignore_entry = -2; verbose = 0; clear = 0; show = 0; if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:vcsnN:h")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'v': verbose = 1; break; case 'c': clear = 1; break; case 's': show = 1; break; case 'n': set_serial = SER_RANDOM; srandom((long)time (0)); serial=random(); break; case 'N': set_serial = SER_SET; serial = strtol(optarg, &eptr, 16); if(*eptr) { fprintf(stderr, "%s not a valid serial number\n", optarg); exit(1); } break; case 'h': usage(0); default: usage(1); } } if (argc - optind != 1 || !argv[optind][0] || argv[optind][1] != ':') usage(1); init_mp(&mp); newLabel = argv[optind]+2; if(strlen(newLabel) > VBUFSIZE) { fprintf(stderr, "Label too long\n"); FREE(&RootDir); exit(1); } interactive = !show && !clear &&!newLabel[0] && (set_serial == SER_NONE); if(!clear && !newLabel[0]) { isRop = &isRo; } RootDir = open_root_dir(argv[optind][0], isRop ? 0 : O_RDWR, isRop); if(isRo) { show = 1; interactive = 0; } if(!RootDir) { fprintf(stderr, "%s: Cannot initialize drive\n", argv[0]); exit(1); } initializeDirentry(&entry, RootDir); r=vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY, shortname, longname); if (r == -2) { FREE(&RootDir); exit(1); } if(show || interactive){ if(isNotFound(&entry)) printf(" Volume has no label\n"); else if (*longname) printf(" Volume label is %s (abbr=%s)\n", longname, shortname); else printf(" Volume label is %s\n", shortname); } /* ask for new label */ if(interactive){ newLabel = longname; fprintf(stderr,"Enter the new volume label : "); if(fgets(newLabel, VBUFSIZE, stdin) == NULL) { newLabel[0] = '\0'; fprintf(stderr, "\n"); } if(newLabel[0]) newLabel[strlen(newLabel)-1] = '\0'; } if((!show || newLabel[0]) && !isNotFound(&entry)){ /* if we have a label, wipe it out before putting new one */ if(interactive && newLabel[0] == '\0') if(ask_confirmation("Delete volume label (y/n): ")){ FREE(&RootDir); exit(0); } entry.dir.attr = 0; /* for old mlabel */ wipeEntry(&entry); } if (newLabel[0] != '\0') { ch.ignore_entry = 1; result = mwrite_one(RootDir,newLabel,0,labelit,NULL,&ch) ? 0 : 1; } have_boot = 0; if( (!show || newLabel[0]) || set_serial != SER_NONE) { Fs = GetFs(RootDir); have_boot = (force_read(Fs,boot.characters,0,sizeof(boot)) == sizeof(boot)); } if(WORD_S(fatlen)) { labelBlock = &boot.boot.ext.old.labelBlock; } else { labelBlock = &boot.boot.ext.fat32.labelBlock; } if(!show || newLabel[0]){ dos_name_t dosname; const char *shrtLabel; doscp_t *cp; if(!newLabel[0]) shrtLabel = "NO NAME "; else shrtLabel = newLabel; cp = GET_DOSCONVERT(Fs); label_name(cp, shrtLabel, verbose, &mangled, &dosname); if(have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29) { strncpy(labelBlock->label, dosname.base, 11); need_write_boot = 1; } } if((set_serial != SER_NONE) & have_boot) { if(have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29) { set_dword(labelBlock->serial, serial); need_write_boot = 1; } } if(need_write_boot) { force_write(Fs, (char *)&boot, 0, sizeof(boot)); } FREE(&RootDir); exit(result); }
void minfo(int argc, char **argv, int type) { union bootsector boot; char name[EXPAND_BUF]; int media; unsigned long tot_sectors; int size_code; int sector_size; int i; struct device dev; char drive; int verbose=0; int c; Stream_t *Stream; struct label_blk_t *labelBlock; int have_drive = 0; unsigned long sect_per_track; int tracks_match=0; int hidden; char *imgFile=NULL; if(helpFlag(argc, argv)) usage(0); while ((c = getopt(argc, argv, "i:vh")) != EOF) { switch (c) { case 'i': set_cmd_line_image(optarg); imgFile=optarg; break; case 'v': verbose = 1; break; case 'h': usage(0); default: usage(1); } } for(;optind <= argc; optind++) { if(optind == argc) { if(have_drive) break; drive = get_default_drive(); } else { if(!argv[optind][0] || argv[optind][1] != ':') usage(1); drive = toupper(argv[optind][0]); } have_drive = 1; if(! (Stream = find_device(drive, O_RDONLY, &dev, &boot, name, &media, 0, NULL))) exit(1); tot_sectors = DWORD_S(bigsect); SET_INT(tot_sectors, WORD_S(psect)); sector_size = WORD_S(secsiz); size_code=2; for(i=0; i<7; i++) { if(sector_size == 128 << i) { size_code = i; break; } } printf("device information:\n"); printf("===================\n"); printf("filename=\"%s\"\n", name); printf("sectors per track: %d\n", dev.sectors); printf("heads: %d\n", dev.heads); printf("cylinders: %d\n\n", dev.tracks); sect_per_track = dev.sectors * dev.heads; if(sect_per_track != 0) { printf("mformat command line: mformat "); hidden = DWORD_S(nhs); if(tot_sectors == dev.tracks * sect_per_track - hidden % sect_per_track) { tracks_match=1; printf("-t %d ", dev.tracks); } else { printf("-T %ld ", tot_sectors); } if(imgFile != NULL) printf("-i %s ", imgFile); printf (" -h %d -s %d ", dev.heads, dev.sectors); if(hidden || !tracks_match) printf("-H %d ", hidden); if(size_code != 2) printf("-S %d ",size_code); printf("%c:\n", tolower(drive)); printf("\n"); } printf("bootsector information\n"); printf("======================\n"); printf("banner:\"%.8s\"\n", boot.boot.banner); printf("sector size: %d bytes\n", WORD_S(secsiz)); printf("cluster size: %d sectors\n", boot.boot.clsiz); printf("reserved (boot) sectors: %d\n", WORD_S(nrsvsect)); printf("fats: %d\n", boot.boot.nfat); printf("max available root directory slots: %d\n", WORD_S(dirents)); printf("small size: %d sectors\n", WORD_S(psect)); printf("media descriptor byte: 0x%x\n", boot.boot.descr); printf("sectors per fat: %d\n", WORD_S(fatlen)); printf("sectors per track: %d\n", WORD_S(nsect)); printf("heads: %d\n", WORD_S(nheads)); printf("hidden sectors: %d\n", DWORD_S(nhs)); printf("big size: %d sectors\n", DWORD_S(bigsect)); if(WORD_S(fatlen)) { labelBlock = &boot.boot.ext.old.labelBlock; } else { labelBlock = &boot.boot.ext.fat32.labelBlock; } printf("physical drive id: 0x%x\n", labelBlock->physdrive); printf("reserved=0x%x\n", labelBlock->reserved); printf("dos4=0x%x\n", labelBlock->dos4); printf("serial number: %08X\n", _DWORD(labelBlock->serial)); printf("disk label=\"%11.11s\"\n", labelBlock->label); printf("disk type=\"%8.8s\"\n", labelBlock->fat_type); if(!WORD_S(fatlen)){ printf("Big fatlen=%u\n", DWORD_S(ext.fat32.bigFat)); printf("Extended flags=0x%04x\n", WORD_S(ext.fat32.extFlags)); printf("FS version=0x%04x\n", WORD_S(ext.fat32.fsVersion)); printf("rootCluster=%u\n", DWORD_S(ext.fat32.rootCluster)); if(WORD_S(ext.fat32.infoSector) != MAX16) printf("infoSector location=%d\n", WORD_S(ext.fat32.infoSector)); if(WORD_S(ext.fat32.backupBoot) != MAX16) printf("backup boot sector=%d\n", WORD_S(ext.fat32.backupBoot)); displayInfosector(Stream,&boot); } if(verbose) { int size; unsigned char *buf; printf("\n"); size = WORD_S(secsiz); buf = (unsigned char *) malloc(size); if(!buf) { fprintf(stderr, "Out of memory error\n"); exit(1); } size = READS(Stream, buf, (mt_off_t) 0, size); if(size < 0) { perror("read boot sector"); exit(1); } print_sector("Boot sector hexdump", buf, size); } } FREE(&Stream); exit(0); }
void mbadblocks(int argc, char **argv, int type) { unsigned int i; unsigned int startSector=2; unsigned int endSector=0; struct MainParam_t mp; Fs_t *Fs; Stream_t *Dir; int ret; char *filename = NULL; char c; unsigned int badClus; int sectorMode=0; int writeMode=0; while ((c = getopt(argc, argv, "i:s:cwS:E:")) != EOF) { switch(c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'c': checkListTwice(filename); filename = strdup(optarg); break; case 's': checkListTwice(filename); filename = strdup(optarg); sectorMode = 1; break; case 'S': startSector = atol(optarg); break; case 'E': endSector = atol(optarg); break; case 'w': writeMode = 1; break; case 'h': usage(0); default: usage(1); } } if (argc != optind+1 || !argv[optind][0] || argv[optind][1] != ':' || argv[optind][2]) { usage(1); } init_mp(&mp); Dir = open_root_dir(argv[optind][0], O_RDWR, NULL); if (!Dir) { fprintf(stderr,"%s: Cannot initialize drive\n", argv[0]); exit(1); } Fs = (Fs_t *)GetFs(Dir); in_len = Fs->cluster_size * Fs->sector_size; in_buf = malloc(in_len); if(!in_buf) { printOom(); ret = 1; goto exit_0; } if(writeMode) { int i; pat_buf=malloc(in_len * N_PATTERN); if(!pat_buf) { printOom(); ret = 1; goto exit_0; } srandom(time(NULL)); for(i=0; i < in_len * N_PATTERN; i++) { pat_buf[i] = random(); } } for(i=0; i < Fs->clus_start; i++ ){ ret = READS(Fs->Next, in_buf, sectorsToBytes((Stream_t*)Fs, i), Fs->sector_size); if( ret < 0 ){ perror("early error"); goto exit_0; } if(ret < (signed int) Fs->sector_size){ fprintf(stderr,"end of file in file_read\n"); ret = 1; goto exit_0; } } ret = 0; badClus = Fs->last_fat + 1; if(startSector < 2) startSector = 2; if(endSector > Fs->num_clus + 2 || endSector <= 0) endSector = Fs->num_clus + 2; if(filename) { char line[80]; FILE *f = fopen(filename, "r"); if(f == NULL) { fprintf(stderr, "Could not open %s (%s)\n", filename, strerror(errno)); ret = 1; goto exit_0; } while(fgets(line, sizeof(line), f)) { char *ptr = line + strspn(line, " \t"); long offset = strtoul(ptr, 0, 0); if(sectorMode) offset = (offset-Fs->clus_start)/Fs->cluster_size + 2; if(offset < 2) { fprintf(stderr, "Sector before start\n"); } else if(offset >= Fs->num_clus) { fprintf(stderr, "Sector beyond end\n"); } else { mark(Fs, offset, badClus); ret = 1; } } } else { Stream_t *dev; dev = Fs->Next; if(dev->Next) dev = dev->Next; in_len = Fs->cluster_size * Fs->sector_size; if(writeMode) { /* Write pattern */ for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, pat_buf + in_len * (i % N_PATTERN), 1); } /* Flush cache, so that we are sure we read the data back from disk, rather than from the cache */ if(!got_signal) DISCARD(dev); /* Read data back, and compare to pattern */ for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, pat_buf + in_len * (i % N_PATTERN), 0); } } else { for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, NULL, 0); } } } exit_0: FREE(&Dir); exit(ret); }