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