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