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