xref: /openbsd-src/sbin/fsck_msdos/main.c (revision c91129808d7b6e89d9eda0dac677825779614b86)
1*c9112980Sderaadt /*	$OpenBSD: main.c,v 1.24 2018/09/24 21:26:02 deraadt Exp $	*/
2a0dbeb59Smillert /*	$NetBSD: main.c,v 1.8 1996/10/17 20:29:53 cgd Exp $	*/
39646ab25Sderaadt 
49646ab25Sderaadt /*
59646ab25Sderaadt  * Copyright (C) 1995 Wolfgang Solfrank
69646ab25Sderaadt  * Copyright (c) 1995 Martin Husemann
79646ab25Sderaadt  *
89646ab25Sderaadt  * Redistribution and use in source and binary forms, with or without
99646ab25Sderaadt  * modification, are permitted provided that the following conditions
109646ab25Sderaadt  * are met:
119646ab25Sderaadt  * 1. Redistributions of source code must retain the above copyright
129646ab25Sderaadt  *    notice, this list of conditions and the following disclaimer.
139646ab25Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
149646ab25Sderaadt  *    notice, this list of conditions and the following disclaimer in the
159646ab25Sderaadt  *    documentation and/or other materials provided with the distribution.
169646ab25Sderaadt  *
179646ab25Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
189646ab25Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199646ab25Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209646ab25Sderaadt  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
219646ab25Sderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229646ab25Sderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239646ab25Sderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249646ab25Sderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259646ab25Sderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269646ab25Sderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279646ab25Sderaadt  */
289646ab25Sderaadt 
299646ab25Sderaadt 
309646ab25Sderaadt #include <stdlib.h>
319646ab25Sderaadt #include <string.h>
329646ab25Sderaadt #include <ctype.h>
339646ab25Sderaadt #include <stdio.h>
349646ab25Sderaadt #include <unistd.h>
359646ab25Sderaadt #include <errno.h>
369646ab25Sderaadt #include <stdarg.h>
37e384f6efSderaadt #include <err.h>
389646ab25Sderaadt 
399646ab25Sderaadt #include "ext.h"
409646ab25Sderaadt 
419646ab25Sderaadt int alwaysno;		/* assume "no" for all questions */
429646ab25Sderaadt int alwaysyes;		/* assume "yes" for all questions */
439646ab25Sderaadt int preen;		/* set when preening */
449646ab25Sderaadt int rdonly;		/* device is opened read only (supersedes above) */
459646ab25Sderaadt 
46c72b5b24Smillert static void usage(void);
47c72b5b24Smillert int main(int, char **);
489646ab25Sderaadt 
499646ab25Sderaadt static void
usage(void)504e95fccfSderaadt usage(void)
519646ab25Sderaadt {
52fd4c6d5dSderaadt 	errexit("usage: fsck_msdos [-fnpy] filesystem\n");
539646ab25Sderaadt }
549646ab25Sderaadt 
559646ab25Sderaadt int
main(int argc,char * argv[])564e95fccfSderaadt main(int argc, char *argv[])
579646ab25Sderaadt {
589646ab25Sderaadt 	int ch;
599646ab25Sderaadt 
60*c9112980Sderaadt 	checkroot();
61*c9112980Sderaadt 
62a0dbeb59Smillert 	while ((ch = getopt(argc, argv, "pynf")) != -1) {
639646ab25Sderaadt 		switch (ch) {
64a0dbeb59Smillert 		case 'f':
65a0dbeb59Smillert 			/* Ignore for consistency with fsck_ffs */
66a0dbeb59Smillert 			break;
67a0dbeb59Smillert 
689646ab25Sderaadt 		case 'n':
699646ab25Sderaadt 			alwaysno = 1;
709646ab25Sderaadt 			alwaysyes = preen = 0;
719646ab25Sderaadt 			break;
72a0dbeb59Smillert 
739646ab25Sderaadt 		case 'y':
749646ab25Sderaadt 			alwaysyes = 1;
759646ab25Sderaadt 			alwaysno = preen = 0;
769646ab25Sderaadt 			break;
779646ab25Sderaadt 
789646ab25Sderaadt 		case 'p':
799646ab25Sderaadt 			preen = 1;
809646ab25Sderaadt 			alwaysyes = alwaysno = 0;
819646ab25Sderaadt 			break;
829646ab25Sderaadt 
839646ab25Sderaadt 		default:
849646ab25Sderaadt 			usage();
859646ab25Sderaadt 			break;
869646ab25Sderaadt 		}
879646ab25Sderaadt 	}
889646ab25Sderaadt 	argc -= optind;
899646ab25Sderaadt 	argv += optind;
909646ab25Sderaadt 
91fd4c6d5dSderaadt 	if (argc != 1)
929646ab25Sderaadt 		usage();
939646ab25Sderaadt 
94e729ad4aSjsing 	setcdevname(*argv, NULL, preen);
95fd4c6d5dSderaadt 	exit (checkfilesys(blockcheck(*argv)));
969646ab25Sderaadt }
979646ab25Sderaadt 
989646ab25Sderaadt int
ask(int def,const char * fmt,...)999646ab25Sderaadt ask(int def, const char *fmt, ...)
1009646ab25Sderaadt {
1019646ab25Sderaadt 	va_list ap;
1029646ab25Sderaadt 
1039646ab25Sderaadt 	char prompt[256];
1049646ab25Sderaadt 	int c;
1059646ab25Sderaadt 
1069646ab25Sderaadt 	if (preen) {
1079646ab25Sderaadt 		if (rdonly)
1089646ab25Sderaadt 			def = 0;
1099646ab25Sderaadt 		if (def)
1109646ab25Sderaadt 			printf("FIXED\n");
111a0dbeb59Smillert 		return (def);
1129646ab25Sderaadt 	}
1139646ab25Sderaadt 
1149646ab25Sderaadt 	va_start(ap, fmt);
1159646ab25Sderaadt 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
11624766c54Sderaadt 	va_end(ap);
1179646ab25Sderaadt 	if (alwaysyes || rdonly) {
1189646ab25Sderaadt 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
119a0dbeb59Smillert 		return (!rdonly);
1209646ab25Sderaadt 	}
1219646ab25Sderaadt 	do {
122daaa95b6Sderaadt 		printf("%s? [Fyn] ", prompt);
1239646ab25Sderaadt 		fflush(stdout);
1249646ab25Sderaadt 		c = getchar();
125daaa95b6Sderaadt 		if (c == 'F') {
126daaa95b6Sderaadt 			alwaysyes = 1;
127daaa95b6Sderaadt 			return (1);
128daaa95b6Sderaadt 		}
1299646ab25Sderaadt 		while (c != '\n' && getchar() != '\n')
1309646ab25Sderaadt 			if (feof(stdin))
131a0dbeb59Smillert 				return (0);
1329646ab25Sderaadt 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
133a0dbeb59Smillert 	return (c == 'y' || c == 'Y');
1349646ab25Sderaadt }
135