Home | History | Annotate | Line # | Download | only in fsck_msdos
main.c revision 1.17
      1 /*	$NetBSD: main.c,v 1.17 2005/01/19 20:00:45 xtraeme 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.17 2005/01/19 20:00:45 xtraeme Exp $");
     39 #endif /* not lint */
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <ctype.h>
     44 #include <stdio.h>
     45 #include <unistd.h>
     46 #include <errno.h>
     47 #include <stdarg.h>
     48 
     49 #include "fsutil.h"
     50 #include "ext.h"
     51 
     52 int alwaysno;		/* assume "no" for all questions */
     53 int alwaysyes;		/* assume "yes" for all questions */
     54 int preen;		/* set when preening */
     55 int rdonly;		/* device is opened read only (supersedes above) */
     56 
     57 static void usage(void);
     58 
     59 static void
     60 usage(void)
     61 {
     62 	errexit("usage: fsck_msdos [-fnpy] filesystem ... \n");
     63 }
     64 
     65 int
     66 main(int argc, char **argv)
     67 {
     68 	int ret = 0, erg;
     69 	int ch;
     70 
     71 	while ((ch = getopt(argc, argv, "pPqynf")) != -1) {
     72 		switch (ch) {
     73 		case 'f':
     74 			/*
     75 			 * We are always forced, since we don't
     76 			 * have a clean flag
     77 			 */
     78 			break;
     79 		case 'n':
     80 			alwaysno = 1;
     81 			alwaysyes = preen = 0;
     82 			break;
     83 		case 'y':
     84 			alwaysyes = 1;
     85 			alwaysno = preen = 0;
     86 			break;
     87 
     88 		case 'p':
     89 			preen = 1;
     90 			alwaysyes = alwaysno = 0;
     91 			break;
     92 
     93 		case 'P':		/* Progress meter not implemented. */
     94 			break;
     95 
     96 		case 'q':		/* Quiet not implemented. */
     97 			break;
     98 
     99 		default:
    100 			usage();
    101 			break;
    102 		}
    103 	}
    104 	argc -= optind;
    105 	argv += optind;
    106 
    107 	if (!argc)
    108 		usage();
    109 
    110 	while (--argc >= 0) {
    111 		setcdevname(*argv, preen);
    112 		erg = checkfilesys(*argv++);
    113 		if (erg > ret)
    114 			ret = erg;
    115 	}
    116 
    117 	return ret;
    118 }
    119 
    120 
    121 /*VARARGS*/
    122 int
    123 ask(int def, const char *fmt, ...)
    124 {
    125 	va_list ap;
    126 
    127 	char prompt[256];
    128 	int c;
    129 
    130 	if (preen) {
    131 		if (rdonly)
    132 			def = 0;
    133 		if (def)
    134 			printf("FIXED\n");
    135 		return def;
    136 	}
    137 
    138 	va_start(ap, fmt);
    139 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
    140 	va_end(ap);
    141 	if (alwaysyes || rdonly) {
    142 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
    143 		return !rdonly;
    144 	}
    145 	do {
    146 		printf("%s? [yn] ", prompt);
    147 		fflush(stdout);
    148 		c = getchar();
    149 		while (c != '\n' && getchar() != '\n')
    150 			if (feof(stdin))
    151 				return 0;
    152 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    153 	return c == 'y' || c == 'Y';
    154 }
    155