xref: /openbsd/sbin/fsck_msdos/main.c (revision 7b91b0e0)
1 /*	$OpenBSD: main.c,v 1.3 1996/06/23 14:30:45 deraadt 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.3 1996/06/23 14:30:45 deraadt 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, "vpyn")) != EOF) {
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 		default:
94 			usage();
95 			break;
96 		}
97 	}
98 	argc -= optind;
99 	argv += optind;
100 
101 	if (!argc)
102 		usage();
103 
104 	while (argc-- > 0) {
105 		erg = checkfilesys(fname = *argv++);
106 		if (erg > ret)
107 			ret = erg;
108 	}
109 	exit(ret);
110 }
111 
112 /*VARARGS*/
113 void
114 #if __STDC__
115 errexit(const char *fmt, ...)
116 #else
117 errexit(fmt, va_alist)
118 	char *fmt;
119 	va_dcl
120 #endif
121 {
122 	va_list ap;
123 
124 #if __STDC__
125 	va_start(ap, fmt);
126 #else
127 	va_start(ap);
128 #endif
129 	vprintf(fmt, ap);
130 	va_end(ap);
131 	exit(8);
132 }
133 
134 /*VARARGS*/
135 void
136 #if __STDC__
137 pfatal(const char *fmt, ...)
138 #else
139 pfatal(fmt, va_alist)
140 	char *fmt;
141 	va_dcl
142 #endif
143 {
144 	va_list ap;
145 
146 	if (preen)
147 		printf("%s: ", fname);
148 #if __STDC__
149 	va_start(ap, fmt);
150 #else
151 	va_start(ap);
152 #endif
153 	vprintf(fmt, ap);
154 	va_end(ap);
155 	printf("\n");
156 	if (preen)
157 		exit(8);
158 }
159 
160 /*VARARGS*/
161 void
162 #if __STDC__
163 pwarn(const char *fmt, ...)
164 #else
165 pwarn(fmt, va_alist)
166 	char *fmt;
167 	va_dcl
168 #endif
169 {
170 	va_list ap;
171 
172 	if (preen)
173 		printf("%s: ", fname);
174 #if __STDC__
175 	va_start(ap, fmt);
176 #else
177 	va_start(ap);
178 #endif
179 	vprintf(fmt, ap);
180 	va_end(ap);
181 }
182 
183 void
184 perror(s)
185 	const char *s;
186 {
187 	pfatal("%s (%s)", s, strerror(errno));
188 }
189 
190 /*VARARGS*/
191 int
192 #if __STDC__
193 ask(int def, const char *fmt, ...)
194 #else
195 ask(def, fmt, va_alist)
196 	int def;
197 	char *fmt;
198 	va_dcl
199 #endif
200 {
201 	va_list ap;
202 
203 	char prompt[256];
204 	int c;
205 
206 	if (preen) {
207 		if (rdonly)
208 			def = 0;
209 		if (def)
210 			printf("FIXED\n");
211 		return def;
212 	}
213 
214 #if __STDC__
215 	va_start(ap, fmt);
216 #else
217 	va_start(ap);
218 #endif
219 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
220 	if (alwaysyes || rdonly) {
221 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
222 		return !rdonly;
223 	}
224 	do {
225 		printf("%s? [yn] ", prompt);
226 		fflush(stdout);
227 		c = getchar();
228 		while (c != '\n' && getchar() != '\n')
229 			if (feof(stdin))
230 				return 0;
231 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
232 	return c == 'y' || c == 'Y';
233 }
234