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