1 /* Copyright 1999,2001,2002,2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
17 * Test program for doctoring the fat
23 #include "sysincludes.h"
29 #include "nameclash.h"
34 typedef struct Arg_t {
46 static int dos_doctorfat(direntry_t *entry, MainParam_t *mp)
48 Fs_t *Fs = getFs(mp->File);
49 Arg_t *arg=(Arg_t *) mp->arg;
51 if(!arg->markbad && entry->entry != -3) {
52 /* if not root directory, change it */
53 set_word(entry->dir.start, arg->fat & 0xffff);
54 set_word(entry->dir.startHi, arg->fat >> 16);
56 set_dword(entry->dir.size, arg->size);
63 static int unix_doctorfat(MainParam_t *mp)
65 fprintf(stderr,"File does not reside on a Dos fs\n");
69 static void usage(int ret) NORETURN;
70 static void usage(int ret)
73 "Mtools version %s, dated %s\n", mversion, mdate);
75 "Usage: [-b] %s file fat\n", progname);
79 void mdoctorfat(int argc, char **argv, int mtype)
83 long address, begin, end;
88 /* get command line options */
90 init_clash_handling(& arg.ch);
97 /* get command line options */
98 if(helpFlag(argc, argv))
100 while ((c = getopt(argc, argv, "i:bo:s:h")) != EOF) {
103 set_cmd_line_image(optarg, 0);
109 offset = strtoul(optarg,0,0);
113 arg.size = strtoul(optarg,0,0);
123 if (argc - optind < 2)
127 /* only 1 file to copy... */
129 arg.mp.arg = (void *) &arg;
131 arg.mp.callback = dos_doctorfat;
132 arg.mp.unixcallback = unix_doctorfat;
134 arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN;
135 arg.mp.openflags = O_RDWR;
136 arg.fat = strtoul(argv[optind+1], 0, 0) + offset;
137 ret=main_loop(&arg.mp, argv + optind, 1);
141 for(i=optind+1; i < argc; i++) {
143 if (*number == '<') {
146 begin = strtoul(number, &eptr, 0);
147 if (eptr && *eptr == '-') {
149 end = strtoul(number, &eptr, 0);
153 if (eptr == number) {
154 fprintf(stderr, "Not a number: %s\n", number);
158 if (eptr && *eptr == '>') {
162 fprintf(stderr, "Not a number: %s\n", eptr);
166 for (j=begin; j <= end; j++) {
168 arg.Fs->fat_encode(arg.Fs, j+offset, arg.Fs->last_fat ^ 6 ^ 8);
171 arg.Fs->fat_encode(arg.Fs, address, j+offset);
178 if (address && !arg.markbad) {
179 arg.Fs->fat_encode(arg.Fs, address, arg.Fs->end_fat);