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