Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:27:32 dillon 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 __P((void));
61 int main __P((int, char **));
62
63 static void
64 usage()
65 {
66         errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
67 }
68
69 int
70 main(argc, argv)
71         int argc;
72         char **argv;
73 {
74         int ret = 0, erg;
75         int ch;
76
77         while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
78                 switch (ch) {
79                 case 'f':
80                         /*
81                          * We are always forced, since we don't
82                          * have a clean flag
83                          */
84                         break;
85                 case 'F':
86                         /* We can never run in background */
87                         exit(5);
88                         break;
89                 case 'n':
90                         alwaysno = 1;
91                         alwaysyes = preen = 0;
92                         break;
93                 case 'y':
94                         alwaysyes = 1;
95                         alwaysno = preen = 0;
96                         break;
97
98                 case 'p':
99                         preen = 1;
100                         alwaysyes = alwaysno = 0;
101                         break;
102
103                 default:
104                         usage();
105                         break;
106                 }
107         }
108         argc -= optind;
109         argv += optind;
110
111         if (!argc)
112                 usage();
113
114         while (--argc >= 0) {
115                 setcdevname(*argv, preen);
116                 erg = checkfilesys(*argv++);
117                 if (erg > ret)
118                         ret = erg;
119         }
120
121         return ret;
122 }
123
124
125 /*VARARGS*/
126 int
127 #if __STDC__
128 ask(int def, const char *fmt, ...)
129 #else
130 ask(def, fmt, va_alist)
131         int def;
132         char *fmt;
133         va_dcl
134 #endif
135 {
136         va_list ap;
137
138         char prompt[256];
139         int c;
140
141         if (preen) {
142                 if (rdonly)
143                         def = 0;
144                 if (def)
145                         printf("FIXED\n");
146                 return def;
147         }
148
149 #if __STDC__
150         va_start(ap, fmt);
151 #else
152         va_start(ap);
153 #endif
154         vsnprintf(prompt, sizeof(prompt), fmt, ap);
155         if (alwaysyes || rdonly) {
156                 printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
157                 return !rdonly;
158         }
159         do {
160                 printf("%s? [yn] ", prompt);
161                 fflush(stdout);
162                 c = getchar();
163                 while (c != '\n' && getchar() != '\n')
164                         if (feof(stdin))
165                                 return 0;
166         } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
167         return c == 'y' || c == 'Y';
168 }