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

