xref: /netbsd/sbin/fsck_msdos/main.c (revision bf9ec67e)
1 /*	$NetBSD: main.c,v 1.13 2002/05/25 23:45:13 wiz 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.13 2002/05/25 23:45:13 wiz 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 #include <stdarg.h>
48 
49 #include "fsutil.h"
50 #include "ext.h"
51 
52 int alwaysno;		/* assume "no" for all questions */
53 int alwaysyes;		/* assume "yes" for all questions */
54 int preen;		/* set when preening */
55 int rdonly;		/* device is opened read only (supersedes above) */
56 
57 static void usage __P((void));
58 int main __P((int, char **));
59 
60 static void
61 usage()
62 {
63 	errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
64 }
65 
66 int
67 main(argc, argv)
68 	int argc;
69 	char **argv;
70 {
71 	int ret = 0, erg;
72 	int ch;
73 
74 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
75 		switch (ch) {
76 		case 'f':
77 			/*
78 			 * We are always forced, since we don't
79 			 * have a clean flag
80 			 */
81 			break;
82 		case 'n':
83 			alwaysno = 1;
84 			alwaysyes = preen = 0;
85 			break;
86 		case 'y':
87 			alwaysyes = 1;
88 			alwaysno = preen = 0;
89 			break;
90 
91 		case 'p':
92 			preen = 1;
93 			alwaysyes = alwaysno = 0;
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 		setcdevname(*argv, preen);
109 		erg = checkfilesys(*argv++);
110 		if (erg > ret)
111 			ret = erg;
112 	}
113 
114 	return ret;
115 }
116 
117 
118 /*VARARGS*/
119 int
120 ask(int def, const char *fmt, ...)
121 {
122 	va_list ap;
123 
124 	char prompt[256];
125 	int c;
126 
127 	if (preen) {
128 		if (rdonly)
129 			def = 0;
130 		if (def)
131 			printf("FIXED\n");
132 		return def;
133 	}
134 
135 	va_start(ap, fmt);
136 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
137 	va_end(ap);
138 	if (alwaysyes || rdonly) {
139 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
140 		return !rdonly;
141 	}
142 	do {
143 		printf("%s? [yn] ", prompt);
144 		fflush(stdout);
145 		c = getchar();
146 		while (c != '\n' && getchar() != '\n')
147 			if (feof(stdin))
148 				return 0;
149 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
150 	return c == 'y' || c == 'Y';
151 }
152