Home | History | Annotate | Line # | Download | only in fsck_msdos
main.c revision 1.20.4.1
      1  1.20.4.1  wrstuden /*	$NetBSD: main.c,v 1.20.4.1 2008/06/23 04:29:57 wrstuden Exp $	*/
      2       1.1        ws 
      3       1.1        ws /*
      4       1.1        ws  * Copyright (C) 1995 Wolfgang Solfrank
      5       1.1        ws  * Copyright (c) 1995 Martin Husemann
      6       1.1        ws  *
      7       1.1        ws  * Redistribution and use in source and binary forms, with or without
      8       1.1        ws  * modification, are permitted provided that the following conditions
      9       1.1        ws  * are met:
     10       1.1        ws  * 1. Redistributions of source code must retain the above copyright
     11       1.1        ws  *    notice, this list of conditions and the following disclaimer.
     12       1.1        ws  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1        ws  *    notice, this list of conditions and the following disclaimer in the
     14       1.1        ws  *    documentation and/or other materials provided with the distribution.
     15       1.1        ws  *
     16       1.1        ws  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     17       1.1        ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1        ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1        ws  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1        ws  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1        ws  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1        ws  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1        ws  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1        ws  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1        ws  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1        ws  */
     27       1.1        ws 
     28       1.1        ws 
     29       1.9     lukem #include <sys/cdefs.h>
     30       1.1        ws #ifndef lint
     31  1.20.4.1  wrstuden __RCSID("$NetBSD: main.c,v 1.20.4.1 2008/06/23 04:29:57 wrstuden Exp $");
     32       1.1        ws #endif /* not lint */
     33       1.1        ws 
     34       1.1        ws #include <stdlib.h>
     35       1.1        ws #include <string.h>
     36       1.1        ws #include <stdio.h>
     37       1.1        ws #include <unistd.h>
     38       1.1        ws #include <errno.h>
     39       1.1        ws #include <stdarg.h>
     40      1.20  christos #include <signal.h>
     41       1.1        ws 
     42       1.6  christos #include "fsutil.h"
     43       1.1        ws #include "ext.h"
     44      1.19  christos #include "exitvalues.h"
     45       1.1        ws 
     46       1.1        ws int alwaysno;		/* assume "no" for all questions */
     47       1.1        ws int alwaysyes;		/* assume "yes" for all questions */
     48       1.1        ws int preen;		/* set when preening */
     49       1.1        ws int rdonly;		/* device is opened read only (supersedes above) */
     50       1.1        ws 
     51      1.19  christos static void usage(void) __dead;
     52       1.3  christos 
     53       1.1        ws static void
     54      1.17   xtraeme usage(void)
     55       1.1        ws {
     56      1.19  christos     	(void)fprintf(stderr, "Usage: %s [-fnpy] filesystem ... \n",
     57      1.19  christos 	    getprogname());
     58      1.19  christos 	exit(FSCK_EXIT_USAGE);
     59       1.1        ws }
     60       1.1        ws 
     61      1.20  christos static void
     62      1.20  christos catch(int n)
     63      1.20  christos {
     64      1.20  christos 	exit(FSCK_EXIT_SIGNALLED);
     65      1.20  christos }
     66      1.20  christos 
     67       1.1        ws int
     68      1.17   xtraeme main(int argc, char **argv)
     69       1.1        ws {
     70      1.19  christos 	int ret = FSCK_EXIT_OK, erg;
     71       1.1        ws 	int ch;
     72       1.1        ws 
     73      1.16  christos 	while ((ch = getopt(argc, argv, "pPqynf")) != -1) {
     74       1.1        ws 		switch (ch) {
     75       1.7  christos 		case 'f':
     76       1.7  christos 			/*
     77       1.7  christos 			 * We are always forced, since we don't
     78       1.7  christos 			 * have a clean flag
     79       1.7  christos 			 */
     80       1.7  christos 			break;
     81       1.1        ws 		case 'n':
     82       1.1        ws 			alwaysno = 1;
     83       1.1        ws 			alwaysyes = preen = 0;
     84       1.1        ws 			break;
     85       1.1        ws 		case 'y':
     86       1.1        ws 			alwaysyes = 1;
     87       1.1        ws 			alwaysno = preen = 0;
     88       1.1        ws 			break;
     89       1.1        ws 
     90       1.1        ws 		case 'p':
     91       1.1        ws 			preen = 1;
     92       1.1        ws 			alwaysyes = alwaysno = 0;
     93      1.14       dsl 			break;
     94      1.14       dsl 
     95      1.16  christos 		case 'P':		/* Progress meter not implemented. */
     96      1.16  christos 			break;
     97      1.16  christos 
     98      1.16  christos 		case 'q':		/* Quiet not implemented. */
     99       1.1        ws 			break;
    100      1.11        ws 
    101       1.1        ws 		default:
    102       1.1        ws 			usage();
    103       1.1        ws 			break;
    104       1.1        ws 		}
    105       1.1        ws 	}
    106       1.1        ws 	argc -= optind;
    107       1.1        ws 	argv += optind;
    108       1.1        ws 
    109       1.5        ws 	if (!argc)
    110       1.5        ws 		usage();
    111       1.5        ws 
    112      1.20  christos 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    113      1.20  christos 		(void) signal(SIGINT, catch);
    114      1.20  christos 	if (preen)
    115      1.20  christos 		(void) signal(SIGQUIT, catch);
    116      1.20  christos 
    117       1.5        ws 	while (--argc >= 0) {
    118       1.4  christos 		setcdevname(*argv, preen);
    119       1.4  christos 		erg = checkfilesys(*argv++);
    120       1.4  christos 		if (erg > ret)
    121       1.4  christos 			ret = erg;
    122       1.1        ws 	}
    123       1.3  christos 
    124       1.3  christos 	return ret;
    125       1.1        ws }
    126       1.1        ws 
    127       1.1        ws 
    128       1.1        ws /*VARARGS*/
    129       1.1        ws int
    130       1.1        ws ask(int def, const char *fmt, ...)
    131       1.1        ws {
    132       1.1        ws 	va_list ap;
    133      1.11        ws 
    134       1.1        ws 	char prompt[256];
    135       1.1        ws 	int c;
    136       1.1        ws 
    137       1.1        ws 	if (preen) {
    138       1.1        ws 		if (rdonly)
    139       1.1        ws 			def = 0;
    140       1.1        ws 		if (def)
    141       1.1        ws 			printf("FIXED\n");
    142       1.1        ws 		return def;
    143       1.1        ws 	}
    144       1.1        ws 
    145       1.1        ws 	va_start(ap, fmt);
    146       1.1        ws 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
    147      1.12       wiz 	va_end(ap);
    148       1.1        ws 	if (alwaysyes || rdonly) {
    149       1.1        ws 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
    150       1.1        ws 		return !rdonly;
    151       1.1        ws 	}
    152       1.1        ws 	do {
    153       1.1        ws 		printf("%s? [yn] ", prompt);
    154       1.1        ws 		fflush(stdout);
    155       1.1        ws 		c = getchar();
    156       1.1        ws 		while (c != '\n' && getchar() != '\n')
    157       1.1        ws 			if (feof(stdin))
    158       1.1        ws 				return 0;
    159       1.1        ws 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    160       1.1        ws 	return c == 'y' || c == 'Y';
    161       1.1        ws }
    162