xref: /dragonfly/sbin/fsck_msdosfs/main.c (revision 984263bc)
1 /*
2  * Copyright (C) 1995 Wolfgang Solfrank
3  * Copyright (c) 1995 Martin Husemann
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Martin Husemann
16  *	and Wolfgang Solfrank.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
37 static const char rcsid[] =
38   "$FreeBSD: src/sbin/fsck_msdosfs/main.c,v 1.4.2.1 2001/08/01 05:47:56 obrien Exp $";
39 #endif /* not lint */
40 
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #if __STDC__
48 #include <stdarg.h>
49 #else
50 #include <varargs.h>
51 #endif
52 
53 #include "fsutil.h"
54 #include "ext.h"
55 
56 int alwaysno;		/* assume "no" for all questions */
57 int alwaysyes;		/* assume "yes" for all questions */
58 int preen;		/* set when preening */
59 int rdonly;		/* device is opened read only (supersedes above) */
60 
61 static void usage __P((void));
62 int main __P((int, char **));
63 
64 static void
65 usage()
66 {
67 	errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
68 }
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char **argv;
74 {
75 	int ret = 0, erg;
76 	int ch;
77 
78 	while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
79 		switch (ch) {
80 		case 'f':
81 			/*
82 			 * We are always forced, since we don't
83 			 * have a clean flag
84 			 */
85 			break;
86 		case 'F':
87 			/* We can never run in background */
88 			exit(5);
89 			break;
90 		case 'n':
91 			alwaysno = 1;
92 			alwaysyes = preen = 0;
93 			break;
94 		case 'y':
95 			alwaysyes = 1;
96 			alwaysno = preen = 0;
97 			break;
98 
99 		case 'p':
100 			preen = 1;
101 			alwaysyes = alwaysno = 0;
102 			break;
103 
104 		default:
105 			usage();
106 			break;
107 		}
108 	}
109 	argc -= optind;
110 	argv += optind;
111 
112 	if (!argc)
113 		usage();
114 
115 	while (--argc >= 0) {
116 		setcdevname(*argv, preen);
117 		erg = checkfilesys(*argv++);
118 		if (erg > ret)
119 			ret = erg;
120 	}
121 
122 	return ret;
123 }
124 
125 
126 /*VARARGS*/
127 int
128 #if __STDC__
129 ask(int def, const char *fmt, ...)
130 #else
131 ask(def, fmt, va_alist)
132 	int def;
133 	char *fmt;
134 	va_dcl
135 #endif
136 {
137 	va_list ap;
138 
139 	char prompt[256];
140 	int c;
141 
142 	if (preen) {
143 		if (rdonly)
144 			def = 0;
145 		if (def)
146 			printf("FIXED\n");
147 		return def;
148 	}
149 
150 #if __STDC__
151 	va_start(ap, fmt);
152 #else
153 	va_start(ap);
154 #endif
155 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
156 	if (alwaysyes || rdonly) {
157 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
158 		return !rdonly;
159 	}
160 	do {
161 		printf("%s? [yn] ", prompt);
162 		fflush(stdout);
163 		c = getchar();
164 		while (c != '\n' && getchar() != '\n')
165 			if (feof(stdin))
166 				return 0;
167 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
168 	return c == 'y' || c == 'Y';
169 }
170