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