Home | History | Annotate | Line # | Download | only in fsck_msdos
main.c revision 1.2
      1  1.2  ws /*	$NetBSD: main.c,v 1.2 1996/05/25 17:09:47 ws 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.1  ws #ifndef lint
     37  1.2  ws static char rcsid[] = "$NetBSD: main.c,v 1.2 1996/05/25 17:09:47 ws Exp $";
     38  1.1  ws #endif /* not lint */
     39  1.1  ws 
     40  1.1  ws #include <stdlib.h>
     41  1.1  ws #include <string.h>
     42  1.1  ws #include <ctype.h>
     43  1.1  ws #include <stdio.h>
     44  1.1  ws #include <unistd.h>
     45  1.1  ws #include <errno.h>
     46  1.1  ws #if __STDC__
     47  1.1  ws #include <stdarg.h>
     48  1.1  ws #else
     49  1.1  ws #include <varargs.h>
     50  1.1  ws #endif
     51  1.1  ws 
     52  1.1  ws #include "ext.h"
     53  1.1  ws 
     54  1.1  ws int alwaysno;		/* assume "no" for all questions */
     55  1.1  ws int alwaysyes;		/* assume "yes" for all questions */
     56  1.1  ws int preen;		/* set when preening */
     57  1.1  ws int rdonly;		/* device is opened read only (supersedes above) */
     58  1.1  ws 
     59  1.1  ws char *fname;		/* filesystem currently checked */
     60  1.1  ws 
     61  1.1  ws static void
     62  1.1  ws usage()
     63  1.1  ws {
     64  1.1  ws 	errexit("Usage: fsck_msdos [-pny] filesystem ... \n");
     65  1.1  ws }
     66  1.1  ws 
     67  1.1  ws int
     68  1.1  ws main(argc, argv)
     69  1.1  ws 	int argc;
     70  1.1  ws 	char **argv;
     71  1.1  ws {
     72  1.1  ws 	extern int optind;
     73  1.1  ws 	int ret = 0, erg;
     74  1.1  ws 	int ch;
     75  1.1  ws 
     76  1.1  ws 	while ((ch = getopt(argc, argv, "vpyn")) != EOF) {
     77  1.1  ws 		switch (ch) {
     78  1.1  ws 		case 'n':
     79  1.1  ws 			alwaysno = 1;
     80  1.1  ws 			alwaysyes = preen = 0;
     81  1.1  ws 			break;
     82  1.1  ws 		case 'y':
     83  1.1  ws 			alwaysyes = 1;
     84  1.1  ws 			alwaysno = preen = 0;
     85  1.1  ws 			break;
     86  1.1  ws 
     87  1.1  ws 		case 'p':
     88  1.1  ws 			preen = 1;
     89  1.1  ws 			alwaysyes = alwaysno = 0;
     90  1.1  ws 			break;
     91  1.1  ws 
     92  1.1  ws 		default:
     93  1.1  ws 			usage();
     94  1.1  ws 			break;
     95  1.1  ws 		}
     96  1.1  ws 	}
     97  1.1  ws 	argc -= optind;
     98  1.1  ws 	argv += optind;
     99  1.1  ws 
    100  1.1  ws 	if (!argc)
    101  1.1  ws 		usage();
    102  1.1  ws 
    103  1.1  ws 	while (argc-- > 0) {
    104  1.1  ws 		erg = checkfilesys(fname = *argv++);
    105  1.1  ws 		if (erg > ret)
    106  1.1  ws 			ret = erg;
    107  1.1  ws 	}
    108  1.1  ws 	exit(ret);
    109  1.1  ws }
    110  1.1  ws 
    111  1.1  ws /*VARARGS*/
    112  1.1  ws void
    113  1.1  ws #if __STDC__
    114  1.1  ws errexit(const char *fmt, ...)
    115  1.1  ws #else
    116  1.1  ws errexit(fmt, va_alist)
    117  1.1  ws 	char *fmt;
    118  1.1  ws 	va_dcl
    119  1.1  ws #endif
    120  1.1  ws {
    121  1.1  ws 	va_list ap;
    122  1.1  ws 
    123  1.1  ws #if __STDC__
    124  1.1  ws 	va_start(ap, fmt);
    125  1.1  ws #else
    126  1.1  ws 	va_start(ap);
    127  1.1  ws #endif
    128  1.1  ws 	vprintf(fmt, ap);
    129  1.1  ws 	va_end(ap);
    130  1.1  ws 	exit(8);
    131  1.1  ws }
    132  1.1  ws 
    133  1.1  ws /*VARARGS*/
    134  1.1  ws void
    135  1.1  ws #if __STDC__
    136  1.1  ws pfatal(const char *fmt, ...)
    137  1.1  ws #else
    138  1.1  ws pfatal(fmt, va_alist)
    139  1.1  ws 	char *fmt;
    140  1.1  ws 	va_dcl
    141  1.1  ws #endif
    142  1.1  ws {
    143  1.1  ws 	va_list ap;
    144  1.1  ws 
    145  1.1  ws 	if (preen)
    146  1.1  ws 		printf("%s: ", fname);
    147  1.1  ws #if __STDC__
    148  1.1  ws 	va_start(ap, fmt);
    149  1.1  ws #else
    150  1.1  ws 	va_start(ap);
    151  1.1  ws #endif
    152  1.1  ws 	vprintf(fmt, ap);
    153  1.1  ws 	va_end(ap);
    154  1.1  ws 	printf("\n");
    155  1.1  ws 	if (preen)
    156  1.1  ws 		exit(8);
    157  1.1  ws }
    158  1.1  ws 
    159  1.1  ws /*VARARGS*/
    160  1.1  ws void
    161  1.1  ws #if __STDC__
    162  1.1  ws pwarn(const char *fmt, ...)
    163  1.1  ws #else
    164  1.1  ws pwarn(fmt, va_alist)
    165  1.1  ws 	char *fmt;
    166  1.1  ws 	va_dcl
    167  1.1  ws #endif
    168  1.1  ws {
    169  1.1  ws 	va_list ap;
    170  1.1  ws 
    171  1.1  ws 	if (preen)
    172  1.1  ws 		printf("%s: ", fname);
    173  1.1  ws #if __STDC__
    174  1.1  ws 	va_start(ap, fmt);
    175  1.1  ws #else
    176  1.1  ws 	va_start(ap);
    177  1.1  ws #endif
    178  1.1  ws 	vprintf(fmt, ap);
    179  1.1  ws 	va_end(ap);
    180  1.1  ws }
    181  1.1  ws 
    182  1.1  ws void
    183  1.1  ws perror(s)
    184  1.1  ws 	const char *s;
    185  1.1  ws {
    186  1.1  ws 	pfatal("%s (%s)", s, strerror(errno));
    187  1.1  ws }
    188  1.1  ws 
    189  1.1  ws /*VARARGS*/
    190  1.1  ws int
    191  1.1  ws #if __STDC__
    192  1.1  ws ask(int def, const char *fmt, ...)
    193  1.1  ws #else
    194  1.1  ws ask(def, fmt, va_alist)
    195  1.1  ws 	int def;
    196  1.1  ws 	char *fmt;
    197  1.1  ws 	va_dcl
    198  1.1  ws #endif
    199  1.1  ws {
    200  1.1  ws 	va_list ap;
    201  1.1  ws 
    202  1.1  ws 	char prompt[256];
    203  1.1  ws 	int c;
    204  1.1  ws 
    205  1.1  ws 	if (preen) {
    206  1.1  ws 		if (rdonly)
    207  1.1  ws 			def = 0;
    208  1.1  ws 		if (def)
    209  1.1  ws 			printf("FIXED\n");
    210  1.1  ws 		return def;
    211  1.1  ws 	}
    212  1.1  ws 
    213  1.1  ws #if __STDC__
    214  1.1  ws 	va_start(ap, fmt);
    215  1.1  ws #else
    216  1.1  ws 	va_start(ap);
    217  1.1  ws #endif
    218  1.1  ws 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
    219  1.1  ws 	if (alwaysyes || rdonly) {
    220  1.1  ws 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
    221  1.1  ws 		return !rdonly;
    222  1.1  ws 	}
    223  1.1  ws 	do {
    224  1.1  ws 		printf("%s? [yn] ", prompt);
    225  1.1  ws 		fflush(stdout);
    226  1.1  ws 		c = getchar();
    227  1.1  ws 		while (c != '\n' && getchar() != '\n')
    228  1.1  ws 			if (feof(stdin))
    229  1.1  ws 				return 0;
    230  1.1  ws 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    231  1.1  ws 	return c == 'y' || c == 'Y';
    232  1.1  ws }
    233