setenv.m4 4.5 KB

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