setenv.m4 4.9 KB

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