unsetenv: Add missing declaration on OSF/1 5.1.
[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 = yes; then
25     AC_CACHE_CHECK([whether setenv validates arguments],
26       [gl_cv_func_setenv_works],
27       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
28        #include <stdlib.h>
29        #include <errno.h>
30        #include <string.h>
31       ]], [[
32        int result = 0;
33        {
34          if (setenv ("", "", 0) != -1)
35            result |= 1;
36          else if (errno != EINVAL)
37            result |= 2;
38        }
39        {
40          if (setenv ("a", "=", 1) != 0)
41            result |= 4;
42          else if (strcmp (getenv ("a"), "=") != 0)
43            result |= 8;
44        }
45        return result;
46       ]])],
47       [gl_cv_func_setenv_works=yes], [gl_cv_func_setenv_works=no],
48       [gl_cv_func_setenv_works="guessing no"])])
49     if test "$gl_cv_func_setenv_works" != yes; then
50       REPLACE_SETENV=1
51       AC_LIBOBJ([setenv])
52     fi
53   fi
54   gl_PREREQ_SETENV
55 ])
56
57 AC_DEFUN([gl_FUNC_UNSETENV],
58 [
59   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
60   AC_CHECK_DECLS_ONCE([unsetenv])
61   if test $ac_cv_have_decl_unsetenv = no; then
62     HAVE_DECL_UNSETENV=0
63   fi
64   AC_CHECK_FUNCS([unsetenv])
65   if test $ac_cv_func_unsetenv = no; then
66     AC_LIBOBJ([unsetenv])
67     gl_PREREQ_UNSETENV
68   else
69     dnl Some BSDs return void, failing to do error checking.
70     AC_CACHE_CHECK([for unsetenv() return type], [gt_cv_func_unsetenv_ret],
71       [AC_COMPILE_IFELSE(
72          [AC_LANG_PROGRAM(
73             [[
74 #undef _BSD
75 #define _BSD 1 /* unhide unsetenv declaration in OSF/1 5.1 <stdlib.h> */
76 #include <stdlib.h>
77 extern
78 #ifdef __cplusplus
79 "C"
80 #endif
81 #if defined(__STDC__) || defined(__cplusplus)
82 int unsetenv (const char *name);
83 #else
84 int unsetenv();
85 #endif
86             ]],
87             [[]])],
88          [gt_cv_func_unsetenv_ret='int'],
89          [gt_cv_func_unsetenv_ret='void'])])
90     if test $gt_cv_func_unsetenv_ret = 'void'; then
91       AC_DEFINE([VOID_UNSETENV], [1], [Define to 1 if unsetenv returns void
92        instead of int.])
93       REPLACE_UNSETENV=1
94       AC_LIBOBJ([unsetenv])
95     fi
96
97     dnl Solaris 10 unsetenv does not remove all copies of a name.
98     dnl OpenBSD 4.7 unsetenv("") does not fail.
99     AC_CACHE_CHECK([whether unsetenv obeys POSIX],
100       [gl_cv_func_unsetenv_works],
101       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
102        #include <stdlib.h>
103        #include <errno.h>
104       ]], [[
105        char entry[] = "b=2";
106        if (putenv ((char *) "a=1")) return 1;
107        if (putenv (entry)) return 2;
108        entry[0] = 'a';
109        unsetenv ("a");
110        if (getenv ("a")) return 3;
111        if (!unsetenv ("") || errno != EINVAL) return 4;
112       ]])],
113       [gl_cv_func_unsetenv_works=yes], [gl_cv_func_unsetenv_works=no],
114       [gl_cv_func_unsetenv_works="guessing no"])])
115     if test "$gl_cv_func_unsetenv_works" != yes; then
116       REPLACE_UNSETENV=1
117       AC_LIBOBJ([unsetenv])
118     fi
119   fi
120 ])
121
122 # Prerequisites of lib/setenv.c.
123 AC_DEFUN([gl_PREREQ_SETENV],
124 [
125   AC_REQUIRE([AC_FUNC_ALLOCA])
126   AC_REQUIRE([gl_ENVIRON])
127   AC_CHECK_HEADERS_ONCE([unistd.h])
128   AC_CHECK_HEADERS([search.h])
129   AC_CHECK_FUNCS([tsearch])
130 ])
131
132 # Prerequisites of lib/unsetenv.c.
133 AC_DEFUN([gl_PREREQ_UNSETENV],
134 [
135   AC_REQUIRE([gl_ENVIRON])
136   AC_CHECK_HEADERS_ONCE([unistd.h])
137 ])