1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999,2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * fsck_pcfs -- main routines.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
36*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
37*7c478bd9Sstevel@tonic-gate #include <strings.h>
38*7c478bd9Sstevel@tonic-gate #include <libintl.h>
39*7c478bd9Sstevel@tonic-gate #include <locale.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
42*7c478bd9Sstevel@tonic-gate #include "pcfs_common.h"
43*7c478bd9Sstevel@tonic-gate #include "fsck_pcfs.h"
44*7c478bd9Sstevel@tonic-gate #include "pcfs_bpb.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate int32_t BytesPerCluster;
47*7c478bd9Sstevel@tonic-gate int32_t TotalClusters;
48*7c478bd9Sstevel@tonic-gate int32_t LastCluster;
49*7c478bd9Sstevel@tonic-gate off64_t FirstClusterOffset;
50*7c478bd9Sstevel@tonic-gate off64_t PartitionOffset;
51*7c478bd9Sstevel@tonic-gate bpb_t TheBIOSParameterBlock;
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate * {Output,Input}Image are the file names where we should write the
55*7c478bd9Sstevel@tonic-gate * checked fs image and from which we should read the initial fs.
56*7c478bd9Sstevel@tonic-gate * The image capability is designed for debugging purposes.
57*7c478bd9Sstevel@tonic-gate */
58*7c478bd9Sstevel@tonic-gate static char *OutputImage = NULL;
59*7c478bd9Sstevel@tonic-gate static char *InputImage = NULL;
60*7c478bd9Sstevel@tonic-gate static int WritableOnly = 0; /* -o w, check writable fs' only */
61*7c478bd9Sstevel@tonic-gate static int Mflag = 0; /* -m, sanity check if fs is mountable */
62*7c478bd9Sstevel@tonic-gate static int Preen = 0; /* -o p, preen; non-interactive */
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate * By default be quick; skip verify reads.
65*7c478bd9Sstevel@tonic-gate * If the user wants more exhaustive checking,
66*7c478bd9Sstevel@tonic-gate * they should run with the -o v option.
67*7c478bd9Sstevel@tonic-gate */
68*7c478bd9Sstevel@tonic-gate static int Quick = 1;
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate int ReadOnly = 0;
71*7c478bd9Sstevel@tonic-gate int IsFAT32 = 0;
72*7c478bd9Sstevel@tonic-gate int Verbose = 0;
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate int AlwaysYes = 0; /* -y or -Y, assume a yes answer to all questions */
75*7c478bd9Sstevel@tonic-gate int AlwaysNo = 0; /* -n or -N, assume a no answer to all questions */
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate extern ClusterContents TheRootDir;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate * Function definitions
81*7c478bd9Sstevel@tonic-gate */
82*7c478bd9Sstevel@tonic-gate static void
passOne(int fd)83*7c478bd9Sstevel@tonic-gate passOne(int fd)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate if (!Quick)
86*7c478bd9Sstevel@tonic-gate findBadClusters(fd);
87*7c478bd9Sstevel@tonic-gate scanAndFixMetadata(fd);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate static void
writeBackChanges(int fd)91*7c478bd9Sstevel@tonic-gate writeBackChanges(int fd)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate writeFATMods(fd);
94*7c478bd9Sstevel@tonic-gate if (!IsFAT32)
95*7c478bd9Sstevel@tonic-gate writeRootDirMods(fd);
96*7c478bd9Sstevel@tonic-gate writeClusterMods(fd);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate static void
tryOpen(int * fd,char * openMe,int oflag,int exitOnFailure)100*7c478bd9Sstevel@tonic-gate tryOpen(int *fd, char *openMe, int oflag, int exitOnFailure)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate int saveError;
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate if ((*fd = open(openMe, oflag)) < 0) {
105*7c478bd9Sstevel@tonic-gate if (exitOnFailure == RETURN_ON_OPEN_FAILURE)
106*7c478bd9Sstevel@tonic-gate return;
107*7c478bd9Sstevel@tonic-gate saveError = errno;
108*7c478bd9Sstevel@tonic-gate mountSanityCheckFails();
109*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", openMe);
110*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, strerror(saveError));
111*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
112*7c478bd9Sstevel@tonic-gate exit(1);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate static void
doOpen(int * inFD,int * outFD,char * name,char * outName)117*7c478bd9Sstevel@tonic-gate doOpen(int *inFD, int *outFD, char *name, char *outName)
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate if (ReadOnly) {
120*7c478bd9Sstevel@tonic-gate tryOpen(inFD, name, O_RDONLY, EXIT_ON_OPEN_FAILURE);
121*7c478bd9Sstevel@tonic-gate *outFD = -1;
122*7c478bd9Sstevel@tonic-gate } else {
123*7c478bd9Sstevel@tonic-gate tryOpen(inFD, name, O_RDWR, RETURN_ON_OPEN_FAILURE);
124*7c478bd9Sstevel@tonic-gate if (*inFD < 0) {
125*7c478bd9Sstevel@tonic-gate if (errno != EACCES || WritableOnly) {
126*7c478bd9Sstevel@tonic-gate int saveError = errno;
127*7c478bd9Sstevel@tonic-gate mountSanityCheckFails();
128*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
129*7c478bd9Sstevel@tonic-gate gettext("%s: "), name);
130*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, strerror(saveError));
131*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
132*7c478bd9Sstevel@tonic-gate exit(2);
133*7c478bd9Sstevel@tonic-gate } else {
134*7c478bd9Sstevel@tonic-gate tryOpen(inFD, name, O_RDONLY,
135*7c478bd9Sstevel@tonic-gate EXIT_ON_OPEN_FAILURE);
136*7c478bd9Sstevel@tonic-gate AlwaysYes = 0;
137*7c478bd9Sstevel@tonic-gate AlwaysNo = 1;
138*7c478bd9Sstevel@tonic-gate ReadOnly = 1;
139*7c478bd9Sstevel@tonic-gate *outFD = -1;
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate } else {
142*7c478bd9Sstevel@tonic-gate *outFD = *inFD;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate
146*7c478bd9Sstevel@tonic-gate if (outName != NULL) {
147*7c478bd9Sstevel@tonic-gate tryOpen(outFD, outName, (O_RDWR | O_CREAT),
148*7c478bd9Sstevel@tonic-gate EXIT_ON_OPEN_FAILURE);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate (void) printf("** %s %s\n", name,
152*7c478bd9Sstevel@tonic-gate ReadOnly ? gettext("(NO WRITE)") : "");
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate static void
openFS(char * special,int * inFD,int * outFD)156*7c478bd9Sstevel@tonic-gate openFS(char *special, int *inFD, int *outFD)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate struct stat dinfo;
159*7c478bd9Sstevel@tonic-gate char *actualDisk = NULL;
160*7c478bd9Sstevel@tonic-gate char *suffix = NULL;
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate if (Verbose)
163*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Opening file system.\n"));
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate if (InputImage == NULL) {
166*7c478bd9Sstevel@tonic-gate actualDisk = stat_actual_disk(special, &dinfo, &suffix);
167*7c478bd9Sstevel@tonic-gate /*
168*7c478bd9Sstevel@tonic-gate * Destination exists, now find more about it.
169*7c478bd9Sstevel@tonic-gate */
170*7c478bd9Sstevel@tonic-gate if (!(S_ISCHR(dinfo.st_mode))) {
171*7c478bd9Sstevel@tonic-gate mountSanityCheckFails();
172*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
173*7c478bd9Sstevel@tonic-gate gettext("\n%s: device name must be a "
174*7c478bd9Sstevel@tonic-gate "character special device.\n"), actualDisk);
175*7c478bd9Sstevel@tonic-gate exit(2);
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate } else {
178*7c478bd9Sstevel@tonic-gate actualDisk = InputImage;
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate doOpen(inFD, outFD, actualDisk, OutputImage);
181*7c478bd9Sstevel@tonic-gate if (suffix) {
182*7c478bd9Sstevel@tonic-gate if ((PartitionOffset =
183*7c478bd9Sstevel@tonic-gate findPartitionOffset(*inFD, suffix)) < 0) {
184*7c478bd9Sstevel@tonic-gate mountSanityCheckFails();
185*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
186*7c478bd9Sstevel@tonic-gate gettext("Unable to find logical drive %s\n"),
187*7c478bd9Sstevel@tonic-gate suffix);
188*7c478bd9Sstevel@tonic-gate exit(2);
189*7c478bd9Sstevel@tonic-gate } else if (Verbose) {
190*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
191*7c478bd9Sstevel@tonic-gate gettext("Partition starts at offset %lld\n"),
192*7c478bd9Sstevel@tonic-gate PartitionOffset);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate } else {
195*7c478bd9Sstevel@tonic-gate PartitionOffset = 0;
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate void
usage()200*7c478bd9Sstevel@tonic-gate usage()
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
203*7c478bd9Sstevel@tonic-gate gettext("pcfs Usage: fsck -F pcfs [-o v|p|w] special-file\n"));
204*7c478bd9Sstevel@tonic-gate exit(1);
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate static
208*7c478bd9Sstevel@tonic-gate char *LegalOpts[] = {
209*7c478bd9Sstevel@tonic-gate #define VFLAG 0
210*7c478bd9Sstevel@tonic-gate "v",
211*7c478bd9Sstevel@tonic-gate #define PFLAG 1
212*7c478bd9Sstevel@tonic-gate "p",
213*7c478bd9Sstevel@tonic-gate #define WFLAG 2
214*7c478bd9Sstevel@tonic-gate "w",
215*7c478bd9Sstevel@tonic-gate #define DFLAG 3
216*7c478bd9Sstevel@tonic-gate "d",
217*7c478bd9Sstevel@tonic-gate #define IFLAG 4
218*7c478bd9Sstevel@tonic-gate "i",
219*7c478bd9Sstevel@tonic-gate #define OFLAG 5
220*7c478bd9Sstevel@tonic-gate "o",
221*7c478bd9Sstevel@tonic-gate NULL
222*7c478bd9Sstevel@tonic-gate };
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate static void
parseSubOptions(char * optsstr)225*7c478bd9Sstevel@tonic-gate parseSubOptions(char *optsstr)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate char *value;
228*7c478bd9Sstevel@tonic-gate int c;
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate while (*optsstr != '\0') {
231*7c478bd9Sstevel@tonic-gate switch (c = getsubopt(&optsstr, LegalOpts, &value)) {
232*7c478bd9Sstevel@tonic-gate case VFLAG:
233*7c478bd9Sstevel@tonic-gate Quick = 0;
234*7c478bd9Sstevel@tonic-gate break;
235*7c478bd9Sstevel@tonic-gate case PFLAG:
236*7c478bd9Sstevel@tonic-gate Preen++;
237*7c478bd9Sstevel@tonic-gate break;
238*7c478bd9Sstevel@tonic-gate case WFLAG:
239*7c478bd9Sstevel@tonic-gate WritableOnly++;
240*7c478bd9Sstevel@tonic-gate break;
241*7c478bd9Sstevel@tonic-gate case DFLAG:
242*7c478bd9Sstevel@tonic-gate Verbose++;
243*7c478bd9Sstevel@tonic-gate break;
244*7c478bd9Sstevel@tonic-gate case IFLAG:
245*7c478bd9Sstevel@tonic-gate if (value == NULL) {
246*7c478bd9Sstevel@tonic-gate missing_arg(LegalOpts[c]);
247*7c478bd9Sstevel@tonic-gate } else {
248*7c478bd9Sstevel@tonic-gate InputImage = value;
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate break;
251*7c478bd9Sstevel@tonic-gate case OFLAG:
252*7c478bd9Sstevel@tonic-gate if (value == NULL) {
253*7c478bd9Sstevel@tonic-gate missing_arg(LegalOpts[c]);
254*7c478bd9Sstevel@tonic-gate } else {
255*7c478bd9Sstevel@tonic-gate OutputImage = value;
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate break;
258*7c478bd9Sstevel@tonic-gate default:
259*7c478bd9Sstevel@tonic-gate bad_arg(value);
260*7c478bd9Sstevel@tonic-gate break;
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate static void
sanityCheckOpts(void)266*7c478bd9Sstevel@tonic-gate sanityCheckOpts(void)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate if (WritableOnly && ReadOnly) {
269*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
270*7c478bd9Sstevel@tonic-gate gettext("-w option may not be used with the -n "
271*7c478bd9Sstevel@tonic-gate "or -m options\n"));
272*7c478bd9Sstevel@tonic-gate exit(4);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate
276*7c478bd9Sstevel@tonic-gate static void
confirmMountable(char * special,int fd)277*7c478bd9Sstevel@tonic-gate confirmMountable(char *special, int fd)
278*7c478bd9Sstevel@tonic-gate {
279*7c478bd9Sstevel@tonic-gate char *printName;
280*7c478bd9Sstevel@tonic-gate int okayToMount = 1;
281*7c478bd9Sstevel@tonic-gate
282*7c478bd9Sstevel@tonic-gate printName = InputImage ? InputImage : special;
283*7c478bd9Sstevel@tonic-gate
284*7c478bd9Sstevel@tonic-gate if (!IsFAT32) {
285*7c478bd9Sstevel@tonic-gate /* make sure we can at least read the root directory */
286*7c478bd9Sstevel@tonic-gate getRootDirectory(fd);
287*7c478bd9Sstevel@tonic-gate if (TheRootDir.bytes == NULL)
288*7c478bd9Sstevel@tonic-gate okayToMount = 0;
289*7c478bd9Sstevel@tonic-gate } else {
290*7c478bd9Sstevel@tonic-gate /* check the bit designed into FAT32 for this purpose */
291*7c478bd9Sstevel@tonic-gate okayToMount = checkFAT32CleanBit(fd);
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate if (okayToMount) {
294*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
295*7c478bd9Sstevel@tonic-gate gettext("pcfs fsck: sanity check: %s okay\n"), printName);
296*7c478bd9Sstevel@tonic-gate exit(0);
297*7c478bd9Sstevel@tonic-gate } else {
298*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
299*7c478bd9Sstevel@tonic-gate gettext("pcfs fsck: sanity check: %s needs checking\n"),
300*7c478bd9Sstevel@tonic-gate printName);
301*7c478bd9Sstevel@tonic-gate exit(32);
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate
305*7c478bd9Sstevel@tonic-gate void
mountSanityCheckFails(void)306*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(void)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate if (Mflag) {
309*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
310*7c478bd9Sstevel@tonic-gate gettext("pcfs fsck: sanity check failed: "));
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate }
313*7c478bd9Sstevel@tonic-gate
314*7c478bd9Sstevel@tonic-gate /*
315*7c478bd9Sstevel@tonic-gate * preenBail
316*7c478bd9Sstevel@tonic-gate * Routine that other routines can call if they would go into a
317*7c478bd9Sstevel@tonic-gate * state where they need user input. They can send an optional
318*7c478bd9Sstevel@tonic-gate * message string to be printed before the exit. Caller should
319*7c478bd9Sstevel@tonic-gate * send a NULL string if they don't have an exit message.
320*7c478bd9Sstevel@tonic-gate */
321*7c478bd9Sstevel@tonic-gate void
preenBail(char * outString)322*7c478bd9Sstevel@tonic-gate preenBail(char *outString)
323*7c478bd9Sstevel@tonic-gate {
324*7c478bd9Sstevel@tonic-gate /*
325*7c478bd9Sstevel@tonic-gate * If we are running in the 'preen' mode, we got here because
326*7c478bd9Sstevel@tonic-gate * we reached a situation that would require user intervention.
327*7c478bd9Sstevel@tonic-gate * We have no choice but to bail at this point.
328*7c478bd9Sstevel@tonic-gate */
329*7c478bd9Sstevel@tonic-gate if (Preen) {
330*7c478bd9Sstevel@tonic-gate if (outString)
331*7c478bd9Sstevel@tonic-gate (void) printf("%s", outString);
332*7c478bd9Sstevel@tonic-gate (void) printf(gettext("FILE SYSTEM FIX REQUIRES USER "
333*7c478bd9Sstevel@tonic-gate "INTERVENTION; RUN fsck MANUALLY.\n"));
334*7c478bd9Sstevel@tonic-gate exit(36);
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate }
337*7c478bd9Sstevel@tonic-gate
338*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])339*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate char *string;
342*7c478bd9Sstevel@tonic-gate int ifd, ofd;
343*7c478bd9Sstevel@tonic-gate int c;
344*7c478bd9Sstevel@tonic-gate
345*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
346*7c478bd9Sstevel@tonic-gate
347*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
348*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
349*7c478bd9Sstevel@tonic-gate #endif
350*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
351*7c478bd9Sstevel@tonic-gate
352*7c478bd9Sstevel@tonic-gate if (argc < 2)
353*7c478bd9Sstevel@tonic-gate usage();
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "F:VYNynmo:")) != EOF) {
356*7c478bd9Sstevel@tonic-gate switch (c) {
357*7c478bd9Sstevel@tonic-gate case 'F':
358*7c478bd9Sstevel@tonic-gate string = optarg;
359*7c478bd9Sstevel@tonic-gate if (strcmp(string, "pcfs") != 0)
360*7c478bd9Sstevel@tonic-gate usage();
361*7c478bd9Sstevel@tonic-gate break;
362*7c478bd9Sstevel@tonic-gate case 'V': {
363*7c478bd9Sstevel@tonic-gate char *opt_text;
364*7c478bd9Sstevel@tonic-gate int opt_count;
365*7c478bd9Sstevel@tonic-gate
366*7c478bd9Sstevel@tonic-gate (void) printf(gettext("fsck -F pcfs "));
367*7c478bd9Sstevel@tonic-gate for (opt_count = 1; opt_count < argc;
368*7c478bd9Sstevel@tonic-gate opt_count++) {
369*7c478bd9Sstevel@tonic-gate opt_text = argv[opt_count];
370*7c478bd9Sstevel@tonic-gate if (opt_text)
371*7c478bd9Sstevel@tonic-gate (void) printf(" %s ",
372*7c478bd9Sstevel@tonic-gate opt_text);
373*7c478bd9Sstevel@tonic-gate }
374*7c478bd9Sstevel@tonic-gate (void) printf("\n");
375*7c478bd9Sstevel@tonic-gate exit(0);
376*7c478bd9Sstevel@tonic-gate }
377*7c478bd9Sstevel@tonic-gate break;
378*7c478bd9Sstevel@tonic-gate case 'N':
379*7c478bd9Sstevel@tonic-gate case 'n':
380*7c478bd9Sstevel@tonic-gate AlwaysYes = 0;
381*7c478bd9Sstevel@tonic-gate AlwaysNo = 1;
382*7c478bd9Sstevel@tonic-gate ReadOnly = 1;
383*7c478bd9Sstevel@tonic-gate break;
384*7c478bd9Sstevel@tonic-gate case 'Y':
385*7c478bd9Sstevel@tonic-gate case 'y':
386*7c478bd9Sstevel@tonic-gate AlwaysYes = 1;
387*7c478bd9Sstevel@tonic-gate AlwaysNo = 0;
388*7c478bd9Sstevel@tonic-gate break;
389*7c478bd9Sstevel@tonic-gate case 'm':
390*7c478bd9Sstevel@tonic-gate Mflag++;
391*7c478bd9Sstevel@tonic-gate ReadOnly = 1;
392*7c478bd9Sstevel@tonic-gate break;
393*7c478bd9Sstevel@tonic-gate case 'o':
394*7c478bd9Sstevel@tonic-gate string = optarg;
395*7c478bd9Sstevel@tonic-gate parseSubOptions(string);
396*7c478bd9Sstevel@tonic-gate break;
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate
400*7c478bd9Sstevel@tonic-gate sanityCheckOpts();
401*7c478bd9Sstevel@tonic-gate if (InputImage == NULL && (optind < 0 || optind >= argc))
402*7c478bd9Sstevel@tonic-gate usage();
403*7c478bd9Sstevel@tonic-gate
404*7c478bd9Sstevel@tonic-gate openFS(argv[optind], &ifd, &ofd);
405*7c478bd9Sstevel@tonic-gate readBPB(ifd);
406*7c478bd9Sstevel@tonic-gate
407*7c478bd9Sstevel@tonic-gate /*
408*7c478bd9Sstevel@tonic-gate * -m mountable fs check. This call will not return.
409*7c478bd9Sstevel@tonic-gate */
410*7c478bd9Sstevel@tonic-gate if (Mflag)
411*7c478bd9Sstevel@tonic-gate confirmMountable(argv[optind], ifd);
412*7c478bd9Sstevel@tonic-gate
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate * Pass 1: Find any bad clusters and adjust the FAT and directory
415*7c478bd9Sstevel@tonic-gate * entries accordingly
416*7c478bd9Sstevel@tonic-gate */
417*7c478bd9Sstevel@tonic-gate passOne(ifd);
418*7c478bd9Sstevel@tonic-gate
419*7c478bd9Sstevel@tonic-gate /*
420*7c478bd9Sstevel@tonic-gate * XXX - future passes?
421*7c478bd9Sstevel@tonic-gate * Ideas:
422*7c478bd9Sstevel@tonic-gate * Data relocation for bad clusters with partial read success?
423*7c478bd9Sstevel@tonic-gate * Syncing backup FAT copies with main copy?
424*7c478bd9Sstevel@tonic-gate * Syncing backup root sector for FAT32?
425*7c478bd9Sstevel@tonic-gate */
426*7c478bd9Sstevel@tonic-gate
427*7c478bd9Sstevel@tonic-gate /*
428*7c478bd9Sstevel@tonic-gate * No problems if we made it this far.
429*7c478bd9Sstevel@tonic-gate */
430*7c478bd9Sstevel@tonic-gate printSummary(stdout);
431*7c478bd9Sstevel@tonic-gate writeBackChanges(ofd);
432*7c478bd9Sstevel@tonic-gate return (0);
433*7c478bd9Sstevel@tonic-gate }
434