xref: /netbsd-src/sbin/fsck_msdos/main.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: main.c,v 1.18 2007/03/10 00:30:36 hubertf 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.18 2007/03/10 00:30:36 hubertf Exp $");
39 #endif /* not lint */
40 
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <stdarg.h>
47 
48 #include "fsutil.h"
49 #include "ext.h"
50 
51 int alwaysno;		/* assume "no" for all questions */
52 int alwaysyes;		/* assume "yes" for all questions */
53 int preen;		/* set when preening */
54 int rdonly;		/* device is opened read only (supersedes above) */
55 
56 static void usage(void);
57 
58 static void
59 usage(void)
60 {
61 	errexit("usage: fsck_msdos [-fnpy] filesystem ... \n");
62 }
63 
64 int
65 main(int argc, char **argv)
66 {
67 	int ret = 0, erg;
68 	int ch;
69 
70 	while ((ch = getopt(argc, argv, "pPqynf")) != -1) {
71 		switch (ch) {
72 		case 'f':
73 			/*
74 			 * We are always forced, since we don't
75 			 * have a clean flag
76 			 */
77 			break;
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 		case 'P':		/* Progress meter not implemented. */
93 			break;
94 
95 		case 'q':		/* Quiet not implemented. */
96 			break;
97 
98 		default:
99 			usage();
100 			break;
101 		}
102 	}
103 	argc -= optind;
104 	argv += optind;
105 
106 	if (!argc)
107 		usage();
108 
109 	while (--argc >= 0) {
110 		setcdevname(*argv, preen);
111 		erg = checkfilesys(*argv++);
112 		if (erg > ret)
113 			ret = erg;
114 	}
115 
116 	return ret;
117 }
118 
119 
120 /*VARARGS*/
121 int
122 ask(int def, const char *fmt, ...)
123 {
124 	va_list ap;
125 
126 	char prompt[256];
127 	int c;
128 
129 	if (preen) {
130 		if (rdonly)
131 			def = 0;
132 		if (def)
133 			printf("FIXED\n");
134 		return def;
135 	}
136 
137 	va_start(ap, fmt);
138 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
139 	va_end(ap);
140 	if (alwaysyes || rdonly) {
141 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
142 		return !rdonly;
143 	}
144 	do {
145 		printf("%s? [yn] ", prompt);
146 		fflush(stdout);
147 		c = getchar();
148 		while (c != '\n' && getchar() != '\n')
149 			if (feof(stdin))
150 				return 0;
151 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
152 	return c == 'y' || c == 'Y';
153 }
154