[Libguestfs] [PATCH nbdkit] freebsd, openbsd: Add an extern decl for environ.

Richard W.M. Jones rjones at redhat.com
Tue Apr 28 15:43:42 UTC 2020


---
 configure.ac      | 24 ++++++++++++++++++++++++
 plugins/sh/call.c |  4 ++++
 2 files changed, 28 insertions(+)

diff --git a/configure.ac b/configure.ac
index cdddae73..9b925539 100644
--- a/configure.ac
+++ b/configure.ac
@@ -266,6 +266,30 @@ test (int a)
     ]
 )
 
+dnl 'environ' is not always declared in public header files:
+dnl Linux => <unistd.h>  Haiku => <stdlib.h>
+dnl FreeBSD & OpenBSD => not declared
+dnl On platforms where it's not declared we must add an extern decl.
+AC_MSG_CHECKING([if environ is declared in header files])
+AC_COMPILE_IFELSE([
+AC_LANG_SOURCE([[
+#include <stdlib.h>
+#include <unistd.h>
+static int
+test (void)
+{
+  const char **env = environ;
+  return env ? 1 : 0; // this just forces env to be used
+}
+]])
+    ],[
+    AC_MSG_RESULT([yes])
+    AC_DEFINE([HAVE_ENVIRON_DECL],[1],[environ is declared in headers])
+    ],[
+    AC_MSG_RESULT([no])
+    ]
+)
+
 dnl Check for other headers, all optional.
 AC_CHECK_HEADERS([\
 	alloca.h \
diff --git a/plugins/sh/call.c b/plugins/sh/call.c
index 4ffacfcc..91e69e9a 100644
--- a/plugins/sh/call.c
+++ b/plugins/sh/call.c
@@ -53,6 +53,10 @@
 
 #include "call.h"
 
+#ifndef HAVE_ENVIRON_DECL
+extern char **environ;
+#endif
+
 /* Temporary directory for scripts to use. */
 char tmpdir[] = "/tmp/nbdkitXXXXXX";
 
-- 
2.25.0




More information about the Libguestfs mailing list