main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  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. #include <sys/cdefs.h>
  28. #ifndef lint
  29. __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
  30. #endif /* not lint */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <stdarg.h>
  37. #include "fsutil.h"
  38. #include "ext.h"
  39. int alwaysno; /* assume "no" for all questions */
  40. int alwaysyes; /* assume "yes" for all questions */
  41. int preen; /* set when preening */
  42. int rdonly; /* device is opened read only (supersedes above) */
  43. int skipclean; /* skip clean file systems if preening */
  44. int allow_mmap; /* Allow the use of mmap(), if possible */
  45. static void usage(void) __dead2;
  46. static void
  47. usage(void)
  48. {
  49. fprintf(stderr, "%s\n%s\n",
  50. "usage: fsck_msdosfs -p [-f] filesystem ...",
  51. " fsck_msdosfs [-ny] filesystem ...");
  52. exit(1);
  53. }
  54. int
  55. main(int argc, char **argv)
  56. {
  57. int ret = 0, erg;
  58. int ch;
  59. skipclean = 1;
  60. allow_mmap = 1;
  61. while ((ch = getopt(argc, argv, "CfFnpyM")) != -1) {
  62. switch (ch) {
  63. case 'C': /* for fsck_ffs compatibility */
  64. break;
  65. case 'f':
  66. skipclean = 0;
  67. break;
  68. case 'F':
  69. /*
  70. * We can never run in the background. We must exit
  71. * silently with a nonzero exit code so that fsck(8)
  72. * can probe our support for -F. The exit code
  73. * doesn't really matter, but we use an unusual one
  74. * in case someone tries -F directly. The -F flag
  75. * is intentionally left out of the usage message.
  76. */
  77. exit(5);
  78. case 'n':
  79. alwaysno = 1;
  80. alwaysyes = 0;
  81. break;
  82. case 'y':
  83. alwaysyes = 1;
  84. alwaysno = 0;
  85. break;
  86. case 'p':
  87. preen = 1;
  88. break;
  89. case 'M':
  90. allow_mmap = 0;
  91. break;
  92. default:
  93. usage();
  94. break;
  95. }
  96. }
  97. argc -= optind;
  98. argv += optind;
  99. if (!argc)
  100. usage();
  101. while (--argc >= 0) {
  102. setcdevname(*argv, preen);
  103. erg = checkfilesys(*argv++);
  104. if (erg > ret)
  105. ret = erg;
  106. }
  107. return ret;
  108. }
  109. /*VARARGS*/
  110. int
  111. ask(int def, const char *fmt, ...)
  112. {
  113. va_list ap;
  114. char prompt[256];
  115. int c;
  116. if (alwaysyes || alwaysno || rdonly)
  117. def = (alwaysyes && !rdonly && !alwaysno);
  118. if (preen) {
  119. if (def)
  120. printf("FIXED\n");
  121. return def;
  122. }
  123. va_start(ap, fmt);
  124. vsnprintf(prompt, sizeof(prompt), fmt, ap);
  125. va_end(ap);
  126. if (alwaysyes || alwaysno || rdonly) {
  127. printf("%s? %s\n", prompt, def ? "yes" : "no");
  128. return def;
  129. }
  130. do {
  131. printf("%s? [yn] ", prompt);
  132. fflush(stdout);
  133. c = getchar();
  134. while (c != '\n' && getchar() != '\n')
  135. if (feof(stdin))
  136. return 0;
  137. } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
  138. return c == 'y' || c == 'Y';
  139. }