1 /* $OpenBSD: main.c,v 1.8 1997/07/25 19:13:15 mickey Exp $ */ 2 /* $NetBSD: main.c,v 1.8 1996/10/17 20:29:53 cgd Exp $ */ 3 4 /* 5 * Copyright (C) 1995 Wolfgang Solfrank 6 * Copyright (c) 1995 Martin Husemann 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Martin Husemann 19 * and Wolfgang Solfrank. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 37 #ifndef lint 38 static char rcsid[] = "$OpenBSD: main.c,v 1.8 1997/07/25 19:13:15 mickey Exp $"; 39 #endif /* not lint */ 40 41 #include <stdlib.h> 42 #include <string.h> 43 #include <ctype.h> 44 #include <stdio.h> 45 #include <unistd.h> 46 #include <errno.h> 47 #ifdef __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 static void usage __P((void)); 61 int main __P((int, char **)); 62 63 static void 64 usage() 65 { 66 errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n"); 67 } 68 69 int 70 main(argc, argv) 71 int argc; 72 char **argv; 73 { 74 extern int optind; 75 int ret = 0, erg; 76 int ch; 77 78 while ((ch = getopt(argc, argv, "pynf")) != -1) { 79 switch (ch) { 80 case 'f': 81 /* Ignore for consistency with fsck_ffs */ 82 break; 83 84 case 'n': 85 alwaysno = 1; 86 alwaysyes = preen = 0; 87 break; 88 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 default: 100 usage(); 101 break; 102 } 103 } 104 argc -= optind; 105 argv += optind; 106 107 if (!argc) 108 usage(); 109 110 while (argc-- > 0) { 111 setcdevname(*argv, preen); 112 erg = checkfilesys(*argv++); 113 if (erg > ret) 114 ret = erg; 115 } 116 exit(ret); 117 } 118 119 /*VARARGS*/ 120 int 121 #ifdef __STDC__ 122 ask(int def, const char *fmt, ...) 123 #else 124 ask(def, fmt, va_alist) 125 int def; 126 char *fmt; 127 va_dcl 128 #endif 129 { 130 va_list ap; 131 132 char prompt[256]; 133 int c; 134 135 if (preen) { 136 if (rdonly) 137 def = 0; 138 if (def) 139 printf("FIXED\n"); 140 return (def); 141 } 142 143 #ifdef __STDC__ 144 va_start(ap, fmt); 145 #else 146 va_start(ap); 147 #endif 148 vsnprintf(prompt, sizeof(prompt), fmt, ap); 149 if (alwaysyes || rdonly) { 150 printf("%s? %s\n", prompt, rdonly ? "no" : "yes"); 151 return (!rdonly); 152 } 153 do { 154 printf("%s? [yn] ", prompt); 155 fflush(stdout); 156 c = getchar(); 157 while (c != '\n' && getchar() != '\n') 158 if (feof(stdin)) 159 return (0); 160 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); 161 return (c == 'y' || c == 'Y'); 162 } 163