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