1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 1995 Wolfgang Solfrank
5 * Copyright (c) 1995 Martin Husemann
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29#include <sys/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
32static const char rcsid[] =
33  "$FreeBSD$";
34#endif /* not lint */
35
36#include <stdlib.h>
37#include <string.h>
38#include <stdio.h>
39#include <unistd.h>
40#include <errno.h>
41#include <stdarg.h>
42
43#include "fsutil.h"
44#include "ext.h"
45
46int alwaysno;		/* assume "no" for all questions */
47int alwaysyes;		/* assume "yes" for all questions */
48int preen;		/* set when preening */
49int rdonly;		/* device is opened read only (supersedes above) */
50int skipclean;		/* skip clean file systems if preening */
51int allow_mmap;		/* Allow the use of mmap(), if possible */
52
53static void usage(void) __dead2;
54
55static void
56usage(void)
57{
58
59	fprintf(stderr, "%s\n%s\n",
60	    "usage: fsck_msdosfs -p [-f] filesystem ...",
61	    "       fsck_msdosfs [-ny] filesystem ...");
62	exit(1);
63}
64
65int
66main(int argc, char **argv)
67{
68	int ret = 0, erg;
69	int ch;
70
71	skipclean = 1;
72	allow_mmap = 1;
73	while ((ch = getopt(argc, argv, "CfFnpyM")) != -1) {
74		switch (ch) {
75		case 'C': /* for fsck_ffs compatibility */
76			break;
77		case 'f':
78			skipclean = 0;
79			break;
80		case 'F':
81			/*
82			 * We can never run in the background.  We must exit
83			 * silently with a nonzero exit code so that fsck(8)
84			 * can probe our support for -F.  The exit code
85			 * doesn't really matter, but we use an unusual one
86			 * in case someone tries -F directly.  The -F flag
87			 * is intentionally left out of the usage message.
88			 */
89			exit(5);
90		case 'n':
91			alwaysno = 1;
92			alwaysyes = 0;
93			break;
94		case 'y':
95			alwaysyes = 1;
96			alwaysno = 0;
97			break;
98
99		case 'p':
100			preen = 1;
101			break;
102
103		case 'M':
104			allow_mmap = 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;
137
138	if (alwaysyes || alwaysno || rdonly)
139		def = (alwaysyes && !rdonly && !alwaysno);
140
141	if (preen) {
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	va_end(ap);
150	if (alwaysyes || alwaysno || rdonly) {
151		printf("%s? %s\n", prompt, def ? "yes" : "no");
152		return def;
153	}
154	do {
155		printf("%s? [yn] ", prompt);
156		fflush(stdout);
157		c = getchar();
158		while (c != '\n' && getchar() != '\n')
159			if (feof(stdin))
160				return 0;
161	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
162	return c == 'y' || c == 'Y';
163}
164