xref: /freebsd/sbin/fsck_msdosfs/main.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*
2  * Copyright (C) 1995 Wolfgang Solfrank
3  * Copyright (c) 1995 Martin Husemann
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Martin Husemann
16  *	and Wolfgang Solfrank.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
37 static const char rcsid[] =
38   "$FreeBSD$";
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(void) __dead2;
58 
59 static void
60 usage()
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, "fFnpy")) != -1) {
72 		switch (ch) {
73 		case 'f':
74 			/*
75 			 * We are always forced, since we don't
76 			 * have a clean flag
77 			 */
78 			break;
79 		case 'F':
80 			/* We can never run in background */
81 			exit(5);
82 			break;
83 		case 'n':
84 			alwaysno = 1;
85 			alwaysyes = preen = 0;
86 			break;
87 		case 'y':
88 			alwaysyes = 1;
89 			alwaysno = preen = 0;
90 			break;
91 
92 		case 'p':
93 			preen = 1;
94 			alwaysyes = alwaysno = 0;
95 			break;
96 
97 		default:
98 			usage();
99 			break;
100 		}
101 	}
102 	argc -= optind;
103 	argv += optind;
104 
105 	if (!argc)
106 		usage();
107 
108 	while (--argc >= 0) {
109 		setcdevname(*argv, preen);
110 		erg = checkfilesys(*argv++);
111 		if (erg > ret)
112 			ret = erg;
113 	}
114 
115 	return ret;
116 }
117 
118 
119 /*VARARGS*/
120 int
121 ask(int def, const char *fmt, ...)
122 {
123 	va_list ap;
124 
125 	char prompt[256];
126 	int c;
127 
128 	if (preen) {
129 		if (rdonly)
130 			def = 0;
131 		if (def)
132 			printf("FIXED\n");
133 		return def;
134 	}
135 
136 	va_start(ap, fmt);
137 	vsnprintf(prompt, sizeof(prompt), fmt, 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