xref: /openbsd/sbin/fsck_msdos/main.c (revision e384f6ef)
1 /*	$OpenBSD: main.c,v 1.22 2015/11/23 19:19:30 deraadt 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 	if (pledge("stdio rpath wpath disklabel", NULL) == -1)
61 		err(1, "pledge");
62 
63 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
64 		switch (ch) {
65 		case 'f':
66 			/* Ignore for consistency with fsck_ffs */
67 			break;
68 
69 		case 'n':
70 			alwaysno = 1;
71 			alwaysyes = preen = 0;
72 			break;
73 
74 		case 'y':
75 			alwaysyes = 1;
76 			alwaysno = preen = 0;
77 			break;
78 
79 		case 'p':
80 			preen = 1;
81 			alwaysyes = alwaysno = 0;
82 			break;
83 
84 		default:
85 			usage();
86 			break;
87 		}
88 	}
89 	argc -= optind;
90 	argv += optind;
91 
92 	if (argc != 1)
93 		usage();
94 
95 	setcdevname(*argv, NULL, preen);
96 	exit (checkfilesys(blockcheck(*argv)));
97 }
98 
99 int
100 ask(int def, const char *fmt, ...)
101 {
102 	va_list ap;
103 
104 	char prompt[256];
105 	int c;
106 
107 	if (preen) {
108 		if (rdonly)
109 			def = 0;
110 		if (def)
111 			printf("FIXED\n");
112 		return (def);
113 	}
114 
115 	va_start(ap, fmt);
116 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
117 	va_end(ap);
118 	if (alwaysyes || rdonly) {
119 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
120 		return (!rdonly);
121 	}
122 	do {
123 		printf("%s? [Fyn] ", prompt);
124 		fflush(stdout);
125 		c = getchar();
126 		if (c == 'F') {
127 			alwaysyes = 1;
128 			return (1);
129 		}
130 		while (c != '\n' && getchar() != '\n')
131 			if (feof(stdin))
132 				return (0);
133 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
134 	return (c == 'y' || c == 'Y');
135 }
136