attempt to regularize atoi mess.
[oweals/busybox.git] / util-linux / mount.c
index 35776e3e0ea615f6a6313c41f3bf49672a50fb1c..531fb4520932d6736ec99d3861112d33c0d6c19e 100644 (file)
@@ -9,10 +9,6 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-/* todo:
- * bb_getopt_ulflags();
- */
-
 /* Design notes: There is no spec for mount.  Remind me to write one.
 
    mount_main() calls singlemount() which calls mount_it_now().
@@ -96,7 +92,7 @@ struct {
        {"remount", MS_REMOUNT},  // action flag
 };
 
-
+#define VECTOR_SIZE(v) (sizeof(v) / sizeof((v)[0]))
 
 /* Append mount options to string */
 static void append_mount_options(char **oldopts, char *newopts)
@@ -143,7 +139,7 @@ static int parse_mount_options(char *options, char **unrecognized)
                if (comma) *comma = 0;
 
                // Find this option in mount_options
-               for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
+               for (i = 0; i < VECTOR_SIZE(mount_options); i++) {
                        if (!strcasecmp(mount_options[i].name, options)) {
                                long fl = mount_options[i].flags;
                                if (fl < 0) flags &= fl;
@@ -152,9 +148,7 @@ static int parse_mount_options(char *options, char **unrecognized)
                        }
                }
                // If unrecognized not NULL, append unrecognized mount options */
-               if (unrecognized
-                               && i == (sizeof(mount_options) / sizeof(*mount_options)))
-               {
+               if (unrecognized && i == VECTOR_SIZE(mount_options)) {
                        // Add it to strflags, to pass on to kernel
                        i = *unrecognized ? strlen(*unrecognized) : 0;
                        *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
@@ -228,9 +222,9 @@ static int fakeIt;
 // NB: mp->xxx fields may be trashed on exit
 static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
 {
-       int rc;
+       int rc = 0;
 
-       if (fakeIt) return 0;
+       if (fakeIt) goto mtab;
 
        // Mount, with fallback to read-only if necessary.
 
@@ -251,7 +245,7 @@ static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
 
        /* If the mount was successful, and we're maintaining an old-style
         * mtab file by hand, add the new entry to it now. */
-
+mtab:
        if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
                char *fsname;
                FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
@@ -713,7 +707,7 @@ static int daemonize(void)
        dup2(fd, 2);
        if (fd > 2) close(fd);
        setsid();
-       openlog(bb_applet_name, LOG_PID, LOG_DAEMON);
+       openlog(applet_name, LOG_PID, LOG_DAEMON);
        logmode = LOGMODE_SYSLOG;
        return 1;
 }
@@ -862,7 +856,7 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
        for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
                char *opteq = strchr(opt, '=');
                if (opteq) {
-                       int val = atoi(opteq + 1);
+                       int val = xatoi_u(opteq + 1);
                        *opteq = '\0';
                        if (!strcmp(opt, "rsize"))
                                data.rsize = val;
@@ -1427,14 +1421,14 @@ report_error:
 
 int mount_main(int argc, char **argv)
 {
-       enum { OPT_ALL = 0x8 };
+       enum { OPT_ALL = 0x10 };
 
        char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
        char *opt_o;
        const char *fstabname;
        FILE *fstab;
        int i, j, rc = 0;
-       unsigned long opt;
+       unsigned opt;
        struct mntent mtpair[2], *mtcur = mtpair;
 
        /* parse long options, like --bind and --move.  Note that -o option
@@ -1445,24 +1439,21 @@ int mount_main(int argc, char **argv)
                        append_mount_options(&cmdopts,argv[i]+2);
                } else argv[j++] = argv[i];
        }
+       argv[j] = 0;
        argc = j;
 
        // Parse remaining options
 
-       opt = bb_getopt_ulflags(argc, argv, "o:t:rwavnf", &opt_o, &fstype);
-       if (opt & 1) // -o
-               append_mount_options(&cmdopts, opt_o);
-       //if (opt & 1) // -t
-       if (opt & 2) // -r
-               append_mount_options(&cmdopts, "ro");
-       if (opt & 4) // -w
-               append_mount_options(&cmdopts, "rw");
-       //if (opt & 8) // -a
-       if (opt & 0x10) // -n
-               USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE);
-       if (opt & 0x20) // -f
-               USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE);
-       //if (opt & 0x40) // ignore -v
+       opt = getopt32(argc, argv, "o:t:rwanfvs", &opt_o, &fstype);
+       if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
+       //if (opt & 0x2) // -t
+       if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
+       if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
+       //if (opt & 0x10) // -a
+       if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
+       if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
+       //if (opt & 0x80) // -v: verbose (ignore)
+       //if (opt & 0x100) // -s: sloppy (ignore)
        argv += optind;
        argc -= optind;
 
@@ -1507,8 +1498,10 @@ int mount_main(int argc, char **argv)
                goto clean_up;
        }
 
+       i = parse_mount_options(cmdopts, 0);
+
        // If we have a shared subtree flag, don't worry about fstab or mtab.
-       i = parse_mount_options(cmdopts,0);
+
        if (ENABLE_FEATURE_MOUNT_FLAGS &&
                        (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
        {
@@ -1519,17 +1512,16 @@ int mount_main(int argc, char **argv)
 
        // Open either fstab or mtab
 
-       if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
+       if (i & MS_REMOUNT)
                fstabname = bb_path_mtab_file;
-       else fstabname="/etc/fstab";
-
+       else fstabname = "/etc/fstab";
        fstab = setmntent(fstabname,"r");
        if (!fstab)
                bb_perror_msg_and_die("cannot read %s", fstabname);
 
        // Loop through entries until we find what we're looking for.
 
-       memset(mtpair,0,sizeof(mtpair));
+       memset(mtpair, 0, sizeof(mtpair));
        for (;;) {
                struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);