xref: /openbsd/sbin/fsck_msdos/main.c (revision 98fba8a9)
1 /*	$OpenBSD: main.c,v 1.18 2012/03/31 17:53:34 krw 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 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <stdarg.h>
44 
45 #include "ext.h"
46 
47 int alwaysno;		/* assume "no" for all questions */
48 int alwaysyes;		/* assume "yes" for all questions */
49 int preen;		/* set when preening */
50 int rdonly;		/* device is opened read only (supersedes above) */
51 
52 static void usage(void);
53 int main(int, char **);
54 
55 static void
56 usage(void)
57 {
58 	errexit("usage: fsck_msdos [-fnpy] filesystem ...\n");
59 }
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	int ret = 0, erg;
65 	int ch;
66 
67 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
68 		switch (ch) {
69 		case 'f':
70 			/* Ignore for consistency with fsck_ffs */
71 			break;
72 
73 		case 'n':
74 			alwaysno = 1;
75 			alwaysyes = preen = 0;
76 			break;
77 
78 		case 'y':
79 			alwaysyes = 1;
80 			alwaysno = preen = 0;
81 			break;
82 
83 		case 'p':
84 			preen = 1;
85 			alwaysyes = alwaysno = 0;
86 			break;
87 
88 		default:
89 			usage();
90 			break;
91 		}
92 	}
93 	argc -= optind;
94 	argv += optind;
95 
96 	if (!argc)
97 		usage();
98 
99 	while (argc-- > 0) {
100 		setcdevname(*argv, NULL, preen);
101 		erg = checkfilesys(blockcheck(*argv++));
102 		if (erg > ret)
103 			ret = erg;
104 	}
105 	exit(ret);
106 }
107 
108 /*VARARGS*/
109 int
110 ask(int def, const char *fmt, ...)
111 {
112 	va_list ap;
113 
114 	char prompt[256];
115 	int c;
116 
117 	if (preen) {
118 		if (rdonly)
119 			def = 0;
120 		if (def)
121 			printf("FIXED\n");
122 		return (def);
123 	}
124 
125 	va_start(ap, fmt);
126 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
127 	va_end(ap);
128 	if (alwaysyes || rdonly) {
129 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
130 		return (!rdonly);
131 	}
132 	do {
133 		printf("%s? [Fyn] ", prompt);
134 		fflush(stdout);
135 		c = getchar();
136 		if (c == 'F') {
137 			alwaysyes = 1;
138 			return (1);
139 		}
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