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