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