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