main.c revision 141612
179455Sobrien/*
279455Sobrien * Copyright (C) 1995 Wolfgang Solfrank
379455Sobrien * Copyright (c) 1995 Martin Husemann
479455Sobrien *
579455Sobrien * Redistribution and use in source and binary forms, with or without
679455Sobrien * modification, are permitted provided that the following conditions
779455Sobrien * are met:
879455Sobrien * 1. Redistributions of source code must retain the above copyright
979455Sobrien *    notice, this list of conditions and the following disclaimer.
1079455Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1179455Sobrien *    notice, this list of conditions and the following disclaimer in the
1279455Sobrien *    documentation and/or other materials provided with the distribution.
1379455Sobrien * 3. All advertising materials mentioning features or use of this software
1479455Sobrien *    must display the following acknowledgement:
1579455Sobrien *	This product includes software developed by Martin Husemann
1679455Sobrien *	and Wolfgang Solfrank.
1779455Sobrien * 4. Neither the name of the University nor the names of its contributors
1879455Sobrien *    may be used to endorse or promote products derived from this software
1979455Sobrien *    without specific prior written permission.
2079455Sobrien *
2179455Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
2279455Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2379455Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2479455Sobrien * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2579455Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2679455Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2779455Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2879455Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2979455Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3079455Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3179455Sobrien */
3279455Sobrien
3379455Sobrien
3479455Sobrien#include <sys/cdefs.h>
3579455Sobrien#ifndef lint
3679455Sobrien__RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
3779455Sobrienstatic const char rcsid[] =
3879455Sobrien  "$FreeBSD: head/sbin/fsck_msdosfs/main.c 141612 2005-02-10 09:39:51Z ru $";
3979455Sobrien#endif /* not lint */
4079455Sobrien
4179455Sobrien#include <stdlib.h>
4279455Sobrien#include <string.h>
4379455Sobrien#include <ctype.h>
4479455Sobrien#include <stdio.h>
4579455Sobrien#include <unistd.h>
4679455Sobrien#include <errno.h>
4779455Sobrien#include <stdarg.h>
4879455Sobrien
4979455Sobrien#include "fsutil.h"
5079455Sobrien#include "ext.h"
5179455Sobrien
5279455Sobrienint alwaysno;		/* assume "no" for all questions */
5379455Sobrienint alwaysyes;		/* assume "yes" for all questions */
5479455Sobrienint preen;		/* set when preening */
5579455Sobrienint rdonly;		/* device is opened read only (supersedes above) */
56125486Sbdeint skipclean;		/* skip clean file systems if preening */
5779455Sobrien
5892839Simpstatic void usage(void) __dead2;
5979455Sobrien
6079455Sobrienstatic void
61123888Sbdeusage(void)
6279455Sobrien{
63123888Sbde
64141612Sru	fprintf(stderr, "%s\n%s\n",
65141612Sru	    "usage: fsck_msdosfs -p [-f] filesystem ...",
66141612Sru	    "       fsck_msdosfs [-ny] filesystem ...");
67123888Sbde	exit(1);
6879455Sobrien}
6979455Sobrien
7079455Sobrienint
7192839Simpmain(int argc, char **argv)
7279455Sobrien{
7379455Sobrien	int ret = 0, erg;
7479455Sobrien	int ch;
75123880Sbde
76125486Sbde	skipclean = 1;
7779976Sobrien	while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
7879455Sobrien		switch (ch) {
7979455Sobrien		case 'f':
80125486Sbde			skipclean = 0;
8179455Sobrien			break;
8279976Sobrien		case 'F':
83123892Sbde			/*
84123892Sbde			 * We can never run in the background.  We must exit
85123892Sbde			 * silently with a nonzero exit code so that fsck(8)
86123892Sbde			 * can probe our support for -F.  The exit code
87123892Sbde			 * doesn't really matter, but we use an unusual one
88123892Sbde			 * in case someone tries -F directly.  The -F flag
89123892Sbde			 * is intentionally left out of the usage message.
90123892Sbde			 */
9179977Sobrien			exit(5);
9279455Sobrien		case 'n':
9379455Sobrien			alwaysno = 1;
9479455Sobrien			alwaysyes = preen = 0;
9579455Sobrien			break;
9679455Sobrien		case 'y':
9779455Sobrien			alwaysyes = 1;
9879455Sobrien			alwaysno = preen = 0;
9979455Sobrien			break;
10079455Sobrien
10179455Sobrien		case 'p':
10279455Sobrien			preen = 1;
10379455Sobrien			alwaysyes = alwaysno = 0;
10479455Sobrien			break;
10579455Sobrien
10679455Sobrien		default:
10779455Sobrien			usage();
10879455Sobrien			break;
10979455Sobrien		}
11079455Sobrien	}
11179455Sobrien	argc -= optind;
11279455Sobrien	argv += optind;
11379455Sobrien
11479455Sobrien	if (!argc)
11579455Sobrien		usage();
11679455Sobrien
11779455Sobrien	while (--argc >= 0) {
11879455Sobrien		setcdevname(*argv, preen);
11979455Sobrien		erg = checkfilesys(*argv++);
12079455Sobrien		if (erg > ret)
12179455Sobrien			ret = erg;
12279455Sobrien	}
12379455Sobrien
12479455Sobrien	return ret;
12579455Sobrien}
12679455Sobrien
12779455Sobrien
12879455Sobrien/*VARARGS*/
12979455Sobrienint
13079455Sobrienask(int def, const char *fmt, ...)
13179455Sobrien{
13279455Sobrien	va_list ap;
13379455Sobrien
13479455Sobrien	char prompt[256];
13579455Sobrien	int c;
13679455Sobrien
13779455Sobrien	if (preen) {
13879455Sobrien		if (rdonly)
13979455Sobrien			def = 0;
14079455Sobrien		if (def)
14179455Sobrien			printf("FIXED\n");
14279455Sobrien		return def;
14379455Sobrien	}
14479455Sobrien
14579455Sobrien	va_start(ap, fmt);
14679455Sobrien	vsnprintf(prompt, sizeof(prompt), fmt, ap);
14779455Sobrien	if (alwaysyes || rdonly) {
14879455Sobrien		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
14979455Sobrien		return !rdonly;
15079455Sobrien	}
15179455Sobrien	do {
15279455Sobrien		printf("%s? [yn] ", prompt);
15379455Sobrien		fflush(stdout);
15479455Sobrien		c = getchar();
15579455Sobrien		while (c != '\n' && getchar() != '\n')
15679455Sobrien			if (feof(stdin))
15779455Sobrien				return 0;
15879455Sobrien	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
15979455Sobrien	return c == 'y' || c == 'Y';
16079455Sobrien}
161