]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mount: use string_to_bitmask()
authorVaclav Dolezal <vdolezal@redhat.com>
Wed, 14 Feb 2018 13:03:41 +0000 (14:03 +0100)
committerVaclav Dolezal <vdolezal@redhat.com>
Wed, 28 Mar 2018 10:26:04 +0000 (12:26 +0200)
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
sys-utils/mount.c

index 4ac14b8ab1c37968863969656a90aa3b022346d5..72e8a455ed81cc9d75e4ee44921f2c10e87056c0 100644 (file)
@@ -475,25 +475,39 @@ struct flag_str {
        char *str;
 };
 
-static int flag_from_string(const struct flag_str *flags, const char *str)
+static int omode2mask(const char *str)
 {
-       for (; flags->str; flags++) {
-               if (!strcmp(str, flags->str))
-                       return flags->value;
+       size_t i;
+
+       static const struct flag_str flags[] = {
+               { MNT_OMODE_IGNORE, "ignore" },
+               { MNT_OMODE_APPEND, "append" },
+               { MNT_OMODE_PREPEND, "prepend" },
+               { MNT_OMODE_REPLACE, "replace" },
+       };
+
+       for (i = 0; i < ARRAY_SIZE(flags); i++) {
+               if (!strcmp(str, flags[i].str))
+                       return flags[i].value;
        }
        return -EINVAL;
 }
 
-static int flags_from_string(const struct flag_str *flags, char *str)
+static long osrc2mask(const char *str, size_t len)
 {
-       int ret = 0, tmp;
-       for (str = strtok(str, ","); str; str = strtok(NULL, ",")) {
-               tmp = flag_from_string(flags, str);
-               if (tmp < 0)
-                       return tmp;
-               ret |= tmp;
+       size_t i;
+
+       static const struct flag_str flags[] = {
+               { MNT_OMODE_FSTAB, "fstab" },
+               { MNT_OMODE_MTAB, "mtab" },
+               { MNT_OMODE_NOTAB, "disable" },
+       };
+
+       for (i = 0; i < ARRAY_SIZE(flags); i++) {
+               if (!strncmp(str, flags[i].str, len) && !flags[i].str[len])
+                       return flags[i].value;
        }
-       return ret;
+       return -EINVAL;
 }
 
 int main(int argc, char **argv)
@@ -570,20 +584,6 @@ int main(int argc, char **argv)
        };
        int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
 
-       static const struct flag_str opt_optmodes[] = {
-               { MNT_OMODE_IGNORE, "ignore" },
-               { MNT_OMODE_APPEND, "append" },
-               { MNT_OMODE_PREPEND, "prepend" },
-               { MNT_OMODE_REPLACE, "replace" },
-               { 0, NULL }
-       };
-       static const struct flag_str opt_optsources[] = {
-               { MNT_OMODE_FSTAB, "fstab" },
-               { MNT_OMODE_MTAB, "mtab" },
-               { MNT_OMODE_NOTAB, "disable" },
-               { 0, NULL }
-       };
-
        sanitize_env();
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
@@ -728,19 +728,22 @@ int main(int argc, char **argv)
                        mnt_context_set_source(cxt, optarg);
                        break;
                case MOUNT_OPT_OPTMODE:
-                       optmode_mode = flag_from_string(opt_optmodes, optarg);
+                       optmode_mode = omode2mask(optarg);
                        if (optmode_mode == -EINVAL) {
                                warnx(_("bad usage"));
                                errtryhelp(MNT_EX_USAGE);
                        }
                        break;
                case MOUNT_OPT_OPTSRC:
-                       optmode_src = flags_from_string(opt_optsources, optarg);
-                       if (optmode_src == -EINVAL) {
+               {
+                       unsigned long tmp = 0;
+                       if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
                                warnx(_("bad usage"));
                                errtryhelp(MNT_EX_USAGE);
                        }
+                       optmode_src = tmp;
                        break;
+               }
                case MOUNT_OPT_OPTSRC_FORCE:
                        optmode |= MNT_OMODE_FORCE;
                        break;