setenv: restore to working order
[gnulib.git] / m4 / setenv.m4
1 # setenv.m4 serial 20
2 dnl Copyright (C) 2001-2004, 2006-2010 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_SETENV],
8 [
9   AC_REQUIRE([gl_FUNC_SETENV_SEPARATE])
10   if test $HAVE_SETENV$REPLACE_SETENV != 10; then
11     AC_LIBOBJ([setenv])
12   fi
13 ])
14
15 # Like gl_FUNC_SETENV, except prepare for separate compilation (no AC_LIBOBJ).
16 AC_DEFUN([gl_FUNC_SETENV_SEPARATE],
17 [
18   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
19   AC_CHECK_DECLS_ONCE([setenv])
20   if test $ac_cv_have_decl_setenv = no; then
21     HAVE_DECL_SETENV=0
22   fi
23   AC_CHECK_FUNCS_ONCE([setenv])
24   if test $ac_cv_func_setenv = no; then
25     HAVE_SETENV=0
26   else
27     AC_CACHE_CHECK([whether setenv validates arguments],
28       [gl_cv_func_setenv_works],
29       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
30        #include <stdlib.h>
31        #include <errno.h>
32        #include <string.h>
33       ]], [[
34        int result = 0;
35        {
36          if (setenv ("", "", 0) != -1)
37            result |= 1;
38          else if (errno != EINVAL)
39            result |= 2;
40        }
41        {
42          if (setenv ("a", "=", 1) != 0)
43            result |= 4;
44          else if (strcmp (getenv ("a"), "=") != 0)
45            result |= 8;
46        }
47        return result;
48       ]])],
49       [gl_cv_func_setenv_works=yes], [gl_cv_func_setenv_works=no],
50       [gl_cv_func_setenv_works="guessing no"])])
51     if test "$gl_cv_func_setenv_works" != yes; then
52       REPLACE_SETENV=1
53       AC_LIBOBJ([setenv])
54     fi
55   fi
56   gl_PREREQ_SETENV
57 ])
58
59 AC_DEFUN([gl_FUNC_UNSETENV],
60 [
61   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
62   AC_CHECK_DECLS_ONCE([unsetenv])
63   if test $ac_cv_have_decl_unsetenv = no; then
64     HAVE_DECL_UNSETENV=0
65   fi
66   AC_CHECK_FUNCS([unsetenv])
67   if test $ac_cv_func_unsetenv = no; then
68     AC_LIBOBJ([unsetenv])
69     gl_PREREQ_UNSETENV
70   else
71     dnl Some BSDs return void, failing to do error checking.
72     AC_CACHE_CHECK([for unsetenv() return type], [gt_cv_func_unsetenv_ret],
73       [AC_COMPILE_IFELSE(
74          [AC_LANG_PROGRAM(
75             [[
76 #undef _BSD
77 #define _BSD 1 /* unhide unsetenv declaration in OSF/1 5.1 <stdlib.h> */
78 #include <stdlib.h>
79 extern
80 #ifdef __cplusplus
81 "C"
82 #endif
83 #if defined(__STDC__) || defined(__cplusplus)
84 int unsetenv (const char *name);
85 #else
86 int unsetenv();
87 #endif
88             ]],
89             [[]])],
90          [gt_cv_func_unsetenv_ret='int'],
91          [gt_cv_func_unsetenv_ret='void'])])
92     if test $gt_cv_func_unsetenv_ret = 'void'; then
93       AC_DEFINE([VOID_UNSETENV], [1], [Define to 1 if unsetenv returns void
94        instead of int.])
95       REPLACE_UNSETENV=1
96       AC_LIBOBJ([unsetenv])
97     fi
98
99     dnl Solaris 10 unsetenv does not remove all copies of a name.
100     dnl OpenBSD 4.7 unsetenv("") does not fail.
101     AC_CACHE_CHECK([whether unsetenv obeys POSIX],
102       [gl_cv_func_unsetenv_works],
103       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
104        #include <stdlib.h>
105        #include <errno.h>
106       ]], [[
107        char entry[] = "b=2";
108        if (putenv ((char *) "a=1")) return 1;
109        if (putenv (entry)) return 2;
110        entry[0] = 'a';
111        unsetenv ("a");
112        if (getenv ("a")) return 3;
113        if (!unsetenv ("") || errno != EINVAL) return 4;
114       ]])],
115       [gl_cv_func_unsetenv_works=yes], [gl_cv_func_unsetenv_works=no],
116       [gl_cv_func_unsetenv_works="guessing no"])])
117     if test "$gl_cv_func_unsetenv_works" != yes; then
118       REPLACE_UNSETENV=1
119       AC_LIBOBJ([unsetenv])
120     fi
121   fi
122 ])
123
124 # Prerequisites of lib/setenv.c.
125 AC_DEFUN([gl_PREREQ_SETENV],
126 [
127   AC_REQUIRE([AC_FUNC_ALLOCA])
128   AC_REQUIRE([gl_ENVIRON])
129   AC_CHECK_HEADERS_ONCE([unistd.h])
130   AC_CHECK_HEADERS([search.h])
131   AC_CHECK_FUNCS([tsearch])
132 ])
133
134 # Prerequisites of lib/unsetenv.c.
135 AC_DEFUN([gl_PREREQ_UNSETENV],
136 [
137   AC_REQUIRE([gl_ENVIRON])
138   AC_CHECK_HEADERS_ONCE([unistd.h])
139 ])