* Nuke ~771 __P()'s from our tree.
[dragonfly.git] / sbin / fsck_msdosfs / main.c
1 /*
2  * Copyright (C) 1995 Wolfgang Solfrank
3  * Copyright (c) 1995 Martin Husemann
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Martin Husemann
16  *      and Wolfgang Solfrank.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $
33  * $FreeBSD: src/sbin/fsck_msdosfs/main.c,v 1.4.2.1 2001/08/01 05:47:56 obrien Exp $
34  * $DragonFly: src/sbin/fsck_msdosfs/main.c,v 1.4 2003/11/01 17:15:58 drhodus Exp $
35  */
36
37
38 #include <sys/cdefs.h>
39
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #if __STDC__
47 #include <stdarg.h>
48 #else
49 #include <varargs.h>
50 #endif
51
52 #include "fsutil.h"
53 #include "ext.h"
54
55 int alwaysno;           /* assume "no" for all questions */
56 int alwaysyes;          /* assume "yes" for all questions */
57 int preen;              /* set when preening */
58 int rdonly;             /* device is opened read only (supersedes above) */
59
60 static void usage(void);
61 int main(int, char **);
62
63 static void
64 usage(void)
65 {
66         errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
67 }
68
69 int
70 main(int argc, char **argv)
71 {
72         int ret = 0, erg;
73         int ch;
74
75         while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
76                 switch (ch) {
77                 case 'f':
78                         /*
79                          * We are always forced, since we don't
80                          * have a clean flag
81                          */
82                         break;
83                 case 'F':
84                         /* We can never run in background */
85                         exit(5);
86                         break;
87                 case 'n':
88                         alwaysno = 1;
89                         alwaysyes = preen = 0;
90                         break;
91                 case 'y':
92                         alwaysyes = 1;
93                         alwaysno = preen = 0;
94                         break;
95
96                 case 'p':
97                         preen = 1;
98                         alwaysyes = alwaysno = 0;
99                         break;
100
101                 default:
102                         usage();
103                         break;
104                 }
105         }
106         argc -= optind;
107         argv += optind;
108
109         if (!argc)
110                 usage();
111
112         while (--argc >= 0) {
113                 setcdevname(*argv, preen);
114                 erg = checkfilesys(*argv++);
115                 if (erg > ret)
116                         ret = erg;
117         }
118
119         return ret;
120 }
121
122
123 /*VARARGS*/
124 int
125 ask(int def, const char *fmt, ...)
126 {
127         va_list ap;
128
129         char prompt[256];
130         int c;
131
132         if (preen) {
133                 if (rdonly)
134                         def = 0;
135                 if (def)
136                         printf("FIXED\n");
137                 return def;
138         }
139
140         va_start(ap, fmt);
141         vsnprintf(prompt, sizeof(prompt), fmt, ap);
142         if (alwaysyes || rdonly) {
143                 printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
144                 return !rdonly;
145         }
146         do {
147                 printf("%s? [yn] ", prompt);
148                 fflush(stdout);
149                 c = getchar();
150                 while (c != '\n' && getchar() != '\n')
151                         if (feof(stdin))
152                                 return 0;
153         } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
154         return c == 'y' || c == 'Y';
155 }