xref: /openbsd/sbin/fsck_msdos/main.c (revision 76953227)
1 /*	$OpenBSD: main.c,v 1.23 2016/05/28 18:00:42 tb 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 #include <err.h>
38 
39 #include "ext.h"
40 
41 int alwaysno;		/* assume "no" for all questions */
42 int alwaysyes;		/* assume "yes" for all questions */
43 int preen;		/* set when preening */
44 int rdonly;		/* device is opened read only (supersedes above) */
45 
46 static void usage(void);
47 int main(int, char **);
48 
49 static void
50 usage(void)
51 {
52 	errexit("usage: fsck_msdos [-fnpy] filesystem\n");
53 }
54 
55 int
56 main(int argc, char *argv[])
57 {
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 != 1)
90 		usage();
91 
92 	setcdevname(*argv, NULL, preen);
93 	exit (checkfilesys(blockcheck(*argv)));
94 }
95 
96 int
97 ask(int def, const char *fmt, ...)
98 {
99 	va_list ap;
100 
101 	char prompt[256];
102 	int c;
103 
104 	if (preen) {
105 		if (rdonly)
106 			def = 0;
107 		if (def)
108 			printf("FIXED\n");
109 		return (def);
110 	}
111 
112 	va_start(ap, fmt);
113 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
114 	va_end(ap);
115 	if (alwaysyes || rdonly) {
116 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
117 		return (!rdonly);
118 	}
119 	do {
120 		printf("%s? [Fyn] ", prompt);
121 		fflush(stdout);
122 		c = getchar();
123 		if (c == 'F') {
124 			alwaysyes = 1;
125 			return (1);
126 		}
127 		while (c != '\n' && getchar() != '\n')
128 			if (feof(stdin))
129 				return (0);
130 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
131 	return (c == 'y' || c == 'Y');
132 }
133