1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
From: Karel Zak <kzak@redhat.com>
Date: Mon, 7 Apr 2025 13:49:43 +0200
Subject: treewide: fix optional arguments usage
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>
(cherry picked from commit c4a24f5301ec9779a0492dd110824c2ce813f3eb)
---
lsfd-cmd/lsfd.c | 2 ++
misc-utils/enosys.c | 2 ++
misc-utils/uuidd.c | 6 ++++--
misc-utils/wipefs.c | 2 ++
sys-utils/ipcrm.c | 2 ++
sys-utils/nsenter.c | 14 ++++++++++----
6 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/lsfd-cmd/lsfd.c b/lsfd-cmd/lsfd.c
index 78f0c2c..75cd1de 100644
--- a/lsfd-cmd/lsfd.c
+++ b/lsfd-cmd/lsfd.c
@@ -2597,6 +2597,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)
diff --git a/misc-utils/enosys.c b/misc-utils/enosys.c
index 1410676..6afb271 100644
--- a/misc-utils/enosys.c
+++ b/misc-utils/enosys.c
@@ -203,6 +203,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);
diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
index 79992e3..961c3a5 100644
--- a/misc-utils/uuidd.c
+++ b/misc-utils/uuidd.c
@@ -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':
diff --git a/misc-utils/wipefs.c b/misc-utils/wipefs.c
index 4bd2e58..04d8ae8 100644
--- a/misc-utils/wipefs.c
+++ b/misc-utils/wipefs.c
@@ -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");
diff --git a/sys-utils/ipcrm.c b/sys-utils/ipcrm.c
index 11adf2f..c47c48a 100644
--- a/sys-utils/ipcrm.c
+++ b/sys-utils/ipcrm.c
@@ -497,6 +497,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"))
diff --git a/sys-utils/nsenter.c b/sys-utils/nsenter.c
index 136931a..cf6c831 100644
--- a/sys-utils/nsenter.c
+++ b/sys-utils/nsenter.c
@@ -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':
|