]> git.cworth.org Git - tar/blob - m4/setenv.m4
Imported Upstream version 1.24
[tar] / m4 / setenv.m4
1 # setenv.m4 serial 18
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        if (setenv ("", "", 0) != -1) return 1;
31        if (errno != EINVAL) return 2;
32        if (setenv ("a", "=", 1) != 0) return 3;
33        if (strcmp (getenv ("a"), "=") != 0) return 4;
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       AC_LIBOBJ([setenv])
40     fi
41   fi
42   gl_PREREQ_SETENV
43 ])
44
45 AC_DEFUN([gl_FUNC_UNSETENV],
46 [
47   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
48   AC_CHECK_FUNCS([unsetenv])
49   if test $ac_cv_func_unsetenv = no; then
50     HAVE_UNSETENV=0
51     AC_LIBOBJ([unsetenv])
52     gl_PREREQ_UNSETENV
53   else
54     dnl Some BSDs return void, failing to do error checking.
55     AC_CACHE_CHECK([for unsetenv() return type], [gt_cv_func_unsetenv_ret],
56       [AC_COMPILE_IFELSE(
57          [AC_LANG_PROGRAM(
58             [[
59 #include <stdlib.h>
60 extern
61 #ifdef __cplusplus
62 "C"
63 #endif
64 #if defined(__STDC__) || defined(__cplusplus)
65 int unsetenv (const char *name);
66 #else
67 int unsetenv();
68 #endif
69             ]],
70             [[]])],
71          [gt_cv_func_unsetenv_ret='int'],
72          [gt_cv_func_unsetenv_ret='void'])])
73     if test $gt_cv_func_unsetenv_ret = 'void'; then
74       AC_DEFINE([VOID_UNSETENV], [1], [Define to 1 if unsetenv returns void
75        instead of int.])
76       REPLACE_UNSETENV=1
77       AC_LIBOBJ([unsetenv])
78     fi
79
80     dnl Solaris 10 unsetenv does not remove all copies of a name.
81     dnl OpenBSD 4.7 unsetenv("") does not fail.
82     AC_CACHE_CHECK([whether unsetenv obeys POSIX],
83       [gl_cv_func_unsetenv_works],
84       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
85        #include <stdlib.h>
86        #include <errno.h>
87       ]], [[
88        char entry[] = "b=2";
89        if (putenv ((char *) "a=1")) return 1;
90        if (putenv (entry)) return 2;
91        entry[0] = 'a';
92        unsetenv ("a");
93        if (getenv ("a")) return 3;
94        if (!unsetenv ("") || errno != EINVAL) return 4;
95       ]])],
96       [gl_cv_func_unsetenv_works=yes], [gl_cv_func_unsetenv_works=no],
97       [gl_cv_func_unsetenv_works="guessing no"])])
98     if test "$gl_cv_func_unsetenv_works" != yes; then
99       REPLACE_UNSETENV=1
100       AC_LIBOBJ([unsetenv])
101     fi
102   fi
103 ])
104
105 # Prerequisites of lib/setenv.c.
106 AC_DEFUN([gl_PREREQ_SETENV],
107 [
108   AC_REQUIRE([AC_FUNC_ALLOCA])
109   AC_REQUIRE([gl_ENVIRON])
110   AC_CHECK_HEADERS_ONCE([unistd.h])
111   AC_CHECK_HEADERS([search.h])
112   AC_CHECK_FUNCS([tsearch])
113 ])
114
115 # Prerequisites of lib/unsetenv.c.
116 AC_DEFUN([gl_PREREQ_UNSETENV],
117 [
118   AC_REQUIRE([gl_ENVIRON])
119   AC_CHECK_HEADERS_ONCE([unistd.h])
120 ])