Home | History | Annotate | Line # | Download | only in fsck_msdos
main.c revision 1.3
      1 /*	$NetBSD: main.c,v 1.3 1996/09/11 20:35:14 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.3 1996/09/11 20:35:14 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 <fstab.h>
     46 #include <errno.h>
     47 #if __STDC__
     48 #include <stdarg.h>
     49 #else
     50 #include <varargs.h>
     51 #endif
     52 
     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 char *fname;		/* filesystem currently checked */
     61 
     62 static char *rawname __P((const char *));
     63 
     64 static void
     65 usage()
     66 {
     67 	errexit("Usage: fsck_msdos [-pny] filesystem ... \n");
     68 }
     69 
     70 int
     71 main(argc, argv)
     72 	int argc;
     73 	char **argv;
     74 {
     75 	extern int optind;
     76 	int ret = 0, erg;
     77 	int ch;
     78 
     79 	while ((ch = getopt(argc, argv, "vpyn")) != EOF) {
     80 		switch (ch) {
     81 		case 'n':
     82 			alwaysno = 1;
     83 			alwaysyes = preen = 0;
     84 			break;
     85 		case 'y':
     86 			alwaysyes = 1;
     87 			alwaysno = preen = 0;
     88 			break;
     89 
     90 		case 'p':
     91 			preen = 1;
     92 			alwaysyes = alwaysno = 0;
     93 			break;
     94 
     95 		default:
     96 			usage();
     97 			break;
     98 		}
     99 	}
    100 	argc -= optind;
    101 	argv += optind;
    102 
    103 #define	BADTYPE(type)							\
    104 	(strcmp(type, FSTAB_RO) &&					\
    105 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    106 
    107 	if (argc == 0) {
    108 		struct fstab *fs;
    109 
    110 		if (!preen)
    111 			usage();
    112 
    113 		while ((fs = getfsent()) != NULL) {
    114 
    115 			if (fs->fs_passno == 0)
    116 				continue;
    117 
    118 			if (BADTYPE(fs->fs_type))
    119 				continue;
    120 
    121 			if (strcmp(fs->fs_vfstype, "msdos"))
    122 				continue;
    123 
    124 			erg = checkfilesys(fname = rawname(fs->fs_spec));
    125 
    126 			if (erg > ret)
    127 				ret = erg;
    128 		}
    129 
    130 	}
    131 	else {
    132 		while (argc-- > 0) {
    133 			erg = checkfilesys(fname = *argv++);
    134 			if (erg > ret)
    135 				ret = erg;
    136 		}
    137 	}
    138 
    139 	return ret;
    140 }
    141 
    142 /*VARARGS*/
    143 void
    144 #if __STDC__
    145 errexit(const char *fmt, ...)
    146 #else
    147 errexit(fmt, va_alist)
    148 	char *fmt;
    149 	va_dcl
    150 #endif
    151 {
    152 	va_list ap;
    153 
    154 #if __STDC__
    155 	va_start(ap, fmt);
    156 #else
    157 	va_start(ap);
    158 #endif
    159 	vprintf(fmt, ap);
    160 	va_end(ap);
    161 	exit(8);
    162 }
    163 
    164 /*VARARGS*/
    165 void
    166 #if __STDC__
    167 pfatal(const char *fmt, ...)
    168 #else
    169 pfatal(fmt, va_alist)
    170 	char *fmt;
    171 	va_dcl
    172 #endif
    173 {
    174 	va_list ap;
    175 
    176 	if (preen)
    177 		printf("%s: ", fname);
    178 #if __STDC__
    179 	va_start(ap, fmt);
    180 #else
    181 	va_start(ap);
    182 #endif
    183 	vprintf(fmt, ap);
    184 	va_end(ap);
    185 	printf("\n");
    186 	if (preen)
    187 		exit(8);
    188 }
    189 
    190 /*VARARGS*/
    191 void
    192 #if __STDC__
    193 pwarn(const char *fmt, ...)
    194 #else
    195 pwarn(fmt, va_alist)
    196 	char *fmt;
    197 	va_dcl
    198 #endif
    199 {
    200 	va_list ap;
    201 
    202 	if (preen)
    203 		printf("%s: ", fname);
    204 #if __STDC__
    205 	va_start(ap, fmt);
    206 #else
    207 	va_start(ap);
    208 #endif
    209 	vprintf(fmt, ap);
    210 	va_end(ap);
    211 }
    212 
    213 void
    214 perror(s)
    215 	const char *s;
    216 {
    217 	pfatal("%s (%s)", s, strerror(errno));
    218 }
    219 
    220 /*VARARGS*/
    221 int
    222 #if __STDC__
    223 ask(int def, const char *fmt, ...)
    224 #else
    225 ask(def, fmt, va_alist)
    226 	int def;
    227 	char *fmt;
    228 	va_dcl
    229 #endif
    230 {
    231 	va_list ap;
    232 
    233 	char prompt[256];
    234 	int c;
    235 
    236 	if (preen) {
    237 		if (rdonly)
    238 			def = 0;
    239 		if (def)
    240 			printf("FIXED\n");
    241 		return def;
    242 	}
    243 
    244 #if __STDC__
    245 	va_start(ap, fmt);
    246 #else
    247 	va_start(ap);
    248 #endif
    249 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
    250 	if (alwaysyes || rdonly) {
    251 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
    252 		return !rdonly;
    253 	}
    254 	do {
    255 		printf("%s? [yn] ", prompt);
    256 		fflush(stdout);
    257 		c = getchar();
    258 		while (c != '\n' && getchar() != '\n')
    259 			if (feof(stdin))
    260 				return 0;
    261 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    262 	return c == 'y' || c == 'Y';
    263 }
    264 
    265 static char *
    266 rawname(name)
    267 	const char *name;
    268 {
    269 	static char rawbuf[32];
    270 	char *dp;
    271 
    272 	if ((dp = strrchr(name, '/')) == 0)
    273 		return (0);
    274 	*dp = 0;
    275 	(void)strcpy(rawbuf, name);
    276 	*dp = '/';
    277 	(void)strcat(rawbuf, "/r");
    278 	(void)strcat(rawbuf, &dp[1]);
    279 	return (rawbuf);
    280 }
    281