]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/fsck_msdosfs/main.c
MFC r203871:
[FreeBSD/stable/8.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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <stdarg.h>
41
42 #include "fsutil.h"
43 #include "ext.h"
44
45 int alwaysno;           /* assume "no" for all questions */
46 int alwaysyes;          /* assume "yes" for all questions */
47 int preen;              /* set when preening */
48 int rdonly;             /* device is opened read only (supersedes above) */
49 int skipclean;          /* skip clean file systems if preening */
50
51 static void usage(void) __dead2;
52
53 static void
54 usage(void)
55 {
56
57         fprintf(stderr, "%s\n%s\n",
58             "usage: fsck_msdosfs -p [-f] filesystem ...",
59             "       fsck_msdosfs [-ny] filesystem ...");
60         exit(1);
61 }
62
63 int
64 main(int argc, char **argv)
65 {
66         int ret = 0, erg;
67         int ch;
68
69         skipclean = 1;
70         while ((ch = getopt(argc, argv, "CfFnpy")) != -1) {
71                 switch (ch) {
72                 case 'C': /* for fsck_ffs compatibility */
73                         break;
74                 case 'f':
75                         skipclean = 0;
76                         break;
77                 case 'F':
78                         /*
79                          * We can never run in the background.  We must exit
80                          * silently with a nonzero exit code so that fsck(8)
81                          * can probe our support for -F.  The exit code
82                          * doesn't really matter, but we use an unusual one
83                          * in case someone tries -F directly.  The -F flag
84                          * is intentionally left out of the usage message.
85                          */
86                         exit(5);
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 }