main.c revision 123892
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 123892 2003-12-27 14:02:52Z bde $";
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 */
54123880Sbdeint force;		/* force check even the fs is clean */
5579455Sobrienint preen;		/* set when preening */
5679455Sobrienint rdonly;		/* device is opened read only (supersedes above) */
5779455Sobrien
5892839Simpstatic void usage(void) __dead2;
5979455Sobrien
6079455Sobrienstatic void
61123888Sbdeusage(void)
6279455Sobrien{
63123888Sbde
64123888Sbde	fprintf(stderr, "usage: fsck_msdos [-fnpy] filesystem ...\n");
65123888Sbde	exit(1);
6679455Sobrien}
6779455Sobrien
6879455Sobrienint
6992839Simpmain(int argc, char **argv)
7079455Sobrien{
7179455Sobrien	int ret = 0, erg;
7279455Sobrien	int ch;
73123880Sbde
74123873Strhodes	force = 0;
7579976Sobrien	while ((ch = getopt(argc, argv, "fFnpy")) != -1) {
7679455Sobrien		switch (ch) {
7779455Sobrien		case 'f':
78123873Strhodes			force = 1;
7979455Sobrien			break;
8079976Sobrien		case 'F':
81123892Sbde			/*
82123892Sbde			 * We can never run in the background.  We must exit
83123892Sbde			 * silently with a nonzero exit code so that fsck(8)
84123892Sbde			 * can probe our support for -F.  The exit code
85123892Sbde			 * doesn't really matter, but we use an unusual one
86123892Sbde			 * in case someone tries -F directly.  The -F flag
87123892Sbde			 * is intentionally left out of the usage message.
88123892Sbde			 */
8979977Sobrien			exit(5);
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
12879455Sobrienask(int def, const char *fmt, ...)
12979455Sobrien{
13079455Sobrien	va_list ap;
13179455Sobrien
13279455Sobrien	char prompt[256];
13379455Sobrien	int c;
13479455Sobrien
13579455Sobrien	if (preen) {
13679455Sobrien		if (rdonly)
13779455Sobrien			def = 0;
13879455Sobrien		if (def)
13979455Sobrien			printf("FIXED\n");
14079455Sobrien		return def;
14179455Sobrien	}
14279455Sobrien
14379455Sobrien	va_start(ap, fmt);
14479455Sobrien	vsnprintf(prompt, sizeof(prompt), fmt, ap);
14579455Sobrien	if (alwaysyes || rdonly) {
14679455Sobrien		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
14779455Sobrien		return !rdonly;
14879455Sobrien	}
14979455Sobrien	do {
15079455Sobrien		printf("%s? [yn] ", prompt);
15179455Sobrien		fflush(stdout);
15279455Sobrien		c = getchar();
15379455Sobrien		while (c != '\n' && getchar() != '\n')
15479455Sobrien			if (feof(stdin))
15579455Sobrien				return 0;
15679455Sobrien	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
15779455Sobrien	return c == 'y' || c == 'Y';
15879455Sobrien}
159