main.c revision 193943
1200038Skib/*
2200038Skib * Copyright (C) 1995 Wolfgang Solfrank
3200038Skib * Copyright (c) 1995 Martin Husemann
4200038Skib *
5200038Skib * Redistribution and use in source and binary forms, with or without
6200038Skib * modification, are permitted provided that the following conditions
7200038Skib * are met:
8200038Skib * 1. Redistributions of source code must retain the above copyright
9200038Skib *    notice, this list of conditions and the following disclaimer.
10200038Skib * 2. Redistributions in binary form must reproduce the above copyright
11200038Skib *    notice, this list of conditions and the following disclaimer in the
12200038Skib *    documentation and/or other materials provided with the distribution.
13200038Skib * 3. All advertising materials mentioning features or use of this software
14200038Skib *    must display the following acknowledgement:
15200038Skib *	This product includes software developed by Martin Husemann
16200038Skib *	and Wolfgang Solfrank.
17200038Skib * 4. Neither the name of the University nor the names of its contributors
18200038Skib *    may be used to endorse or promote products derived from this software
19200038Skib *    without specific prior written permission.
20200038Skib *
21200038Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22200038Skib * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23200038Skib * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24200038Skib * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25200038Skib * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26200038Skib * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27200038Skib * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28216338Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29216338Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30200038Skib * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31200038Skib */
32200038Skib
33200038Skib
34200038Skib#include <sys/cdefs.h>
35209295Skib#ifndef lint
36209295Skib__RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
37209295Skibstatic const char rcsid[] =
38200038Skib  "$FreeBSD: head/sbin/fsck_msdosfs/main.c 193943 2009-06-10 19:02:54Z avg $";
39209295Skib#endif /* not lint */
40200038Skib
41209295Skib#include <stdlib.h>
42209295Skib#include <string.h>
43200038Skib#include <ctype.h>
44200038Skib#include <stdio.h>
45217383Skib#include <unistd.h>
46200038Skib#include <errno.h>
47200038Skib#include <stdarg.h>
48200038Skib
49200038Skib#include "fsutil.h"
50209295Skib#include "ext.h"
51209295Skib
52200038Skibint alwaysno;		/* assume "no" for all questions */
53217105Skibint alwaysyes;		/* assume "yes" for all questions */
54217105Skibint preen;		/* set when preening */
55int rdonly;		/* device is opened read only (supersedes above) */
56int skipclean;		/* skip clean file systems if preening */
57
58static void usage(void) __dead2;
59
60static void
61usage(void)
62{
63
64	fprintf(stderr, "%s\n%s\n",
65	    "usage: fsck_msdosfs -p [-f] filesystem ...",
66	    "       fsck_msdosfs [-ny] filesystem ...");
67	exit(1);
68}
69
70int
71main(int argc, char **argv)
72{
73	int ret = 0, erg;
74	int ch;
75
76	skipclean = 1;
77	while ((ch = getopt(argc, argv, "CfFnpy")) != -1) {
78		switch (ch) {
79		case 'C': /* for fsck_ffs compatibility */
80			break;
81		case 'f':
82			skipclean = 0;
83			break;
84		case 'F':
85			/*
86			 * We can never run in the background.  We must exit
87			 * silently with a nonzero exit code so that fsck(8)
88			 * can probe our support for -F.  The exit code
89			 * doesn't really matter, but we use an unusual one
90			 * in case someone tries -F directly.  The -F flag
91			 * is intentionally left out of the usage message.
92			 */
93			exit(5);
94		case 'n':
95			alwaysno = 1;
96			alwaysyes = preen = 0;
97			break;
98		case 'y':
99			alwaysyes = 1;
100			alwaysno = preen = 0;
101			break;
102
103		case 'p':
104			preen = 1;
105			alwaysyes = alwaysno = 0;
106			break;
107
108		default:
109			usage();
110			break;
111		}
112	}
113	argc -= optind;
114	argv += optind;
115
116	if (!argc)
117		usage();
118
119	while (--argc >= 0) {
120		setcdevname(*argv, preen);
121		erg = checkfilesys(*argv++);
122		if (erg > ret)
123			ret = erg;
124	}
125
126	return ret;
127}
128
129
130/*VARARGS*/
131int
132ask(int def, const char *fmt, ...)
133{
134	va_list ap;
135
136	char prompt[256];
137	int c;
138
139	if (preen) {
140		if (rdonly)
141			def = 0;
142		if (def)
143			printf("FIXED\n");
144		return def;
145	}
146
147	va_start(ap, fmt);
148	vsnprintf(prompt, sizeof(prompt), fmt, ap);
149	if (alwaysyes || rdonly) {
150		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
151		return !rdonly;
152	}
153	do {
154		printf("%s? [yn] ", prompt);
155		fflush(stdout);
156		c = getchar();
157		while (c != '\n' && getchar() != '\n')
158			if (feof(stdin))
159				return 0;
160	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
161	return c == 'y' || c == 'Y';
162}
163