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