Debian Codegraph @

gnulib/m4/setenv.m4

raw
1 # setenv.m4
2 # serial 35
3 dnl Copyright (C) 2001-2004, 2006-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7
8 AC_DEFUN([gl_FUNC_SETENV],
9 [
10 AC_REQUIRE([gl_FUNC_SETENV_SEPARATE])
11 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12 if test $ac_cv_func_setenv = no; then
13 HAVE_SETENV=0
14 else
15 AC_CACHE_CHECK([whether setenv validates arguments],
16 [gl_cv_func_setenv_works],
17 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <string.h>
21 ]], [[
22 int result = 0;
23 {
24 if (setenv ("", "", 0) != -1)
25 result |= 1;
26 else if (errno != EINVAL)
27 result |= 2;
28 }
29 {
30 if (setenv ("a", "=", 1) != 0)
31 result |= 4;
32 else if (strcmp (getenv ("a"), "=") != 0)
33 result |= 8;
34 }
35 return result;
36 ]])],
37 [gl_cv_func_setenv_works=yes], [gl_cv_func_setenv_works=no],
38 [case "$host_os" in
39 # Guess yes on glibc systems.
40 *-gnu* | gnu*) gl_cv_func_setenv_works="guessing yes" ;;
41 # Guess yes on musl systems.
42 *-musl* | midipix*) gl_cv_func_setenv_works="guessing yes" ;;
43 # If we don't know, obey --enable-cross-guesses.
44 *) gl_cv_func_setenv_works="$gl_cross_guess_normal" ;;
45 esac
46 ])])
47 case "$gl_cv_func_setenv_works" in
48 *yes) ;;
49 *)
50 REPLACE_SETENV=1
51 ;;
52 esac
53 fi
54 ])
55
56 # Like gl_FUNC_SETENV, except prepare for separate compilation
57 # (no REPLACE_SETENV, no AC_LIBOBJ).
58 AC_DEFUN([gl_FUNC_SETENV_SEPARATE],
59 [
60 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
61 AC_CHECK_DECLS_ONCE([setenv])
62 if test $ac_cv_have_decl_setenv = no; then
63 HAVE_DECL_SETENV=0
64 fi
65 AC_CHECK_FUNCS_ONCE([setenv])
66 gl_PREREQ_SETENV
67 ])
68
69 AC_DEFUN([gl_FUNC_UNSETENV],
70 [
71 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
72 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
73 AC_CHECK_DECLS_ONCE([unsetenv])
74 if test $ac_cv_have_decl_unsetenv = no; then
75 HAVE_DECL_UNSETENV=0
76 fi
77 AC_CHECK_FUNCS([unsetenv])
78 if test $ac_cv_func_unsetenv = no; then
79 HAVE_UNSETENV=0
80 else
81 HAVE_UNSETENV=1
82 dnl Some BSDs return void, failing to do error checking.
83 AC_CACHE_CHECK([for unsetenv() return type], [gt_cv_func_unsetenv_ret],
84 [AC_COMPILE_IFELSE(
85 [AC_LANG_PROGRAM(
86 [[
87 #undef _BSD
88 #define _BSD 1 /* unhide unsetenv declaration in OSF/1 5.1 <stdlib.h> */
89 #include <stdlib.h>
90 extern
91 #ifdef __cplusplus
92 "C"
93 #endif
94 int unsetenv (const char *name);
95 ]],
96 [[]])],
97 [gt_cv_func_unsetenv_ret='int'],
98 [gt_cv_func_unsetenv_ret='void'])])
99 if test $gt_cv_func_unsetenv_ret = 'void'; then
100 AC_DEFINE([VOID_UNSETENV], [1], [Define to 1 if unsetenv returns void
101 instead of int.])
102 REPLACE_UNSETENV=1
103 fi
104
105 dnl Solaris 10 unsetenv does not remove all copies of a name.
106 dnl Haiku alpha 2 unsetenv gets confused by assignment to environ.
107 dnl OpenBSD 4.7 unsetenv("") does not fail.
108 AC_CACHE_CHECK([whether unsetenv obeys POSIX],
109 [gl_cv_func_unsetenv_works],
110 [AC_RUN_IFELSE(
111 [AC_LANG_PROGRAM([[
112 #include <stdlib.h>
113 #include <errno.h>
114 extern char **environ;
115 ]GL_MDA_DEFINES],
116 [[
117 char entry1[] = "a=1";
118 char entry2[] = "b=2";
119 char *env[] = { entry1, entry2, NULL };
120 if (putenv ((char *) "a=1")) return 1;
121 if (putenv (entry2)) return 2;
122 entry2[0] = 'a';
123 unsetenv ("a");
124 if (getenv ("a")) return 3;
125 if (!unsetenv ("") || errno != EINVAL) return 4;
126 entry2[0] = 'b';
127 environ = env;
128 if (!getenv ("a")) return 5;
129 entry2[0] = 'a';
130 unsetenv ("a");
131 if (getenv ("a")) return 6;
132 ]])],
133 [gl_cv_func_unsetenv_works=yes],
134 [gl_cv_func_unsetenv_works=no],
135 [case "$host_os" in
136 # Guess yes on glibc systems.
137 *-gnu*) gl_cv_func_unsetenv_works="guessing yes" ;;
138 # If we don't know, obey --enable-cross-guesses.
139 *) gl_cv_func_unsetenv_works="$gl_cross_guess_normal" ;;
140 esac
141 ])
142 ])
143 case "$gl_cv_func_unsetenv_works" in
144 *yes) ;;
145 *)
146 REPLACE_UNSETENV=1
147 ;;
148 esac
149 fi
150 ])
151
152 # Prerequisites of lib/setenv.c.
153 AC_DEFUN([gl_PREREQ_SETENV],
154 [
155 AC_REQUIRE([gl_ENVIRON])
156 AC_CHECK_HEADERS_ONCE([unistd.h])
157 AC_CHECK_HEADERS([search.h])
158 AC_CHECK_DECLS_ONCE([_putenv])
159 gl_CHECK_FUNCS_ANDROID([tsearch], [[#include <search.h>]])
160 ])
161
162 # Prerequisites of lib/unsetenv.c.
163 AC_DEFUN([gl_PREREQ_UNSETENV],
164 [
165 AC_REQUIRE([gl_ENVIRON])
166 AC_CHECK_HEADERS_ONCE([unistd.h])
167 AC_CHECK_DECLS_ONCE([_putenv])
168 ])