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 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <stdarg.h>
34
35 #include "fsutil.h"
36 #include "ext.h"
37
38 int alwaysno; /* assume "no" for all questions */
39 int alwaysyes; /* assume "yes" for all questions */
40 int preen; /* set when preening */
41 int rdonly; /* device is opened read only (supersedes above) */
42 int allow_mmap; /* Allow the use of mmap(), if possible */
43
44 static void usage(void) __dead2;
45
46 static void
usage(void)47 usage(void)
48 {
49 errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
50 }
51
52 int
main(int argc,char ** argv)53 main(int argc, char **argv)
54 {
55 int ret = 0, erg;
56 int ch;
57
58 allow_mmap = 1;
59 while ((ch = getopt(argc, argv, "CfFnpyM")) != -1) {
60 switch (ch) {
61 case 'f':
62 /*
63 * We are always forced, since we don't
64 * have a clean flag
65 */
66 break;
67 case 'F':
68 /*
69 * We can never run in the background. We must exit
70 * silently with a nonzero exit code so that fsck(8)
71 * can probe our support for -F. The exit code
72 * doesn't really matter, but we use an unusual one
73 * in case someone tries -F directly. The -F flag
74 * is intentionally left out of the usage message.
75 */
76 exit(5);
77 case 'n':
78 alwaysno = 1;
79 alwaysyes = 0;
80 break;
81 case 'y':
82 alwaysyes = 1;
83 alwaysno = 0;
84 break;
85
86 case 'p':
87 preen = 1;
88 break;
89
90 case 'M':
91 allow_mmap = 0;
92 break;
93
94 default:
95 usage();
96 break;
97 }
98 }
99 argc -= optind;
100 argv += optind;
101
102 if (!argc)
103 usage();
104
105 while (--argc >= 0) {
106 setcdevname(*argv, preen);
107 erg = checkfilesys(*argv++);
108 if (erg > ret)
109 ret = erg;
110 }
111
112 return ret;
113 }
114
115
116 /*VARARGS*/
117 int
ask(int def,const char * fmt,...)118 ask(int def, const char *fmt, ...)
119 {
120 va_list ap;
121
122 char prompt[256];
123 int c;
124
125 if (alwaysyes || alwaysno || rdonly)
126 def = (alwaysyes && !rdonly && !alwaysno);
127
128 if (preen) {
129 if (def)
130 printf("FIXED\n");
131 return def;
132 }
133
134 va_start(ap, fmt);
135 vsnprintf(prompt, sizeof(prompt), fmt, ap);
136 va_end(ap);
137 if (alwaysyes || alwaysno || rdonly) {
138 printf("%s? %s\n", prompt, def ? "yes" : "no");
139 return def;
140 }
141 do {
142 printf("%s? [yn] ", prompt);
143 fflush(stdout);
144 c = getchar();
145 while (c != '\n' && getchar() != '\n')
146 if (feof(stdin))
147 return 0;
148 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
149 return c == 'y' || c == 'Y';
150 }
151