main.c revision 123880
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 * 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: head/sbin/fsck_msdosfs/main.c 123880 2003-12-27 05:57:20Z bde $";
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"
51
52int alwaysno;		/* assume "no" for all questions */
53int alwaysyes;		/* assume "yes" for all questions */
54int force;		/* force check even the fs is clean */
55int preen;		/* set when preening */
56int rdonly;		/* device is opened read only (supersedes above) */
57
58static void usage(void) __dead2;
59
60static void
61usage()
62{
63	errexit("usage: fsck_msdos [-fnpy] filesystem ... \n");
64}
65
66int
67main(int argc, char **argv)
68{
69	int ret = 0, erg;
70	int ch;
71
72	force = 0;
73	while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
74		switch (ch) {
75		case 'f':
76			force = 1;
77			break;
78		case 'F':
79			/* We can never run in background */
80			exit(5);
81			break;
82		case 'n':
83			alwaysno = 1;
84			alwaysyes = preen = 0;
85			break;
86		case 'y':
87			alwaysyes = 1;
88			alwaysno = preen = 0;
89			break;
90
91		case 'p':
92			preen = 1;
93			alwaysyes = alwaysno = 0;
94			break;
95
96		default:
97			usage();
98			break;
99		}
100	}
101	argc -= optind;
102	argv += optind;
103
104	if (!argc)
105		usage();
106
107	while (--argc >= 0) {
108		setcdevname(*argv, preen);
109		erg = checkfilesys(*argv++);
110		if (erg > ret)
111			ret = erg;
112	}
113
114	return ret;
115}
116
117
118/*VARARGS*/
119int
120ask(int def, const char *fmt, ...)
121{
122	va_list ap;
123
124	char prompt[256];
125	int c;
126
127	if (preen) {
128		if (rdonly)
129			def = 0;
130		if (def)
131			printf("FIXED\n");
132		return def;
133	}
134
135	va_start(ap, fmt);
136	vsnprintf(prompt, sizeof(prompt), fmt, ap);
137	if (alwaysyes || rdonly) {
138		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
139		return !rdonly;
140	}
141	do {
142		printf("%s? [yn] ", prompt);
143		fflush(stdout);
144		c = getchar();
145		while (c != '\n' && getchar() != '\n')
146			if (feof(stdin))
147				return 0;
148	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
149	return c == 'y' || c == 'Y';
150}
151