main.c revision 79977
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 79977 2001-07-19 17:06:08Z obrien $";
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#if __STDC__
4879455Sobrien#include <stdarg.h>
4979455Sobrien#else
5079455Sobrien#include <varargs.h>
5179455Sobrien#endif
5279455Sobrien
5379455Sobrien#include "fsutil.h"
5479455Sobrien#include "ext.h"
5579455Sobrien
5679455Sobrienint alwaysno;		/* assume "no" for all questions */
5779455Sobrienint alwaysyes;		/* assume "yes" for all questions */
5879455Sobrienint preen;		/* set when preening */
5979455Sobrienint rdonly;		/* device is opened read only (supersedes above) */
6079455Sobrien
6179455Sobrienstatic void usage __P((void));
6279455Sobrienint main __P((int, char **));
6379455Sobrien
6479455Sobrienstatic void
6579455Sobrienusage()
6679455Sobrien{
6779455Sobrien	errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n");
6879455Sobrien}
6979455Sobrien
7079455Sobrienint
7179455Sobrienmain(argc, argv)
7279455Sobrien	int argc;
7379455Sobrien	char **argv;
7479455Sobrien{
7579455Sobrien	int ret = 0, erg;
7679455Sobrien	int ch;
7779455Sobrien
7879976Sobrien	while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
7979455Sobrien		switch (ch) {
8079455Sobrien		case 'f':
8179455Sobrien			/*
8279455Sobrien			 * We are always forced, since we don't
8379455Sobrien			 * have a clean flag
8479455Sobrien			 */
8579455Sobrien			break;
8679976Sobrien		case 'F':
8779976Sobrien			/* We can never run in background */
8879977Sobrien			exit(5);
8979976Sobrien			break;
9079455Sobrien		case 'n':
9179455Sobrien			alwaysno = 1;
9279455Sobrien			alwaysyes = preen = 0;
9379455Sobrien			break;
9479455Sobrien		case 'y':
9579455Sobrien			alwaysyes = 1;
9679455Sobrien			alwaysno = preen = 0;
9779455Sobrien			break;
9879455Sobrien
9979455Sobrien		case 'p':
10079455Sobrien			preen = 1;
10179455Sobrien			alwaysyes = alwaysno = 0;
10279455Sobrien			break;
10379455Sobrien
10479455Sobrien		default:
10579455Sobrien			usage();
10679455Sobrien			break;
10779455Sobrien		}
10879455Sobrien	}
10979455Sobrien	argc -= optind;
11079455Sobrien	argv += optind;
11179455Sobrien
11279455Sobrien	if (!argc)
11379455Sobrien		usage();
11479455Sobrien
11579455Sobrien	while (--argc >= 0) {
11679455Sobrien		setcdevname(*argv, preen);
11779455Sobrien		erg = checkfilesys(*argv++);
11879455Sobrien		if (erg > ret)
11979455Sobrien			ret = erg;
12079455Sobrien	}
12179455Sobrien
12279455Sobrien	return ret;
12379455Sobrien}
12479455Sobrien
12579455Sobrien
12679455Sobrien/*VARARGS*/
12779455Sobrienint
12879455Sobrien#if __STDC__
12979455Sobrienask(int def, const char *fmt, ...)
13079455Sobrien#else
13179455Sobrienask(def, fmt, va_alist)
13279455Sobrien	int def;
13379455Sobrien	char *fmt;
13479455Sobrien	va_dcl
13579455Sobrien#endif
13679455Sobrien{
13779455Sobrien	va_list ap;
13879455Sobrien
13979455Sobrien	char prompt[256];
14079455Sobrien	int c;
14179455Sobrien
14279455Sobrien	if (preen) {
14379455Sobrien		if (rdonly)
14479455Sobrien			def = 0;
14579455Sobrien		if (def)
14679455Sobrien			printf("FIXED\n");
14779455Sobrien		return def;
14879455Sobrien	}
14979455Sobrien
15079455Sobrien#if __STDC__
15179455Sobrien	va_start(ap, fmt);
15279455Sobrien#else
15379455Sobrien	va_start(ap);
15479455Sobrien#endif
15579455Sobrien	vsnprintf(prompt, sizeof(prompt), fmt, ap);
15679455Sobrien	if (alwaysyes || rdonly) {
15779455Sobrien		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
15879455Sobrien		return !rdonly;
15979455Sobrien	}
16079455Sobrien	do {
16179455Sobrien		printf("%s? [yn] ", prompt);
16279455Sobrien		fflush(stdout);
16379455Sobrien		c = getchar();
16479455Sobrien		while (c != '\n' && getchar() != '\n')
16579455Sobrien			if (feof(stdin))
16679455Sobrien				return 0;
16779455Sobrien	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
16879455Sobrien	return c == 'y' || c == 'Y';
16979455Sobrien}
170