summaryrefslogtreecommitdiff
path: root/backport-support-for-POSIX-getopt-behaviour.patch
blob: aa3c2c74425ff3c99cc81a01c70da7c686ee43e9 (plain)
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
From 1f47b1cc0eddbb1921d81249a4bd604089c71495 Mon Sep 17 00:00:00 2001
From: "(GalaxyMaster)" <galaxy4public@users.noreply.github.com>
Date: Tue, 31 Jan 2023 18:24:55 +1100
Subject: [PATCH] support for POSIX getopt() behaviour

[POSIX defines optarg only for options with arguments](https://pubs.opengroup.org/onlinepubs/000095399/functions/getopt.html) and callback() is expecting optarg to be NULL for options without arguments, however, at least on musl optarg will carry a pointer to the argument of the previous option with argument.  This commit makes the behaviour deterministic and expected.
---
 rpmio/rgetopt.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/rpmio/rgetopt.c b/rpmio/rgetopt.c
index f789fa8fe..b14366a8a 100644
--- a/rpmio/rgetopt.c
+++ b/rpmio/rgetopt.c
@@ -28,6 +28,7 @@ int rgetopt(int argc, char * const argv[], const char *opts,
     optind = 0;
 #else
     optind = 1;
+    optarg = NULL;
 #endif
 
     while ((c = getopt(argc, argv, opts)) != -1) {
@@ -39,6 +40,7 @@ int rgetopt(int argc, char * const argv[], const char *opts,
 	    rc = -1;
 	    break;
 	}
+	optarg = NULL;
     }
     return (rc < 0) ? -optopt : optind;
 }
-- 
2.27.0