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 <stdlib.h>
28#include <string.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <errno.h>
32#include <stdarg.h>
33
34#include "ext.h"
35
36int alwaysno;		/* assume "no" for all questions */
37int alwaysyes;		/* assume "yes" for all questions */
38int preen;		/* set when preening */
39int rdonly;		/* device is opened read only (supersedes above) */
40int skipclean;		/* skip clean file systems if preening */
41
42static void usage(void)
43{
44
45	fprintf(stderr, "%s\n%s\n",
46	    "usage: fsck_msdosfs -p [-f] filesystem ...",
47	    "       fsck_msdosfs [-ny] filesystem ...");
48	exit(1);
49}
50
51int
52main(int argc, char **argv)
53{
54	int ret = 0, erg;
55	int ch;
56
57	skipclean = 1;
58	while ((ch = getopt(argc, argv, "CfFnpy")) != -1) {
59		switch (ch) {
60		case 'C': /* for fsck_ffs compatibility */
61			break;
62		case 'f':
63			skipclean = 0;
64			break;
65		case 'F':
66			/*
67			 * We can never run in the background.  We must exit
68			 * silently with a nonzero exit code so that fsck(8)
69			 * can probe our support for -F.  The exit code
70			 * doesn't really matter, but we use an unusual one
71			 * in case someone tries -F directly.  The -F flag
72			 * is intentionally left out of the usage message.
73			 */
74			exit(5);
75		case 'n':
76			alwaysno = 1;
77			alwaysyes = preen = 0;
78			break;
79		case 'y':
80			alwaysyes = 1;
81			alwaysno = preen = 0;
82			break;
83
84		case 'p':
85			preen = 1;
86			alwaysyes = alwaysno = 0;
87			break;
88
89		default:
90			usage();
91			break;
92		}
93	}
94	argc -= optind;
95	argv += optind;
96
97	if (!argc)
98		usage();
99
100	while (--argc >= 0) {
101		erg = checkfilesys(*argv++);
102		if (erg > ret)
103			ret = erg;
104	}
105
106	return ret;
107}
108
109
110/*VARARGS*/
111int
112ask(int def, const char *fmt, ...)
113{
114	va_list ap;
115
116	char prompt[256];
117	int c;
118
119	if (preen) {
120		if (rdonly)
121			def = 0;
122		if (def)
123			printf("FIXED\n");
124		return def;
125	}
126
127	va_start(ap, fmt);
128	vsnprintf(prompt, sizeof(prompt), fmt, ap);
129	va_end(ap);
130	if (alwaysyes || rdonly) {
131		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
132		return !rdonly;
133	}
134	do {
135		printf("%s? [yn] ", prompt);
136		fflush(stdout);
137		c = getchar();
138		while (c != '\n' && getchar() != '\n')
139			if (feof(stdin))
140				return 0;
141	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
142	return c == 'y' || c == 'Y';
143}
144