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