xref: /dflybsd-src/sbin/fsck_msdosfs/main.c (revision 07fe4dca207b83850d5b017501d917200afff416)
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  * $NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $
33  * $FreeBSD: src/sbin/fsck_msdosfs/main.c,v 1.4.2.1 2001/08/01 05:47:56 obrien Exp $
34  */
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <stdarg.h>
43 
44 #include "fsutil.h"
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 
54 static void
55 usage(void)
56 {
57 	errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
58 }
59 
60 int
61 main(int argc, char **argv)
62 {
63 	int ret = 0, erg;
64 	int ch;
65 
66 	while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
67 		switch (ch) {
68 		case 'f':
69 			/*
70 			 * We are always forced, since we don't
71 			 * have a clean flag
72 			 */
73 			break;
74 		case 'F':
75 			/*
76 			 * We can never run in the background.  We must exit
77 			 * silently with a nonzero exit code so that fsck(8)
78 			 * can probe our support for -F.  The exit code
79 			 * doesn't really matter, but we use an unusual one
80 			 * in case someone tries -F directly.  The -F flag
81 			 * is intentionally left out of the usage message.
82 			 */
83 			exit(5);
84 			break;
85 		case 'n':
86 			alwaysno = 1;
87 			alwaysyes = preen = 0;
88 			break;
89 		case 'y':
90 			alwaysyes = 1;
91 			alwaysno = preen = 0;
92 			break;
93 
94 		case 'p':
95 			preen = 1;
96 			alwaysyes = alwaysno = 0;
97 			break;
98 
99 		default:
100 			usage();
101 			break;
102 		}
103 	}
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (!argc)
108 		usage();
109 
110 	while (--argc >= 0) {
111 		setcdevname(*argv, preen);
112 		erg = checkfilesys(*argv++);
113 		if (erg > ret)
114 			ret = erg;
115 	}
116 
117 	return ret;
118 }
119 
120 
121 /*VARARGS*/
122 int
123 ask(int def, const char *fmt, ...)
124 {
125 	va_list ap;
126 
127 	char prompt[256];
128 	int c;
129 
130 	if (preen) {
131 		if (rdonly)
132 			def = 0;
133 		if (def)
134 			printf("FIXED\n");
135 		return def;
136 	}
137 
138 	va_start(ap, fmt);
139 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
140 	if (alwaysyes || rdonly) {
141 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
142 		return !rdonly;
143 	}
144 	do {
145 		printf("%s? [yn] ", prompt);
146 		fflush(stdout);
147 		c = getchar();
148 		while (c != '\n' && getchar() != '\n')
149 			if (feof(stdin))
150 				return 0;
151 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
152 	return c == 'y' || c == 'Y';
153 }
154