commit c91524261465dbb2d4fb51dfd90ebb2b4fc196e8
parent 8273c5fa450aabafdfac1f775561620459232459
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 14:34:21 -0300
patch: getopt_reset behind a backend
Diffstat:
10 files changed, 118 insertions(+), 14 deletions(-)
diff --git a/cmd/posix/patch.c b/cmd/posix/patch.c
@@ -157,7 +157,6 @@ static char end_defined[128];
/* function declarations */
static void reinitialize_almost_everything(void);
static void get_some_switches(void);
-static void reset_getopt_state(void);
static LINENUM locate_hunk(LINENUM);
static void rej_line(int, LINENUM);
static void abort_hunk(void);
@@ -576,17 +575,6 @@ main(int argc, char *argv[])
}
static void
-reset_getopt_state(void)
-{
-#if defined(__GLIBC__) || defined(__linux__)
- optind = 0;
-#else
- optreset = 1;
- optind = 1;
-#endif
-}
-
-static void
reinitialize_almost_everything(void)
{
re_patch();
@@ -653,7 +641,7 @@ get_some_switches(void)
Argv_last = Argv;
if (!Argc)
return;
- reset_getopt_state();
+ getopt_reset();
while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
switch (ch) {
case 'b':
diff --git a/ninqu.rules b/ninqu.rules
@@ -43,6 +43,8 @@
(skip "diskutil_freebsd.c" "diskutil_linux.c" "diskutil_netbsd.c"
"diskutil_openbsd.c" "diskutil_other.c"
"filesystem_linux.c" "filesystem_other.c"
+ "getopt_darwin.c" "getopt_dragonfly.c" "getopt_freebsd.c"
+ "getopt_linux.c" "getopt_netbsd.c" "getopt_openbsd.c" "getopt_other.c"
"net_linux.c" "net_other.c"
"passwd_dragonfly.c" "passwd_freebsd.c" "passwd_linux.c"
"passwd_netbsd.c" "passwd_openbsd.c" "passwd_other.c"
@@ -61,6 +63,13 @@
"shared/libutil/diskutil_other.c"
"shared/libutil/filesystem_linux.c"
"shared/libutil/filesystem_other.c"
+ "shared/libutil/getopt_darwin.c"
+ "shared/libutil/getopt_dragonfly.c"
+ "shared/libutil/getopt_freebsd.c"
+ "shared/libutil/getopt_linux.c"
+ "shared/libutil/getopt_netbsd.c"
+ "shared/libutil/getopt_openbsd.c"
+ "shared/libutil/getopt_other.c"
"shared/libutil/net_linux.c"
"shared/libutil/net_other.c"
"shared/libutil/passwd_dragonfly.c"
@@ -74,7 +83,7 @@
"shared/libutil/sysinfo_linux.c"
"shared/libutil/sysinfo_other.c")
(dep $(MATCH) "shared/diskutil.h" "shared/filesystem.h" "shared/net.h" "shared/passwd.h"
- "shared/proc.h" "shared/sysinfo.h" FEATURES_H)
+ "shared/proc.h" "shared/sysinfo.h" "shared/util.h" FEATURES_H)
(after NOFORK_LIST_H)
(out "shared/libutil/$(BASESTEM).o")
(exec $(CC) $(CPPFLAGS_LIBUTIL) -Ishared -Ishared/libutil $(CFLAGS) -c -o $(OUT) $(MATCH)))
diff --git a/shared/libutil/getopt_darwin.c b/shared/libutil/getopt_darwin.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+#if defined(__APPLE__) && !defined(ARUU_FORCE_OTHER)
+extern int optreset;
+
+void
+getopt_reset(void)
+{
+ optreset = 1;
+ optind = 1;
+}
+#endif
diff --git a/shared/libutil/getopt_dragonfly.c b/shared/libutil/getopt_dragonfly.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+#if defined(__DragonFly__) && !defined(ARUU_FORCE_OTHER)
+extern int optreset;
+
+void
+getopt_reset(void)
+{
+ optreset = 1;
+ optind = 1;
+}
+#endif
diff --git a/shared/libutil/getopt_freebsd.c b/shared/libutil/getopt_freebsd.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+#if defined(__FreeBSD__) && !defined(ARUU_FORCE_OTHER)
+extern int optreset;
+
+void
+getopt_reset(void)
+{
+ optreset = 1;
+ optind = 1;
+}
+#endif
diff --git a/shared/libutil/getopt_linux.c b/shared/libutil/getopt_linux.c
@@ -0,0 +1,13 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+/* glibc and musl both start over when optind is 0 */
+#if defined(__linux__) && !defined(ARUU_FORCE_OTHER)
+void
+getopt_reset(void)
+{
+ optind = 0;
+}
+#endif
diff --git a/shared/libutil/getopt_netbsd.c b/shared/libutil/getopt_netbsd.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+#if defined(__NetBSD__) && !defined(ARUU_FORCE_OTHER)
+extern int optreset;
+
+void
+getopt_reset(void)
+{
+ optreset = 1;
+ optind = 1;
+}
+#endif
diff --git a/shared/libutil/getopt_openbsd.c b/shared/libutil/getopt_openbsd.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+#if defined(__OpenBSD__) && !defined(ARUU_FORCE_OTHER)
+extern int optreset;
+
+void
+getopt_reset(void)
+{
+ optreset = 1;
+ optind = 1;
+}
+#endif
diff --git a/shared/libutil/getopt_other.c b/shared/libutil/getopt_other.c
@@ -0,0 +1,15 @@
+/* see LICENSE file for copyright and license details */
+#include "../util.h"
+
+#include <unistd.h>
+
+/* the one reset posix names */
+#if !(defined(__linux__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) \
+ || defined(__OpenBSD__) || defined(__APPLE__)) \
+ || defined(ARUU_FORCE_OTHER)
+void
+getopt_reset(void)
+{
+ optind = 1;
+}
+#endif
diff --git a/shared/util.h b/shared/util.h
@@ -115,6 +115,10 @@ void recurse_dir(const char *, void (*)(const char *));
void devtotty(int, int *, int *);
int ttytostr(int, int, char *, size_t);
+/* getopt starts over on a new argument list, the way each system's libc
+ * is told to: a getopt_<os>.c per system */
+void getopt_reset(void);
+
#if FEATURE_NOEXEC || FEATURE_NOFORK
#include "wexec.h"
#define exit(code) wexec_exit(code)