xref: /openbsd/sbin/fsck_msdos/main.c (revision f36f6633)
1 /*	$OpenBSD: main.c,v 1.14 2005/02/03 05:03:50 jaredy 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.14 2005/02/03 05:03:50 jaredy 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 "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 int main(int, char **);
58 
59 static void
60 usage(void)
61 {
62 	errexit("usage: fsck_msdos [-fnpy] filesystem ...\n");
63 }
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	int ret = 0, erg;
69 	int ch;
70 
71 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
72 		switch (ch) {
73 		case 'f':
74 			/* Ignore for consistency with fsck_ffs */
75 			break;
76 
77 		case 'n':
78 			alwaysno = 1;
79 			alwaysyes = preen = 0;
80 			break;
81 
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 		default:
93 			usage();
94 			break;
95 		}
96 	}
97 	argc -= optind;
98 	argv += optind;
99 
100 	if (!argc)
101 		usage();
102 
103 	while (argc-- > 0) {
104 		setcdevname(*argv, preen);
105 		erg = checkfilesys(*argv++);
106 		if (erg > ret)
107 			ret = erg;
108 	}
109 	exit(ret);
110 }
111 
112 /*VARARGS*/
113 int
114 ask(int def, const char *fmt, ...)
115 {
116 	va_list ap;
117 
118 	char prompt[256];
119 	int c;
120 
121 	if (preen) {
122 		if (rdonly)
123 			def = 0;
124 		if (def)
125 			printf("FIXED\n");
126 		return (def);
127 	}
128 
129 	va_start(ap, fmt);
130 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
131 	va_end(ap);
132 	if (alwaysyes || rdonly) {
133 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
134 		return (!rdonly);
135 	}
136 	do {
137 		printf("%s? [yn] ", prompt);
138 		fflush(stdout);
139 		c = getchar();
140 		while (c != '\n' && getchar() != '\n')
141 			if (feof(stdin))
142 				return (0);
143 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
144 	return (c == 'y' || c == 'Y');
145 }
146