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 *
1479455Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1579455Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1679455Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1779455Sobrien * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1879455Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1979455Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2079455Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2179455Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2279455Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2379455Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2479455Sobrien */
2579455Sobrien
2679455Sobrien
2779455Sobrien#include <sys/cdefs.h>
2879455Sobrien#ifndef lint
2979455Sobrien__RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
3079455Sobrienstatic const char rcsid[] =
3179455Sobrien  "$FreeBSD$";
3279455Sobrien#endif /* not lint */
3379455Sobrien
3479455Sobrien#include <stdlib.h>
3579455Sobrien#include <string.h>
3679455Sobrien#include <stdio.h>
3779455Sobrien#include <unistd.h>
3879455Sobrien#include <errno.h>
3979455Sobrien#include <stdarg.h>
4079455Sobrien
4179455Sobrien#include "fsutil.h"
4279455Sobrien#include "ext.h"
4379455Sobrien
4479455Sobrienint alwaysno;		/* assume "no" for all questions */
4579455Sobrienint alwaysyes;		/* assume "yes" for all questions */
4679455Sobrienint preen;		/* set when preening */
4779455Sobrienint rdonly;		/* device is opened read only (supersedes above) */
48125486Sbdeint skipclean;		/* skip clean file systems if preening */
4979455Sobrien
5092839Simpstatic void usage(void) __dead2;
5179455Sobrien
5279455Sobrienstatic void
53123888Sbdeusage(void)
5479455Sobrien{
55123888Sbde
56141612Sru	fprintf(stderr, "%s\n%s\n",
57141612Sru	    "usage: fsck_msdosfs -p [-f] filesystem ...",
58141612Sru	    "       fsck_msdosfs [-ny] filesystem ...");
59123888Sbde	exit(1);
6079455Sobrien}
6179455Sobrien
6279455Sobrienint
6392839Simpmain(int argc, char **argv)
6479455Sobrien{
6579455Sobrien	int ret = 0, erg;
6679455Sobrien	int ch;
67123880Sbde
68125486Sbde	skipclean = 1;
69193943Savg	while ((ch = getopt(argc, argv, "CfFnpy")) != -1) {
7079455Sobrien		switch (ch) {
71193943Savg		case 'C': /* for fsck_ffs compatibility */
72193943Savg			break;
7379455Sobrien		case 'f':
74125486Sbde			skipclean = 0;
7579455Sobrien			break;
7679976Sobrien		case 'F':
77123892Sbde			/*
78123892Sbde			 * We can never run in the background.  We must exit
79123892Sbde			 * silently with a nonzero exit code so that fsck(8)
80123892Sbde			 * can probe our support for -F.  The exit code
81123892Sbde			 * doesn't really matter, but we use an unusual one
82123892Sbde			 * in case someone tries -F directly.  The -F flag
83123892Sbde			 * is intentionally left out of the usage message.
84123892Sbde			 */
8579977Sobrien			exit(5);
8679455Sobrien		case 'n':
8779455Sobrien			alwaysno = 1;
8879455Sobrien			alwaysyes = preen = 0;
8979455Sobrien			break;
9079455Sobrien		case 'y':
9179455Sobrien			alwaysyes = 1;
9279455Sobrien			alwaysno = preen = 0;
9379455Sobrien			break;
9479455Sobrien
9579455Sobrien		case 'p':
9679455Sobrien			preen = 1;
9779455Sobrien			alwaysyes = alwaysno = 0;
9879455Sobrien			break;
9979455Sobrien
10079455Sobrien		default:
10179455Sobrien			usage();
10279455Sobrien			break;
10379455Sobrien		}
10479455Sobrien	}
10579455Sobrien	argc -= optind;
10679455Sobrien	argv += optind;
10779455Sobrien
10879455Sobrien	if (!argc)
10979455Sobrien		usage();
11079455Sobrien
11179455Sobrien	while (--argc >= 0) {
11279455Sobrien		setcdevname(*argv, preen);
11379455Sobrien		erg = checkfilesys(*argv++);
11479455Sobrien		if (erg > ret)
11579455Sobrien			ret = erg;
11679455Sobrien	}
11779455Sobrien
11879455Sobrien	return ret;
11979455Sobrien}
12079455Sobrien
12179455Sobrien
12279455Sobrien/*VARARGS*/
12379455Sobrienint
12479455Sobrienask(int def, const char *fmt, ...)
12579455Sobrien{
12679455Sobrien	va_list ap;
12779455Sobrien
12879455Sobrien	char prompt[256];
12979455Sobrien	int c;
13079455Sobrien
13179455Sobrien	if (preen) {
13279455Sobrien		if (rdonly)
13379455Sobrien			def = 0;
13479455Sobrien		if (def)
13579455Sobrien			printf("FIXED\n");
13679455Sobrien		return def;
13779455Sobrien	}
13879455Sobrien
13979455Sobrien	va_start(ap, fmt);
14079455Sobrien	vsnprintf(prompt, sizeof(prompt), fmt, ap);
14179455Sobrien	if (alwaysyes || rdonly) {
14279455Sobrien		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
14379455Sobrien		return !rdonly;
14479455Sobrien	}
14579455Sobrien	do {
14679455Sobrien		printf("%s? [yn] ", prompt);
14779455Sobrien		fflush(stdout);
14879455Sobrien		c = getchar();
14979455Sobrien		while (c != '\n' && getchar() != '\n')
15079455Sobrien			if (feof(stdin))
15179455Sobrien				return 0;
15279455Sobrien	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
15379455Sobrien	return c == 'y' || c == 'Y';
15479455Sobrien}
155