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