]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/fsck.minix.c
fsck.minix: use rpmatch() for yes/no question
[thirdparty/util-linux.git] / disk-utils / fsck.minix.c
index 0c6cfa4c59d90d2a36d6c7f446fe63617f0cbd74..4aeaf743b24c5a78d8751039c43ed90bb22701dd 100644 (file)
 #include <sys/stat.h>
 #include <signal.h>
 
+#include "exitcodes.h"
 #include "minix_programs.h"
 #include "nls.h"
 #include "pathnames.h"
 #include "writeall.h"
 
 #define ROOT_INO 1
+#define YESNO_LENGTH 64
 
 /*
  * Global variables used in minix_programs.h inline fuctions
@@ -195,7 +197,7 @@ usage(void) {
        fprintf(stderr,
                _("Usage: %s [-larvsmf] /dev/name\n"),
                program_name);
-       leave(16);
+       leave(FSCK_EX_USAGE);
 }
 
 static void die(const char *fmt, ...)
@@ -210,7 +212,7 @@ die(const char *fmt, ...) {
        vfprintf(stderr, fmt, ap);
        va_end (ap);
        fputc('\n', stderr);
-       leave(8);
+       leave(FSCK_EX_ERROR);
 }
 
 /*
@@ -237,7 +239,8 @@ get_current_name(void) {
 
 static int
 ask(const char * string, int def) {
-       int c;
+       int resp;
+       char input[YESNO_LENGTH];
 
        if (!repair) {
                printf("\n");
@@ -250,30 +253,30 @@ ask(const char * string, int def) {
                      errors_uncorrected = 1;
                return def;
        }
-       printf(def?"%s (y/n)? ":"%s (n/y)? ",string);
-       for (;;) {
-               fflush(stdout);
-               if ((c=getchar())==EOF) {
-                       if (!def)
-                             errors_uncorrected = 1;
-                       return def;
-               }
-               c=toupper(c);
-               if (c == 'Y') {
-                       def = 1;
-                       break;
-               } else if (c == 'N') {
-                       def = 0;
-                       break;
-               } else if (c == ' ' || c == '\n')
-                       break;
+       /* TRANSLATORS: these yes no questions uses rpmatch(), and should be
+        * translated.  */
+       printf(def ? _("%s (y/n)? ") : _("%s (n/y)? "), string);
+       fflush(stdout);
+       fgets(input, YESNO_LENGTH, stdin);
+       resp = rpmatch(input);
+       switch (resp) {
+       case -1:
+               /* def = def */
+               break;
+       case 0:
+       case 1:
+               def = resp;
+               break;
+       default:
+               /* rpmatch bug? */
+               abort();
        }
        if (def)
-               printf("y\n");
+               printf(_("y\n"));
        else {
-               printf("n\n");
+               printf(_("n\n"));
                errors_uncorrected = 1;
-            }
+       }
        return def;
 }
 
@@ -297,7 +300,7 @@ check_mount(void)
                cont = 0;
        if (!cont) {
                printf (_("check aborted.\n"));
-               exit (0);
+               exit (FSCK_EX_OK);
        }
        return;
 }
@@ -609,17 +612,15 @@ read_tables(void) {
        if (!inode_map)
                die(_("Unable to allocate buffer for inode map"));
        zone_map = malloc(zmaps * MINIX_BLOCK_SIZE);
-       if (!inode_map)
+       if (!zone_map)
                die(_("Unable to allocate buffer for zone map"));
-       memset(inode_map,0,sizeof(inode_map));
-       memset(zone_map,0,sizeof(zone_map));
        inode_buffer = malloc(buffsz);
        if (!inode_buffer)
                die(_("Unable to allocate buffer for inodes"));
-       inode_count = malloc(inodes + 1);
+       inode_count = calloc(1, inodes + 1);
        if (!inode_count)
                die(_("Unable to allocate buffer for inode count"));
-       zone_count = malloc(zones);
+       zone_count = calloc(1, zones);
        if (!zone_count)
                die(_("Unable to allocate buffer for zone count"));
 
@@ -1270,7 +1271,7 @@ main(int argc, char ** argv) {
        if (argc == 2 &&
            (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
                printf(_("%s (%s)\n"), program_name, PACKAGE_STRING);
-               exit(0);
+               exit(FSCK_EX_OK);
        }
 
        if (INODE_SIZE * MINIX_INODES_PER_BLOCK != MINIX_BLOCK_SIZE)
@@ -1306,7 +1307,7 @@ main(int argc, char ** argv) {
        }
        IN = open(device_name,repair?O_RDWR:O_RDONLY);
        if (IN < 0)
-               die(_("unable to open '%s': %s"), device_name, strerror(errno));
+               die(_("unable to open '%s': %m"), device_name);
        for (count=0 ; count<3 ; count++)
                sync();
        read_superblock();