xref: /openbsd/sbin/fsck_msdos/main.c (revision 24766c54)
1 /*	$OpenBSD: main.c,v 1.10 2001/09/05 22:32:37 deraadt Exp $	*/
2 /*	$NetBSD: main.c,v 1.8 1996/10/17 20:29:53 cgd 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.10 2001/09/05 22:32:37 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 #ifdef __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 static void usage __P((void));
61 int main __P((int, char **));
62 
63 static void
64 usage()
65 {
66 	errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
67 }
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char **argv;
73 {
74 	int ret = 0, erg;
75 	int ch;
76 
77 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
78 		switch (ch) {
79 		case 'f':
80 			/* Ignore for consistency with fsck_ffs */
81 			break;
82 
83 		case 'n':
84 			alwaysno = 1;
85 			alwaysyes = preen = 0;
86 			break;
87 
88 		case 'y':
89 			alwaysyes = 1;
90 			alwaysno = preen = 0;
91 			break;
92 
93 		case 'p':
94 			preen = 1;
95 			alwaysyes = alwaysno = 0;
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 	exit(ret);
116 }
117 
118 /*VARARGS*/
119 int
120 #ifdef __STDC__
121 ask(int def, const char *fmt, ...)
122 #else
123 ask(def, fmt, va_alist)
124 	int def;
125 	char *fmt;
126 	va_dcl
127 #endif
128 {
129 	va_list ap;
130 
131 	char prompt[256];
132 	int c;
133 
134 	if (preen) {
135 		if (rdonly)
136 			def = 0;
137 		if (def)
138 			printf("FIXED\n");
139 		return (def);
140 	}
141 
142 #ifdef __STDC__
143 	va_start(ap, fmt);
144 #else
145 	va_start(ap);
146 #endif
147 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
148 	va_end(ap);
149 	if (alwaysyes || rdonly) {
150 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
151 		return (!rdonly);
152 	}
153 	do {
154 		printf("%s? [yn] ", prompt);
155 		fflush(stdout);
156 		c = getchar();
157 		while (c != '\n' && getchar() != '\n')
158 			if (feof(stdin))
159 				return (0);
160 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
161 	return (c == 'y' || c == 'Y');
162 }
163