xref: /netbsd-src/sbin/fsck_msdos/main.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: main.c,v 1.11 1997/10/17 11:20:03 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: main.c,v 1.11 1997/10/17 11:20:03 ws 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, "pynf")) != -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 'n':
87 			alwaysno = 1;
88 			alwaysyes = preen = 0;
89 			break;
90 		case 'y':
91 			alwaysyes = 1;
92 			alwaysno = preen = 0;
93 			break;
94 
95 		case 'p':
96 			preen = 1;
97 			alwaysyes = alwaysno = 0;
98 			break;
99 
100 		default:
101 			usage();
102 			break;
103 		}
104 	}
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (!argc)
109 		usage();
110 
111 	while (--argc >= 0) {
112 		setcdevname(*argv, preen);
113 		erg = checkfilesys(*argv++);
114 		if (erg > ret)
115 			ret = erg;
116 	}
117 
118 	return ret;
119 }
120 
121 
122 /*VARARGS*/
123 int
124 #if __STDC__
125 ask(int def, const char *fmt, ...)
126 #else
127 ask(def, fmt, va_alist)
128 	int def;
129 	char *fmt;
130 	va_dcl
131 #endif
132 {
133 	va_list ap;
134 
135 	char prompt[256];
136 	int c;
137 
138 	if (preen) {
139 		if (rdonly)
140 			def = 0;
141 		if (def)
142 			printf("FIXED\n");
143 		return def;
144 	}
145 
146 #if __STDC__
147 	va_start(ap, fmt);
148 #else
149 	va_start(ap);
150 #endif
151 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
152 	if (alwaysyes || rdonly) {
153 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
154 		return !rdonly;
155 	}
156 	do {
157 		printf("%s? [yn] ", prompt);
158 		fflush(stdout);
159 		c = getchar();
160 		while (c != '\n' && getchar() != '\n')
161 			if (feof(stdin))
162 				return 0;
163 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
164 	return c == 'y' || c == 'Y';
165 }
166