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