1From db7fab775d03438b4cfce6b49fab2d3176ecb1d3 Mon Sep 17 00:00:00 2001
2From: Dario Binacchi <dario.binacchi@amarulasolutions.com>
3Date: Sat, 11 May 2024 19:38:01 +0200
4Subject: [PATCH] Fix getopt linking error with musl-libc
5
6The buildroot project, to which the sscep application was added, has
7configurations that raise the following linking error:
8buildroot/output/host/opt/ext-toolchain/bin/../lib/gcc/i586-buildroot-linux-musl/9.3.0/../../../../i586-buildroot-linux-musl/bin/ld: buildroot/output/host/i586-buildroot-linux-musl/sysroot/lib/libc.a(getopt.o): in function `getopt':
9getopt.c:(.text.getopt+0x0): multiple definition of `getopt'; src/getopt.o:getopt.c:(.text+0x0): first defined here
10buildroot/output/host/opt/ext-toolchain/bin/../lib/gcc/i586-buildroot-linux-musl/9.3.0/../../../../i586-buildroot-linux-musl/bin/ld: buildroot/output/host/i586-buildroot-linux-musl/sysroot/lib/libc.a(getopt.o):(.data.optind+0x0): multiple definition of `optind'; src/getopt.o:(.data+0x0): first defined here
11collect2: error: ld returned 1 exit status
12
13The commit 65561b53344b8 ("Fix getopt linking error") actually fixed the
14linking error only for uclibc, but not for musl-libc. The patch fixes
15the error for both uclibc and musl-libc.
16
17Link: http://autobuild.buildroot.net/results/d5b1b4e5e9d9c8eca5e75c345db4d1f3f0cd84ed/build-end.log
18Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
19Upstream: https://github.com/certnanny/sscep/pull/181
20---
21 configure.ac |  2 +-
22 src/getopt.c | 12 ++----------
23 2 files changed, 3 insertions(+), 11 deletions(-)
24
25diff --git a/configure.ac b/configure.ac
26index 9f3ee15686a2..7a968d97dcaa 100644
27--- a/configure.ac
28+++ b/configure.ac
29@@ -34,7 +34,7 @@ AC_TYPE_SIZE_T
30 # Checks for library functions.
31 AC_FUNC_MALLOC
32 AC_FUNC_REALLOC
33-AC_CHECK_FUNCS([alarm gethostbyname memset socket strchr strdup strstr])
34+AC_CHECK_FUNCS([alarm gethostbyname getopt memset socket strchr strdup strstr])
35
36 AC_CONFIG_FILES([Makefile])
37 AC_SUBST([LIBTOOL_DEPS])
38diff --git a/src/getopt.c b/src/getopt.c
39index 0109406ba4ac..8793052845ed 100644
40--- a/src/getopt.c
41+++ b/src/getopt.c
42@@ -31,15 +31,7 @@
43 #include <stddef.h>
44 #include <string.h>
45
46-#define GETOPT_INTERFACE_VERSION 2
47-#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
48-# include <gnu-versions.h>
49-# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
50-#  define ELIDE_CODE
51-# endif
52-#endif
53-
54-#ifndef ELIDE_CODE
55+#ifndef HAVE_GETOPT
56
57 char* optarg;
58 int optopt;
59@@ -237,4 +229,4 @@ int getopt_long(int argc, char* const argv[], const char* optstring,
60   return retval;
61 }
62
63-#endif	/* Not ELIDE_CODE.  */
64+#endif	/* HAVE_GETOPT  */
65--
662.43.0
67
68