setenv.m4 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # setenv.m4 serial 25
  2. dnl Copyright (C) 2001-2004, 2006-2012 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. AC_DEFUN([gl_FUNC_SETENV],
  7. [
  8. AC_REQUIRE([gl_FUNC_SETENV_SEPARATE])
  9. if test $ac_cv_func_setenv = no; then
  10. HAVE_SETENV=0
  11. else
  12. AC_CACHE_CHECK([whether setenv validates arguments],
  13. [gl_cv_func_setenv_works],
  14. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. ]], [[
  19. int result = 0;
  20. {
  21. if (setenv ("", "", 0) != -1)
  22. result |= 1;
  23. else if (errno != EINVAL)
  24. result |= 2;
  25. }
  26. {
  27. if (setenv ("a", "=", 1) != 0)
  28. result |= 4;
  29. else if (strcmp (getenv ("a"), "=") != 0)
  30. result |= 8;
  31. }
  32. return result;
  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. fi
  39. fi
  40. ])
  41. # Like gl_FUNC_SETENV, except prepare for separate compilation
  42. # (no REPLACE_SETENV, no AC_LIBOBJ).
  43. AC_DEFUN([gl_FUNC_SETENV_SEPARATE],
  44. [
  45. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  46. AC_CHECK_DECLS_ONCE([setenv])
  47. if test $ac_cv_have_decl_setenv = no; then
  48. HAVE_DECL_SETENV=0
  49. fi
  50. AC_CHECK_FUNCS_ONCE([setenv])
  51. gl_PREREQ_SETENV
  52. ])
  53. AC_DEFUN([gl_FUNC_UNSETENV],
  54. [
  55. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  56. AC_CHECK_DECLS_ONCE([unsetenv])
  57. if test $ac_cv_have_decl_unsetenv = no; then
  58. HAVE_DECL_UNSETENV=0
  59. fi
  60. AC_CHECK_FUNCS([unsetenv])
  61. if test $ac_cv_func_unsetenv = no; then
  62. HAVE_UNSETENV=0
  63. else
  64. HAVE_UNSETENV=1
  65. dnl Some BSDs return void, failing to do error checking.
  66. AC_CACHE_CHECK([for unsetenv() return type], [gt_cv_func_unsetenv_ret],
  67. [AC_COMPILE_IFELSE(
  68. [AC_LANG_PROGRAM(
  69. [[
  70. #undef _BSD
  71. #define _BSD 1 /* unhide unsetenv declaration in OSF/1 5.1 <stdlib.h> */
  72. #include <stdlib.h>
  73. extern
  74. #ifdef __cplusplus
  75. "C"
  76. #endif
  77. int unsetenv (const char *name);
  78. ]],
  79. [[]])],
  80. [gt_cv_func_unsetenv_ret='int'],
  81. [gt_cv_func_unsetenv_ret='void'])])
  82. if test $gt_cv_func_unsetenv_ret = 'void'; then
  83. AC_DEFINE([VOID_UNSETENV], [1], [Define to 1 if unsetenv returns void
  84. instead of int.])
  85. REPLACE_UNSETENV=1
  86. fi
  87. dnl Solaris 10 unsetenv does not remove all copies of a name.
  88. dnl Haiku alpha 2 unsetenv gets confused by assignment to environ.
  89. dnl OpenBSD 4.7 unsetenv("") does not fail.
  90. AC_CACHE_CHECK([whether unsetenv obeys POSIX],
  91. [gl_cv_func_unsetenv_works],
  92. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  93. #include <stdlib.h>
  94. #include <errno.h>
  95. extern char **environ;
  96. ]], [[
  97. char entry1[] = "a=1";
  98. char entry2[] = "b=2";
  99. char *env[] = { entry1, entry2, NULL };
  100. if (putenv ((char *) "a=1")) return 1;
  101. if (putenv (entry2)) return 2;
  102. entry2[0] = 'a';
  103. unsetenv ("a");
  104. if (getenv ("a")) return 3;
  105. if (!unsetenv ("") || errno != EINVAL) return 4;
  106. entry2[0] = 'b';
  107. environ = env;
  108. if (!getenv ("a")) return 5;
  109. entry2[0] = 'a';
  110. unsetenv ("a");
  111. if (getenv ("a")) return 6;
  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. fi
  118. fi
  119. ])
  120. # Prerequisites of lib/setenv.c.
  121. AC_DEFUN([gl_PREREQ_SETENV],
  122. [
  123. AC_REQUIRE([AC_FUNC_ALLOCA])
  124. AC_REQUIRE([gl_ENVIRON])
  125. AC_CHECK_HEADERS_ONCE([unistd.h])
  126. AC_CHECK_HEADERS([search.h])
  127. AC_CHECK_FUNCS([tsearch])
  128. ])
  129. # Prerequisites of lib/unsetenv.c.
  130. AC_DEFUN([gl_PREREQ_UNSETENV],
  131. [
  132. AC_REQUIRE([gl_ENVIRON])
  133. AC_CHECK_HEADERS_ONCE([unistd.h])
  134. ])