xref: /aosp_15_r20/external/fsck_msdos/main.c (revision 9558e6ac2e10ab0fef46fdd14187b840555f86f4)
1*9558e6acSTreehugger Robot /*-
2*9558e6acSTreehugger Robot  * SPDX-License-Identifier: BSD-2-Clause
3*9558e6acSTreehugger Robot  *
4*9558e6acSTreehugger Robot  * Copyright (C) 1995 Wolfgang Solfrank
5*9558e6acSTreehugger Robot  * Copyright (c) 1995 Martin Husemann
6*9558e6acSTreehugger Robot  *
7*9558e6acSTreehugger Robot  * Redistribution and use in source and binary forms, with or without
8*9558e6acSTreehugger Robot  * modification, are permitted provided that the following conditions
9*9558e6acSTreehugger Robot  * are met:
10*9558e6acSTreehugger Robot  * 1. Redistributions of source code must retain the above copyright
11*9558e6acSTreehugger Robot  *    notice, this list of conditions and the following disclaimer.
12*9558e6acSTreehugger Robot  * 2. Redistributions in binary form must reproduce the above copyright
13*9558e6acSTreehugger Robot  *    notice, this list of conditions and the following disclaimer in the
14*9558e6acSTreehugger Robot  *    documentation and/or other materials provided with the distribution.
15*9558e6acSTreehugger Robot  *
16*9558e6acSTreehugger Robot  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17*9558e6acSTreehugger Robot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*9558e6acSTreehugger Robot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*9558e6acSTreehugger Robot  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*9558e6acSTreehugger Robot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*9558e6acSTreehugger Robot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*9558e6acSTreehugger Robot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*9558e6acSTreehugger Robot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*9558e6acSTreehugger Robot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*9558e6acSTreehugger Robot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*9558e6acSTreehugger Robot  */
27*9558e6acSTreehugger Robot 
28*9558e6acSTreehugger Robot 
29*9558e6acSTreehugger Robot #include <sys/cdefs.h>
30*9558e6acSTreehugger Robot #ifndef lint
31*9558e6acSTreehugger Robot __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
32*9558e6acSTreehugger Robot static const char rcsid[] =
33*9558e6acSTreehugger Robot   "$FreeBSD$";
34*9558e6acSTreehugger Robot #endif /* not lint */
35*9558e6acSTreehugger Robot 
36*9558e6acSTreehugger Robot #include <stdlib.h>
37*9558e6acSTreehugger Robot #include <string.h>
38*9558e6acSTreehugger Robot #include <stdio.h>
39*9558e6acSTreehugger Robot #include <unistd.h>
40*9558e6acSTreehugger Robot #include <errno.h>
41*9558e6acSTreehugger Robot #include <stdarg.h>
42*9558e6acSTreehugger Robot 
43*9558e6acSTreehugger Robot #include "fsutil.h"
44*9558e6acSTreehugger Robot #include "ext.h"
45*9558e6acSTreehugger Robot 
46*9558e6acSTreehugger Robot int alwaysno;		/* assume "no" for all questions */
47*9558e6acSTreehugger Robot int alwaysyes;		/* assume "yes" for all questions */
48*9558e6acSTreehugger Robot int preen;		/* set when preening */
49*9558e6acSTreehugger Robot int rdonly;		/* device is opened read only (supersedes above) */
50*9558e6acSTreehugger Robot int skipclean;		/* skip clean file systems if preening */
51*9558e6acSTreehugger Robot int allow_mmap;		/* Allow the use of mmap(), if possible */
52*9558e6acSTreehugger Robot 
53*9558e6acSTreehugger Robot static void usage(void) __dead2;
54*9558e6acSTreehugger Robot 
55*9558e6acSTreehugger Robot static void
usage(void)56*9558e6acSTreehugger Robot usage(void)
57*9558e6acSTreehugger Robot {
58*9558e6acSTreehugger Robot 
59*9558e6acSTreehugger Robot 	fprintf(stderr, "%s\n%s\n",
60*9558e6acSTreehugger Robot 	    "usage: fsck_msdosfs -p [-f] filesystem ...",
61*9558e6acSTreehugger Robot 	    "       fsck_msdosfs [-ny] filesystem ...");
62*9558e6acSTreehugger Robot 	exit(1);
63*9558e6acSTreehugger Robot }
64*9558e6acSTreehugger Robot 
65*9558e6acSTreehugger Robot int
main(int argc,char ** argv)66*9558e6acSTreehugger Robot main(int argc, char **argv)
67*9558e6acSTreehugger Robot {
68*9558e6acSTreehugger Robot 	int ret = 0, erg;
69*9558e6acSTreehugger Robot 	int ch;
70*9558e6acSTreehugger Robot 
71*9558e6acSTreehugger Robot 	skipclean = 1;
72*9558e6acSTreehugger Robot 	allow_mmap = 1;
73*9558e6acSTreehugger Robot 	while ((ch = getopt(argc, argv, "CfFnpyM")) != -1) {
74*9558e6acSTreehugger Robot 		switch (ch) {
75*9558e6acSTreehugger Robot 		case 'C': /* for fsck_ffs compatibility */
76*9558e6acSTreehugger Robot 			break;
77*9558e6acSTreehugger Robot 		case 'f':
78*9558e6acSTreehugger Robot 			skipclean = 0;
79*9558e6acSTreehugger Robot 			break;
80*9558e6acSTreehugger Robot 		case 'F':
81*9558e6acSTreehugger Robot 			/*
82*9558e6acSTreehugger Robot 			 * We can never run in the background.  We must exit
83*9558e6acSTreehugger Robot 			 * silently with a nonzero exit code so that fsck(8)
84*9558e6acSTreehugger Robot 			 * can probe our support for -F.  The exit code
85*9558e6acSTreehugger Robot 			 * doesn't really matter, but we use an unusual one
86*9558e6acSTreehugger Robot 			 * in case someone tries -F directly.  The -F flag
87*9558e6acSTreehugger Robot 			 * is intentionally left out of the usage message.
88*9558e6acSTreehugger Robot 			 */
89*9558e6acSTreehugger Robot 			exit(5);
90*9558e6acSTreehugger Robot 		case 'n':
91*9558e6acSTreehugger Robot 			alwaysno = 1;
92*9558e6acSTreehugger Robot 			alwaysyes = 0;
93*9558e6acSTreehugger Robot 			break;
94*9558e6acSTreehugger Robot 		case 'y':
95*9558e6acSTreehugger Robot 			alwaysyes = 1;
96*9558e6acSTreehugger Robot 			alwaysno = 0;
97*9558e6acSTreehugger Robot 			break;
98*9558e6acSTreehugger Robot 
99*9558e6acSTreehugger Robot 		case 'p':
100*9558e6acSTreehugger Robot 			preen = 1;
101*9558e6acSTreehugger Robot 			break;
102*9558e6acSTreehugger Robot 
103*9558e6acSTreehugger Robot 		case 'M':
104*9558e6acSTreehugger Robot 			allow_mmap = 0;
105*9558e6acSTreehugger Robot 			break;
106*9558e6acSTreehugger Robot 
107*9558e6acSTreehugger Robot 		default:
108*9558e6acSTreehugger Robot 			usage();
109*9558e6acSTreehugger Robot 			break;
110*9558e6acSTreehugger Robot 		}
111*9558e6acSTreehugger Robot 	}
112*9558e6acSTreehugger Robot 	argc -= optind;
113*9558e6acSTreehugger Robot 	argv += optind;
114*9558e6acSTreehugger Robot 
115*9558e6acSTreehugger Robot 	if (!argc)
116*9558e6acSTreehugger Robot 		usage();
117*9558e6acSTreehugger Robot 
118*9558e6acSTreehugger Robot 	while (--argc >= 0) {
119*9558e6acSTreehugger Robot 		setcdevname(*argv, preen);
120*9558e6acSTreehugger Robot 		erg = checkfilesys(*argv++);
121*9558e6acSTreehugger Robot 		if (erg > ret)
122*9558e6acSTreehugger Robot 			ret = erg;
123*9558e6acSTreehugger Robot 	}
124*9558e6acSTreehugger Robot 
125*9558e6acSTreehugger Robot 	return ret;
126*9558e6acSTreehugger Robot }
127*9558e6acSTreehugger Robot 
128*9558e6acSTreehugger Robot 
129*9558e6acSTreehugger Robot /*VARARGS*/
130*9558e6acSTreehugger Robot int
ask(int def,const char * fmt,...)131*9558e6acSTreehugger Robot ask(int def, const char *fmt, ...)
132*9558e6acSTreehugger Robot {
133*9558e6acSTreehugger Robot 	va_list ap;
134*9558e6acSTreehugger Robot 
135*9558e6acSTreehugger Robot 	char prompt[256];
136*9558e6acSTreehugger Robot 	int c;
137*9558e6acSTreehugger Robot 
138*9558e6acSTreehugger Robot 	if (alwaysyes || alwaysno || rdonly)
139*9558e6acSTreehugger Robot 		def = (alwaysyes && !rdonly && !alwaysno);
140*9558e6acSTreehugger Robot 
141*9558e6acSTreehugger Robot 	if (preen) {
142*9558e6acSTreehugger Robot 		if (def)
143*9558e6acSTreehugger Robot 			printf("FIXED\n");
144*9558e6acSTreehugger Robot 		return def;
145*9558e6acSTreehugger Robot 	}
146*9558e6acSTreehugger Robot 
147*9558e6acSTreehugger Robot 	va_start(ap, fmt);
148*9558e6acSTreehugger Robot 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
149*9558e6acSTreehugger Robot 	va_end(ap);
150*9558e6acSTreehugger Robot 	if (alwaysyes || alwaysno || rdonly) {
151*9558e6acSTreehugger Robot 		printf("%s? %s\n", prompt, def ? "yes" : "no");
152*9558e6acSTreehugger Robot 		return def;
153*9558e6acSTreehugger Robot 	}
154*9558e6acSTreehugger Robot 	do {
155*9558e6acSTreehugger Robot 		printf("%s? [yn] ", prompt);
156*9558e6acSTreehugger Robot 		fflush(stdout);
157*9558e6acSTreehugger Robot 		c = getchar();
158*9558e6acSTreehugger Robot 		while (c != '\n' && getchar() != '\n')
159*9558e6acSTreehugger Robot 			if (feof(stdin))
160*9558e6acSTreehugger Robot 				return 0;
161*9558e6acSTreehugger Robot 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
162*9558e6acSTreehugger Robot 	return c == 'y' || c == 'Y';
163*9558e6acSTreehugger Robot }
164