Commit Diff


commit - 0e5a098ac4b42e007819f387dac06a941534cea5
commit + 8d60d668faa58a809d2f1244697a7a5e285951b7
blob - 7e6b79ae62757d1c015a7205088eafd1ddfbfaa0
blob + 18a7820985a416bc26a0f3cf0e29f47841b18123
--- compat/Makefile.am
+++ compat/Makefile.am
@@ -10,7 +10,6 @@ libopenbsd_compat_a_SOURCES =  \
 	fmt_scaled.c \
 	freezero.c \
 	getdtablecount.c \
-	getopt.c \
 	getprogname.c \
 	merge.c \
 	reallocarray.c \
@@ -32,6 +31,10 @@ if HOST_DARWIN
 libopenbsd_compat_a_SOURCES += uuid.c bsd-poll.c bsd-poll.h
 endif
 
+if !HAVE_GETOPT
+libopenbsd_compat_a_SOURCES += getopt.c
+endif
+
 if !HAVE_B64
 libopenbsd_compat_a_SOURCES += base64.c
 LDADD += $(libresolv_LIBS)
blob - bc33cb31875dc69cb26099420531a03373eb1606
blob + 8fb5fec63f8b4d8c0162285ace7b93a93e74c436
--- compat/getopt.c
+++ compat/getopt.c
@@ -29,12 +29,12 @@
 
 /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
 
+#include "got_compat.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "got_compat.h"
-
 int	BSDopterr = 1,		/* if error message should be printed */
 	BSDoptind = 1,		/* index into parent argv vector */
 	BSDoptopt,		/* character checked for validity */
blob - f77680096ad1835f084e287eb76deab4463f3a4e
blob + 86591a8da124685a8adaecf14d50e848f5747988
--- configure.ac
+++ configure.ac
@@ -58,6 +58,7 @@ fi
 # Checks for header files.
 AC_CHECK_HEADERS([ \
 	fcntl.h \
+	getopt.h \
 	langinfo.h \
 	libutil.h \
 	limits.h \
@@ -129,7 +130,22 @@ AC_CHECK_DECL(
 )
 
 AM_CONDITIONAL([HAVE_SHA2], [test "x$ac_cv_header_sha2_h" = xyes])
+
+AC_CACHE_CHECK([whether getopt has optreset support],
+		ac_cv_have_getopt_optreset, [
+	AC_LINK_IFELSE([AC_LANG_PROGRAM([[ #include <getopt.h> ]],
+		[[ extern int optreset; optreset = 0; ]])],
+	[ ac_cv_have_getopt_optreset="yes" ],
+	[ ac_cv_have_getopt_optreset="no"
+	])
+])
 
+AM_CONDITIONAL([HAVE_GETOPT], [test "x$ac_cv_have_getopt_optreset" = "xyes"])
+if test "x$ac_cv_have_getopt_optreset" = "xyes" ; then
+	AC_DEFINE([HAVE_GETOPT_OPTRESET], [1],
+		[Define if your getopt(3) defines and uses optreset])
+fi
+
 AC_CHECK_MEMBERS([struct pollfd.fd], [], [], [[
 #include <sys/types.h>
 #ifdef HAVE_POLL_H
blob - ec546e4b8d53c0c0e01752af08638c7ca5683bfd
blob + 68c02fe36df0f00abffe293ccae6d27b225b6297
--- include/got_compat2.h
+++ include/got_compat2.h
@@ -390,7 +390,7 @@ int scan_scaled(char *, long long *);
 #define FMT_SCALED_STRSIZE	7  /* minus sign, 4 digits, suffix, null byte */
 #endif
 
-#ifndef HAVE_LIBBSD
+#if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
 /* getopt.c */
 extern int	BSDopterr;
 extern int	BSDoptind;