commit 62f363ce7bf30bd7855ef9d7a54628f64322926e from: Thomas Adam via: Thomas Adam date: Wed Mar 15 22:00:29 2023 UTC portable: fix getopt check Rather than always asserting our version of getopt(2), only do so if it lacks certain features. Some of the configure.ac changes have come from openssh-portable. commit - 0e5a098ac4b42e007819f387dac06a941534cea5 commit + 62f363ce7bf30bd7855ef9d7a54628f64322926e 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 #include #include -#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 + fb68b072a97da873e45ce9600c4f81815796c65d --- 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 ]], + [[ 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" = "xno"]) +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 #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;