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
21 #include "sysincludes.h"
25 #include "nameclash.h"
30 typedef struct Arg_t {
42 static int dos_doctorfat(direntry_t *entry, MainParam_t *mp)
44 Fs_t *Fs = getFs(mp->File);
45 Arg_t *arg=(Arg_t *) mp->arg;
47 if(!arg->markbad && isRootEntry(entry)) {
48 /* if not root directory, change it */
49 set_word(entry->dir.start, arg->fat & 0xffff);
50 set_word(entry->dir.startHi, arg->fat >> 16);
52 set_dword(entry->dir.size, arg->size);
59 static int unix_doctorfat(MainParam_t *mp UNUSEDP)
61 fprintf(stderr,"File does not reside on a Dos fs\n");
65 static void usage(int ret) NORETURN;
66 static void usage(int ret)
69 "Mtools version %s, dated %s\n", mversion, mdate);
71 "Usage: [-b] %s file fat\n", progname);
75 void mdoctorfat(int argc, char **argv, int mtype UNUSEDP) NORETURN;
76 void mdoctorfat(int argc, char **argv, int mtype UNUSEDP)
81 unsigned int begin, end;
86 /* get command line options */
88 init_clash_handling(& arg.ch);
95 /* get command line options */
96 if(helpFlag(argc, argv))
98 while ((c = getopt(argc, argv, "i:bo:s:h")) != EOF) {
103 set_cmd_line_image(optarg);
109 offset = strtoui(optarg,&endptr,0);
113 arg.size = strtou32(optarg,&endptr,0);
120 check_number_parse_errno((char)c, optarg, endptr);
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 = strtoui(argv[optind+1], 0, 0) + offset;
137 ret=main_loop(&arg.mp, argv + optind, 1);
141 for(i=optind+1; i < argc; i++) {
144 if (*number == '<') {
147 begin = strtoui(number, &eptr, 0);
148 if (eptr && *eptr == '-') {
150 end = strtoui(number, &eptr, 0);
154 if (eptr == number) {
155 fprintf(stderr, "Not a number: %s\n", number);
159 if (eptr && *eptr == '>') {
163 fprintf(stderr, "Not a number: %s\n", eptr);
167 for (j=begin; j <= end; j++) {
169 arg.Fs->fat_encode(arg.Fs, j+offset, arg.Fs->last_fat ^ 6 ^ 8);
172 arg.Fs->fat_encode(arg.Fs, address, j+offset);
179 if (address && !arg.markbad) {
180 arg.Fs->fat_encode(arg.Fs, address, arg.Fs->end_fat);