xref: /openbsd/sbin/fsck_msdos/main.c (revision 9646ab25)
1 /*	$NetBSD: main.c,v 1.1 1996/05/14 17:39:36 ws Exp $	*/
2 
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Martin Husemann
18  *	and Wolfgang Solfrank.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 
36 #ifndef lint
37 static char rcsid[] = "$NetBSD: main.c,v 1.1 1996/05/14 17:39:36 ws Exp $";
38 #endif /* not lint */
39 
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #if __STDC__
47 #include <stdarg.h>
48 #else
49 #include <varargs.h>
50 #endif
51 
52 #include "ext.h"
53 
54 int alwaysno;		/* assume "no" for all questions */
55 int alwaysyes;		/* assume "yes" for all questions */
56 int preen;		/* set when preening */
57 int rdonly;		/* device is opened read only (supersedes above) */
58 
59 char *fname;		/* filesystem currently checked */
60 
61 static void
62 usage()
63 {
64 	errexit("Usage: fsck_msdos [-pny] filesystem ... \n");
65 }
66 
67 int
68 main(argc, argv)
69 	int argc;
70 	char **argv;
71 {
72 	extern int optind;
73 	int ret = 0, erg;
74 	int ch;
75 
76 	while ((ch = getopt(argc, argv, "vpyn")) != EOF) {
77 		switch (ch) {
78 		case 'n':
79 			alwaysno = 1;
80 			alwaysyes = preen = 0;
81 			break;
82 		case 'y':
83 			alwaysyes = 1;
84 			alwaysno = preen = 0;
85 			break;
86 
87 		case 'p':
88 			preen = 1;
89 			alwaysyes = alwaysno = 0;
90 			break;
91 
92 		default:
93 			usage();
94 			break;
95 		}
96 	}
97 	argc -= optind;
98 	argv += optind;
99 
100 	if (!argc)
101 		usage();
102 
103 	while (argc-- > 0) {
104 		erg = checkfilesys(fname = *argv++);
105 		if (erg > ret)
106 			ret = erg;
107 	}
108 	exit(ret);
109 }
110 
111 /*VARARGS*/
112 void
113 #if __STDC__
114 errexit(const char *fmt, ...)
115 #else
116 errexit(fmt, va_alist)
117 	char *fmt;
118 	va_dcl
119 #endif
120 {
121 	va_list ap;
122 
123 #if __STDC__
124 	va_start(ap, fmt);
125 #else
126 	va_start(ap);
127 #endif
128 	vprintf(fmt, ap);
129 	va_end(ap);
130 	exit(8);
131 }
132 
133 /*VARARGS*/
134 void
135 #if __STDC__
136 pfatal(const char *fmt, ...)
137 #else
138 pfatal(fmt, va_alist)
139 	char *fmt;
140 	va_dcl
141 #endif
142 {
143 	va_list ap;
144 
145 	if (preen)
146 		printf("%s: ", fname);
147 #if __STDC__
148 	va_start(ap, fmt);
149 #else
150 	va_start(ap);
151 #endif
152 	vprintf(fmt, ap);
153 	va_end(ap);
154 	printf("\n");
155 	if (preen)
156 		exit(8);
157 }
158 
159 /*VARARGS*/
160 void
161 #if __STDC__
162 pwarn(const char *fmt, ...)
163 #else
164 pwarn(fmt, va_alist)
165 	char *fmt;
166 	va_dcl
167 #endif
168 {
169 	va_list ap;
170 
171 	if (preen)
172 		printf("%s: ", fname);
173 #if __STDC__
174 	va_start(ap, fmt);
175 #else
176 	va_start(ap);
177 #endif
178 	vprintf(fmt, ap);
179 	va_end(ap);
180 }
181 
182 #if sun
183 char *
184 strerror(n)
185 	int n;
186 {
187 	extern int sys_nerr;
188 	extern char *sys_errlist[];
189 	static char alt[80];
190 
191 	if (n < sys_nerr)
192 		return sys_errlist[n];
193 	sprintf(alt, "Unknown error %d", n);
194 	return alt;
195 }
196 #endif
197 
198 void
199 perror(s)
200 	const char *s;
201 {
202 	pfatal("%s (%s)", s, strerror(errno));
203 }
204 
205 /*VARARGS*/
206 int
207 #if __STDC__
208 ask(int def, const char *fmt, ...)
209 #else
210 ask(def, fmt, va_alist)
211 	int def;
212 	char *fmt;
213 	va_dcl
214 #endif
215 {
216 	va_list ap;
217 
218 	char prompt[256];
219 	int c;
220 
221 	if (preen) {
222 		if (rdonly)
223 			def = 0;
224 		if (def)
225 			printf("FIXED\n");
226 		return def;
227 	}
228 
229 #if __STDC__
230 	va_start(ap, fmt);
231 #else
232 	va_start(ap);
233 #endif
234 #if sun
235 	vsprintf(prompt, fmt, ap);
236 #else
237 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
238 #endif
239 	if (alwaysyes || rdonly) {
240 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
241 		return !rdonly;
242 	}
243 	do {
244 		printf("%s? [yn] ", prompt);
245 		fflush(stdout);
246 		c = getchar();
247 		while (c != '\n' && getchar() != '\n')
248 			if (feof(stdin))
249 				return 0;
250 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
251 	return c == 'y' || c == 'Y';
252 }
253