Home | History | Annotate | Line # | Download | only in fsck_msdos
main.c revision 1.22
      1 /*	$NetBSD: main.c,v 1.22 2010/04/11 08:23:52 hannken 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __RCSID("$NetBSD: main.c,v 1.22 2010/04/11 08:23:52 hannken Exp $");
     32 #endif /* not lint */
     33 
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <stdio.h>
     37 #include <unistd.h>
     38 #include <err.h>
     39 #include <errno.h>
     40 #include <stdarg.h>
     41 #include <signal.h>
     42 
     43 #include "fsutil.h"
     44 #include "snapshot.h"
     45 #include "ext.h"
     46 #include "exitvalues.h"
     47 
     48 int alwaysno;		/* assume "no" for all questions */
     49 int alwaysyes;		/* assume "yes" for all questions */
     50 int preen;		/* set when preening */
     51 int rdonly;		/* device is opened read only (supersedes above) */
     52 
     53 static void usage(void) __dead;
     54 
     55 static void
     56 usage(void)
     57 {
     58     	(void)fprintf(stderr,
     59 	    "Usage: %s [-fnpy] [-x snap_backup] filesystem ... \n",
     60 	    getprogname());
     61 	exit(FSCK_EXIT_USAGE);
     62 }
     63 
     64 static void
     65 catch(int n)
     66 {
     67 	exit(FSCK_EXIT_SIGNALLED);
     68 }
     69 
     70 int
     71 main(int argc, char **argv)
     72 {
     73 	int ret = FSCK_EXIT_OK, erg;
     74 	int ch, snapfd;
     75 	char *snap_backup = NULL, *snap_dev;
     76 
     77 	while ((ch = getopt(argc, argv, "pPqynfx:")) != -1) {
     78 		switch (ch) {
     79 		case 'f':
     80 			/*
     81 			 * We are always forced, since we don't
     82 			 * have a clean flag
     83 			 */
     84 			break;
     85 		case 'n':
     86 			alwaysno = 1;
     87 			alwaysyes = preen = 0;
     88 			break;
     89 		case 'y':
     90 			alwaysyes = 1;
     91 			alwaysno = preen = 0;
     92 			break;
     93 
     94 		case 'p':
     95 			preen = 1;
     96 			alwaysyes = alwaysno = 0;
     97 			break;
     98 
     99 		case 'P':		/* Progress meter not implemented. */
    100 			break;
    101 
    102 		case 'q':		/* Quiet not implemented. */
    103 			break;
    104 
    105 		case 'x':
    106 			snap_backup = optarg;
    107 			break;
    108 
    109 		default:
    110 			usage();
    111 			break;
    112 		}
    113 	}
    114 	if (snap_backup != NULL && (!alwaysno || alwaysyes)) {
    115 		warnx("Cannot use -x without -n\n");
    116 		snap_backup = NULL;
    117 	}
    118 	argc -= optind;
    119 	argv += optind;
    120 
    121 	if (!argc)
    122 		usage();
    123 
    124 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    125 		(void) signal(SIGINT, catch);
    126 	if (preen)
    127 		(void) signal(SIGQUIT, catch);
    128 
    129 	while (--argc >= 0) {
    130 		setcdevname(*argv, preen);
    131 		if (snap_backup != NULL) {
    132 			snapfd = snap_open(*argv, snap_backup, NULL, &snap_dev);
    133 			if (snapfd < 0) {
    134 				warn("can't take snapshot of %s", *argv);
    135 				erg = checkfilesys(*argv);
    136 			} else {
    137 				erg = checkfilesys(snap_dev);
    138 				close(snapfd);
    139 			}
    140 			argv++;
    141 		} else
    142 			erg = checkfilesys(*argv++);
    143 		if (erg > ret)
    144 			ret = erg;
    145 	}
    146 
    147 	return ret;
    148 }
    149 
    150 
    151 /*VARARGS*/
    152 int
    153 ask(int def, const char *fmt, ...)
    154 {
    155 	va_list ap;
    156 
    157 	char prompt[256];
    158 	int c;
    159 
    160 	if (preen) {
    161 		if (rdonly)
    162 			def = 0;
    163 		if (def)
    164 			printf("FIXED\n");
    165 		return def;
    166 	}
    167 
    168 	va_start(ap, fmt);
    169 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
    170 	va_end(ap);
    171 	if (alwaysyes || rdonly) {
    172 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
    173 		return !rdonly;
    174 	}
    175 	do {
    176 		printf("%s? [yn] ", prompt);
    177 		fflush(stdout);
    178 		c = getchar();
    179 		while (c != '\n' && getchar() != '\n')
    180 			if (feof(stdin))
    181 				return 0;
    182 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    183 	return c == 'y' || c == 'Y';
    184 }
    185