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