1 /* $OpenBSD: main.c,v 1.12 2002/02/19 19:39:38 millert 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.12 2002/02/19 19:39:38 millert 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 #include <stdarg.h> 48 49 #include "ext.h" 50 51 int alwaysno; /* assume "no" for all questions */ 52 int alwaysyes; /* assume "yes" for all questions */ 53 int preen; /* set when preening */ 54 int rdonly; /* device is opened read only (supersedes above) */ 55 56 static void usage(void); 57 int main(int, char **); 58 59 static void 60 usage() 61 { 62 errexit("Usage: fsck_msdos [-fnpy] filesystem ... \n"); 63 } 64 65 int 66 main(argc, argv) 67 int argc; 68 char **argv; 69 { 70 int ret = 0, erg; 71 int ch; 72 73 while ((ch = getopt(argc, argv, "pynf")) != -1) { 74 switch (ch) { 75 case 'f': 76 /* Ignore for consistency with fsck_ffs */ 77 break; 78 79 case 'n': 80 alwaysno = 1; 81 alwaysyes = preen = 0; 82 break; 83 84 case 'y': 85 alwaysyes = 1; 86 alwaysno = preen = 0; 87 break; 88 89 case 'p': 90 preen = 1; 91 alwaysyes = alwaysno = 0; 92 break; 93 94 default: 95 usage(); 96 break; 97 } 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (!argc) 103 usage(); 104 105 while (argc-- > 0) { 106 setcdevname(*argv, preen); 107 erg = checkfilesys(*argv++); 108 if (erg > ret) 109 ret = erg; 110 } 111 exit(ret); 112 } 113 114 /*VARARGS*/ 115 int 116 ask(int def, const char *fmt, ...) 117 { 118 va_list ap; 119 120 char prompt[256]; 121 int c; 122 123 if (preen) { 124 if (rdonly) 125 def = 0; 126 if (def) 127 printf("FIXED\n"); 128 return (def); 129 } 130 131 va_start(ap, fmt); 132 vsnprintf(prompt, sizeof(prompt), fmt, ap); 133 va_end(ap); 134 if (alwaysyes || rdonly) { 135 printf("%s? %s\n", prompt, rdonly ? "no" : "yes"); 136 return (!rdonly); 137 } 138 do { 139 printf("%s? [yn] ", prompt); 140 fflush(stdout); 141 c = getchar(); 142 while (c != '\n' && getchar() != '\n') 143 if (feof(stdin)) 144 return (0); 145 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); 146 return (c == 'y' || c == 'Y'); 147 } 148