blob: 1c6d514da3e953ec7b7a21f53984cec538539bd5 [file] [log] [blame]
San Mehat58d4c6c2009-06-25 08:08:05 -07001/*
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
33
34#include <sys/cdefs.h>
35#ifndef lint
36__RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
37static const char rcsid[] =
38 "$FreeBSD: src/sbin/fsck_msdosfs/main.c,v 1.16 2009/06/10 19:02:54 avg Exp $";
39#endif /* not lint */
40
41#include <stdlib.h>
42#include <string.h>
43#include <ctype.h>
44#include <stdio.h>
45#include <unistd.h>
46#include <errno.h>
47#include <stdarg.h>
48
49#include "fsutil.h"
50#include "ext.h"
jianfeng594baf12012-06-19 19:53:15 +080051#include "fatcache.h"
San Mehat58d4c6c2009-06-25 08:08:05 -070052int alwaysno; /* assume "no" for all questions */
53int alwaysyes; /* assume "yes" for all questions */
54int preen; /* set when preening */
55int rdonly; /* device is opened read only (supersedes above) */
56int skipclean; /* skip clean file systems if preening */
San Mehat58d4c6c2009-06-25 08:08:05 -070057static void usage(void);
58
59static void
60usage(void)
61{
62
63 fprintf(stderr, "%s\n%s\n",
64 "usage: fsck_msdosfs -p [-f] filesystem ...",
65 " fsck_msdosfs [-ny] filesystem ...");
66 exit(1);
67}
68
69int
70main(int argc, char **argv)
71{
72 int ret = 0, erg;
73 int ch;
74
75 skipclean = 1;
76 while ((ch = getopt(argc, argv, "CfFnpy")) != -1) {
77 switch (ch) {
78 case 'C': /* for fsck_ffs compatibility */
79 break;
80 case 'f':
81 skipclean = 0;
82 break;
83 case 'F':
84 /*
85 * We can never run in the background. We must exit
86 * silently with a nonzero exit code so that fsck(8)
87 * can probe our support for -F. The exit code
88 * doesn't really matter, but we use an unusual one
89 * in case someone tries -F directly. The -F flag
90 * is intentionally left out of the usage message.
91 */
92 exit(5);
93 case 'n':
94 alwaysno = 1;
95 alwaysyes = preen = 0;
96 break;
97 case 'y':
98 alwaysyes = 1;
99 alwaysno = preen = 0;
100 break;
101
102 case 'p':
103 preen = 1;
104 alwaysyes = alwaysno = 0;
105 break;
106
107 default:
108 usage();
109 break;
110 }
111 }
112 argc -= optind;
113 argv += optind;
114
115 if (!argc)
116 usage();
117
118 while (--argc >= 0) {
119// setcdevname(*argv, preen);
120 erg = checkfilesys(*argv++);
121 if (erg > ret)
122 ret = erg;
123 }
124
125 return ret;
126}
127
128
129/*VARARGS*/
130int
131ask(int def, const char *fmt, ...)
132{
133 va_list ap;
134
135 char prompt[256];
136 int c;
jianfeng594baf12012-06-19 19:53:15 +0800137 fsck_info("%s ? \n",fmt);
San Mehat58d4c6c2009-06-25 08:08:05 -0700138 if (preen) {
139 if (rdonly)
140 def = 0;
141 if (def)
142 printf("FIXED\n");
143 return def;
144 }
145
146 va_start(ap, fmt);
147 vsnprintf(prompt, sizeof(prompt), fmt, ap);
148 if (alwaysyes || rdonly) {
149 printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
150 return !rdonly;
151 }
152 do {
153 printf("%s? [yn] ", prompt);
154 fflush(stdout);
155 c = getchar();
156 while (c != '\n' && getchar() != '\n')
157 if (feof(stdin))
158 return 0;
159 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
160 return c == 'y' || c == 'Y';
161}