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