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