xref: /freebsd/sbin/fsck_msdosfs/main.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 
41 #include "fsutil.h"
42 #include "ext.h"
43 
44 int alwaysno;		/* assume "no" for all questions */
45 int alwaysyes;		/* assume "yes" for all questions */
46 int preen;		/* set when preening */
47 int rdonly;		/* device is opened read only (supersedes above) */
48 int skipclean;		/* skip clean file systems if preening */
49 
50 static void usage(void) __dead2;
51 
52 static void
53 usage(void)
54 {
55 
56 	fprintf(stderr, "%s\n%s\n",
57 	    "usage: fsck_msdosfs -p [-f] filesystem ...",
58 	    "       fsck_msdosfs [-ny] filesystem ...");
59 	exit(1);
60 }
61 
62 int
63 main(int argc, char **argv)
64 {
65 	int ret = 0, erg;
66 	int ch;
67 
68 	skipclean = 1;
69 	while ((ch = getopt(argc, argv, "CfFnpy")) != -1) {
70 		switch (ch) {
71 		case 'C': /* for fsck_ffs compatibility */
72 			break;
73 		case 'f':
74 			skipclean = 0;
75 			break;
76 		case 'F':
77 			/*
78 			 * We can never run in the background.  We must exit
79 			 * silently with a nonzero exit code so that fsck(8)
80 			 * can probe our support for -F.  The exit code
81 			 * doesn't really matter, but we use an unusual one
82 			 * in case someone tries -F directly.  The -F flag
83 			 * is intentionally left out of the usage message.
84 			 */
85 			exit(5);
86 		case 'n':
87 			alwaysno = 1;
88 			alwaysyes = preen = 0;
89 			break;
90 		case 'y':
91 			alwaysyes = 1;
92 			alwaysno = preen = 0;
93 			break;
94 
95 		case 'p':
96 			preen = 1;
97 			alwaysyes = alwaysno = 0;
98 			break;
99 
100 		default:
101 			usage();
102 			break;
103 		}
104 	}
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (!argc)
109 		usage();
110 
111 	while (--argc >= 0) {
112 		setcdevname(*argv, preen);
113 		erg = checkfilesys(*argv++);
114 		if (erg > ret)
115 			ret = erg;
116 	}
117 
118 	return ret;
119 }
120 
121 
122 /*VARARGS*/
123 int
124 ask(int def, const char *fmt, ...)
125 {
126 	va_list ap;
127 
128 	char prompt[256];
129 	int c;
130 
131 	if (preen) {
132 		if (rdonly)
133 			def = 0;
134 		if (def)
135 			printf("FIXED\n");
136 		return def;
137 	}
138 
139 	va_start(ap, fmt);
140 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
141 	va_end(ap);
142 	if (alwaysyes || rdonly) {
143 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
144 		return !rdonly;
145 	}
146 	do {
147 		printf("%s? [yn] ", prompt);
148 		fflush(stdout);
149 		c = getchar();
150 		while (c != '\n' && getchar() != '\n')
151 			if (feof(stdin))
152 				return 0;
153 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
154 	return c == 'y' || c == 'Y';
155 }
156