xref: /netbsd-src/sbin/fsck_msdos/main.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: main.c,v 1.21 2008/06/13 20:46:09 martin Exp $	*/
2 
3 /*
4  * Copyright (C) 1995 Wolfgang Solfrank
5  * Copyright (c) 1995 Martin Husemann
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: main.c,v 1.21 2008/06/13 20:46:09 martin Exp $");
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 #include <signal.h>
41 
42 #include "fsutil.h"
43 #include "ext.h"
44 #include "exitvalues.h"
45 
46 int alwaysno;		/* assume "no" for all questions */
47 int alwaysyes;		/* assume "yes" for all questions */
48 int preen;		/* set when preening */
49 int rdonly;		/* device is opened read only (supersedes above) */
50 
51 static void usage(void) __dead;
52 
53 static void
54 usage(void)
55 {
56     	(void)fprintf(stderr, "Usage: %s [-fnpy] filesystem ... \n",
57 	    getprogname());
58 	exit(FSCK_EXIT_USAGE);
59 }
60 
61 static void
62 catch(int n)
63 {
64 	exit(FSCK_EXIT_SIGNALLED);
65 }
66 
67 int
68 main(int argc, char **argv)
69 {
70 	int ret = FSCK_EXIT_OK, erg;
71 	int ch;
72 
73 	while ((ch = getopt(argc, argv, "pPqynf")) != -1) {
74 		switch (ch) {
75 		case 'f':
76 			/*
77 			 * We are always forced, since we don't
78 			 * have a clean flag
79 			 */
80 			break;
81 		case 'n':
82 			alwaysno = 1;
83 			alwaysyes = preen = 0;
84 			break;
85 		case 'y':
86 			alwaysyes = 1;
87 			alwaysno = preen = 0;
88 			break;
89 
90 		case 'p':
91 			preen = 1;
92 			alwaysyes = alwaysno = 0;
93 			break;
94 
95 		case 'P':		/* Progress meter not implemented. */
96 			break;
97 
98 		case 'q':		/* Quiet not implemented. */
99 			break;
100 
101 		default:
102 			usage();
103 			break;
104 		}
105 	}
106 	argc -= optind;
107 	argv += optind;
108 
109 	if (!argc)
110 		usage();
111 
112 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
113 		(void) signal(SIGINT, catch);
114 	if (preen)
115 		(void) signal(SIGQUIT, catch);
116 
117 	while (--argc >= 0) {
118 		setcdevname(*argv, preen);
119 		erg = checkfilesys(*argv++);
120 		if (erg > ret)
121 			ret = erg;
122 	}
123 
124 	return ret;
125 }
126 
127 
128 /*VARARGS*/
129 int
130 ask(int def, const char *fmt, ...)
131 {
132 	va_list ap;
133 
134 	char prompt[256];
135 	int c;
136 
137 	if (preen) {
138 		if (rdonly)
139 			def = 0;
140 		if (def)
141 			printf("FIXED\n");
142 		return def;
143 	}
144 
145 	va_start(ap, fmt);
146 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
147 	va_end(ap);
148 	if (alwaysyes || rdonly) {
149 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
150 		return !rdonly;
151 	}
152 	do {
153 		printf("%s? [yn] ", prompt);
154 		fflush(stdout);
155 		c = getchar();
156 		while (c != '\n' && getchar() != '\n')
157 			if (feof(stdin))
158 				return 0;
159 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
160 	return c == 'y' || c == 'Y';
161 }
162