]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
treewide: fix optional arguments usage
authorKarel Zak <kzak@redhat.com>
Mon, 7 Apr 2025 11:49:43 +0000 (13:49 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 7 Apr 2025 12:38:45 +0000 (14:38 +0200)
In some parts of the code, the optional argument handling is missing
for cases where the argument starts with '='. This is particularly
important for short options with optional arguments, as suggested by
our man pages. The libc getopt_long() handles this for long options,
but for short options, it's our responsibility.

Note that some argument parsing functions (mostly colormode_or_err())
already implement this, as they are usually used with optional
arguments.

Signed-off-by: Karel Zak <kzak@redhat.com>
lsfd-cmd/lsfd.c
misc-utils/enosys.c
misc-utils/uuidd.c
misc-utils/wipefs.c
sys-utils/ipcrm.c
sys-utils/nsenter.c

index bc41e20e16a6ec3db2ca118a2ebe7303b39c7cdb..2eb53935c9a6fba72108204d59be83a64e9c4a24 100644 (file)
@@ -2600,6 +2600,8 @@ int main(int argc, char *argv[])
                        const char *subexpr = NULL;
 
                        ctl.sockets_only = 1;
+                       if (optarg && *optarg == '=')
+                               optarg++;
                        if (optarg == NULL)
                                subexpr = inet46_subexpr;
                        else if (strcmp(optarg, "4") == 0)
index b7469c6bf76f452c9647a77d65426ab772b3008a..2d3d833d49d3a4c6ed8a93a49a96b6504152c126 100644 (file)
@@ -207,6 +207,8 @@ int main(int argc, char **argv)
                        return EXIT_SUCCESS;
                case 'd':
                        if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
                                dump = fopen(optarg, "w");
                                if (!dump)
                                        err(EXIT_FAILURE, _("Could not open %s"), optarg);
index 79992e309d694db766fd690131b5df8f45d491b6..961c3a5fad0ed76e469cb56b3cb81ea4dcdaa836 100644 (file)
@@ -627,9 +627,11 @@ static void parse_options(int argc, char **argv, struct uuidd_cxt_t *uuidd_cxt,
                err_exclusive_options(c, longopts, excl, excl_st);
                switch (c) {
                case 'C':
-                       if (optarg != NULL)
+                       if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
                                uuidd_cxt->cont_clock_offset = parse_cont_clock(optarg);
-                       else
+                       else
                                uuidd_cxt->cont_clock_offset = 7200; /* default 2h */
                        break;
                case 'd':
index 4bd2e5864fbeb10ecfc166054478ac6e08da0bb7..04d8ae8c22336f5496273186bc4ded8fa17eda73 100644 (file)
@@ -711,6 +711,8 @@ main(int argc, char **argv)
                        break;
                case 'b':
                        if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
                                ctl.backup = optarg;
                        } else {
                                ctl.backup = getenv("HOME");
index 8ddb104f44a7fcf3e916bbaf3ade15340e1c74a5..6cad9d9918bc8deafbc29cf3f8cc7ee25f4d6b69 100644 (file)
@@ -498,6 +498,8 @@ int main(int argc, char **argv)
                case 'a':
                        rm_all = 1;
                        if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
                                if (!strcmp(optarg, "shm"))
                                        what_all = SHM;
                                else if (!strcmp(optarg, "pshm"))
index 136931a9c000ac486cd9ed49bbe375910d7d00b7..cf6c831744b45b64245ef7e9a0aa920e5c7a86f9 100644 (file)
@@ -196,9 +196,11 @@ static void enable_namespace(int nstype, const char *path)
 {
        struct namespace_file *nsfile = get_nsfile(nstype);
 
-       if (nsfile)
+       if (nsfile) {
+               if (path && *path == '=')       /* used in getopt_long() block */
+                       path++;
                enable_nsfile(nsfile, path);
-       else
+       else
                assert(nsfile);
 }
 
@@ -620,12 +622,16 @@ int main(int argc, char *argv[])
                                do_rd = true;
                        break;
                case 'w':
-                       if (optarg)
+                       if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
                                open_target_fd(&wd_fd, "cwd", optarg);
-                       else
+                       else
                                do_wd = true;
                        break;
                case 'W':
+                       if (optarg && *optarg == '=')
+                               optarg++;
                        wdns = optarg;
                        break;
                case 'e':