xref: /openbsd/sbin/fsck_msdos/main.c (revision 14e30480)
1 /*	$OpenBSD: main.c,v 1.19 2014/06/16 18:33:33 tobias 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 
38 #include "ext.h"
39 
40 int alwaysno;		/* assume "no" for all questions */
41 int alwaysyes;		/* assume "yes" for all questions */
42 int preen;		/* set when preening */
43 int rdonly;		/* device is opened read only (supersedes above) */
44 
45 static void usage(void);
46 int main(int, char **);
47 
48 static void
49 usage(void)
50 {
51 	errexit("usage: fsck_msdos [-fnpy] filesystem ...\n");
52 }
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	int ret = 0, erg;
58 	int ch;
59 
60 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
61 		switch (ch) {
62 		case 'f':
63 			/* Ignore for consistency with fsck_ffs */
64 			break;
65 
66 		case 'n':
67 			alwaysno = 1;
68 			alwaysyes = preen = 0;
69 			break;
70 
71 		case 'y':
72 			alwaysyes = 1;
73 			alwaysno = preen = 0;
74 			break;
75 
76 		case 'p':
77 			preen = 1;
78 			alwaysyes = alwaysno = 0;
79 			break;
80 
81 		default:
82 			usage();
83 			break;
84 		}
85 	}
86 	argc -= optind;
87 	argv += optind;
88 
89 	if (!argc)
90 		usage();
91 
92 	while (argc-- > 0) {
93 		setcdevname(*argv, NULL, preen);
94 		erg = checkfilesys(blockcheck(*argv++));
95 		if (erg > ret)
96 			ret = erg;
97 	}
98 	exit(ret);
99 }
100 
101 /*VARARGS*/
102 int
103 ask(int def, const char *fmt, ...)
104 {
105 	va_list ap;
106 
107 	char prompt[256];
108 	int c;
109 
110 	if (preen) {
111 		if (rdonly)
112 			def = 0;
113 		if (def)
114 			printf("FIXED\n");
115 		return (def);
116 	}
117 
118 	va_start(ap, fmt);
119 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
120 	va_end(ap);
121 	if (alwaysyes || rdonly) {
122 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
123 		return (!rdonly);
124 	}
125 	do {
126 		printf("%s? [Fyn] ", prompt);
127 		fflush(stdout);
128 		c = getchar();
129 		if (c == 'F') {
130 			alwaysyes = 1;
131 			return (1);
132 		}
133 		while (c != '\n' && getchar() != '\n')
134 			if (feof(stdin))
135 				return (0);
136 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
137 	return (c == 'y' || c == 'Y');
138 }
139