hush: add support for special vars in braces
[oweals/busybox.git] / e2fsprogs / fsck.c
index 0707d295ddc00a2fc8a9d5b6c53ebff3531d7424..3c6cafbd3f2b1ddee90e2306ef2d682fb872151d 100644 (file)
  * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
  *      2001, 2002, 2003, 2004, 2005 by  Theodore Ts'o.
  *
- * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
- * %End-Header%
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
  */
 
 /* All filesystem specific hooks have been removed.
@@ -280,9 +277,11 @@ static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
        fs = xzalloc(sizeof(*fs));
        fs->device = xstrdup(device);
        fs->mountpt = xstrdup(mntpnt);
+       if (strchr(type, ','))
+               type = (char *)"auto";
        fs->type = xstrdup(type);
        fs->opts = xstrdup(opts ? opts : "");
-       fs->passno = passno;
+       fs->passno = passno < 0 ? 1 : passno;
        /*fs->flags = 0; */
        /*fs->next = NULL; */
 
@@ -295,130 +294,29 @@ static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
        return fs;
 }
 
-static void strip_line(char *line)
-{
-       char *p = line + strlen(line) - 1;
-
-       while (*line) {
-               if (*p != '\n' && *p != '\r')
-                       break;
-               *p-- = '\0';
-       }
-}
-
-static char *parse_word(char **buf)
-{
-       char *word, *next;
-
-       word = *buf;
-       if (*word == '\0')
-               return NULL;
-
-       word = skip_whitespace(word);
-       next = skip_non_whitespace(word);
-       if (*next)
-               *next++ = '\0';
-       *buf = next;
-       return word;
-}
-
-static void parse_escape(char *word)
-{
-       char *q, c;
-       const char *p;
-
-       if (!word)
-               return;
-
-       for (p = q = word; *p; q++) {
-               c = *p++;
-               if (c != '\\') {
-                       *q = c;
-               } else {
-                       *q = bb_process_escape_sequence(&p);
-               }
-       }
-       *q = '\0';
-}
-
-static int parse_fstab_line(char *line, struct fs_info **ret_fs)
-{
-       char *device, *mntpnt, *type, *opts, *passno, *cp;
-       struct fs_info *fs;
-
-       *ret_fs = NULL;
-       strip_line(line);
-       *strchrnul(line, '#') = '\0'; /* Ignore everything after comment */
-       cp = line;
-
-       device = parse_word(&cp);
-       if (!device) return 0; /* Allow blank lines */
-       mntpnt = parse_word(&cp);
-       type = parse_word(&cp);
-       opts = parse_word(&cp);
-       /*freq =*/ parse_word(&cp);
-       passno = parse_word(&cp);
-
-       if (!mntpnt || !type)
-               return -1;
-
-       parse_escape(device);
-       parse_escape(mntpnt);
-       parse_escape(type);
-       parse_escape(opts);
-       parse_escape(passno);
-
-       if (strchr(type, ','))
-               type = NULL;
-
-       fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
-                       (passno ? atoi(passno) : -1));
-       *ret_fs = fs;
-       return 0;
-}
-
 /* Load the filesystem database from /etc/fstab */
 static void load_fs_info(const char *filename)
 {
-       FILE *f;
-       int lineno = 0;
-       int old_fstab = 1;
+       FILE *fstab;
+       struct mntent mte;
        struct fs_info *fs;
 
-       f = fopen_or_warn(filename, "r");
-       if (f == NULL) {
+       fstab = setmntent(filename, "r");
+       if (!fstab) {
+               bb_perror_msg("cannot read %s", filename);
                return;
        }
-       while (1) {
-               int r;
-               char *buf = xmalloc_fgetline(f);
-               if (!buf) break;
-               r = parse_fstab_line(buf, &fs);
-               free(buf);
-               lineno++;
-               if (r < 0) {
-                       bb_error_msg("WARNING: bad format "
-                               "on line %d of %s", lineno, filename);
-                       continue;
-               }
-               if (!fs)
-                       continue;
-               if (fs->passno < 0)
-                       fs->passno = 0;
-               else
-                       old_fstab = 0;
-       }
-       fclose(f);
 
-       if (old_fstab) {
-               fputs("\007"
-"WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
-"I will kludge around things for you, but you should fix\n"
-"your /etc/fstab file as soon as you can.\n\n", stderr);
-               for (fs = filesys_info; fs; fs = fs->next) {
-                       fs->passno = 1;
-               }
+       // Loop through entries
+       while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
+               //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
+               //      mte.mnt_type, mte.mnt_opts,
+               //      mte.mnt_passno);
+               fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
+                       mte.mnt_type, mte.mnt_opts,
+                       mte.mnt_passno);
        }
+       endmntent(fstab);
 }
 
 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
@@ -1028,13 +926,13 @@ static void parse_args(char **argv)
 // FIXME: must check that arg is a blkdev, or resolve
 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
-                       devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
+                       devices = xrealloc_vector(devices, 2, num_devices);
                        devices[num_devices++] = xstrdup(arg);
                        continue;
                }
 
                if (arg[0] != '-' || opts_for_fsck) {
-                       args = xrealloc(args, (num_args+1) * sizeof(args[0]));
+                       args = xrealloc_vector(args, 2, num_args);
                        args[num_args++] = xstrdup(arg);
                        continue;
                }
@@ -1111,7 +1009,7 @@ static void parse_args(char **argv)
                if (optpos) {
                        options[0] = '-';
                        options[optpos + 1] = '\0';
-                       args = xrealloc(args, (num_args+1) * sizeof(args[0]));
+                       args = xrealloc_vector(args, 2, num_args);
                        args[num_args++] = options;
                }
        }
@@ -1122,13 +1020,13 @@ static void parse_args(char **argv)
                max_running = xatoi(tmp);
 }
 
-static void signal_cancel(int sig ATTRIBUTE_UNUSED)
+static void signal_cancel(int sig UNUSED_PARAM)
 {
        cancel_requested = 1;
 }
 
 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int fsck_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int fsck_main(int argc UNUSED_PARAM, char **argv)
 {
        int i, status;
        /*int interactive;*/