putenv.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 1991, 2000, 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
  2. * This library is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU Lesser General Public License
  4. * as published by the Free Software Foundation; either version 3 of
  5. * the License, or (at your option) any later version.
  6. *
  7. * This library is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * Lesser General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Lesser General Public
  13. * License along with this library; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/scmconfig.h"
  21. #include <sys/types.h>
  22. #include <errno.h>
  23. /* Don't include stdlib.h for non-GNU C libraries because some of them
  24. contain conflicting prototypes for getopt.
  25. This needs to come after some library #include
  26. to get __GNU_LIBRARY__ defined. */
  27. #ifdef __GNU_LIBRARY__
  28. #include <stdlib.h>
  29. #else
  30. char *malloc ();
  31. #endif /* GNU C library. */
  32. #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
  33. #include <string.h>
  34. #else
  35. #include <strings.h>
  36. #ifndef strchr
  37. #define strchr index
  38. #endif
  39. #ifndef memcpy
  40. #define memcpy(d, s, n) bcopy((s), (d), (n))
  41. #endif
  42. #endif
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h>
  45. #endif
  46. #if HAVE_CRT_EXTERNS_H
  47. #include <crt_externs.h> /* for Darwin _NSGetEnviron */
  48. #endif
  49. #ifndef NULL
  50. #define NULL 0
  51. #endif
  52. extern char **environ;
  53. /* On Apple Darwin in a shared library there's no "environ" to access
  54. directly, instead the address of that variable must be obtained with
  55. _NSGetEnviron(). */
  56. #if HAVE__NSGETENVIRON && defined (PIC)
  57. #define environ (*_NSGetEnviron())
  58. #endif
  59. /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
  60. int
  61. putenv (const char *string)
  62. {
  63. char *name_end = strchr (string, '=');
  64. register size_t size;
  65. register char **ep;
  66. if (name_end == NULL)
  67. {
  68. /* Remove the variable from the environment. */
  69. size = strlen (string);
  70. for (ep = environ; *ep != NULL; ++ep)
  71. if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
  72. {
  73. while (ep[1] != NULL)
  74. {
  75. ep[0] = ep[1];
  76. ++ep;
  77. }
  78. *ep = NULL;
  79. return 0;
  80. }
  81. }
  82. size = 0;
  83. for (ep = environ; *ep != NULL; ++ep)
  84. if (!strncmp (*ep, string, name_end - string) &&
  85. (*ep)[name_end - string] == '=')
  86. break;
  87. else
  88. ++size;
  89. if (*ep == NULL)
  90. {
  91. static char **last_environ = NULL;
  92. char **new_environ = (char **) scm_malloc ((size + 2) * sizeof (char *));
  93. memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *));
  94. new_environ[size] = (char *) string;
  95. new_environ[size + 1] = NULL;
  96. if (last_environ != NULL)
  97. free ((char *) last_environ);
  98. last_environ = new_environ;
  99. environ = new_environ;
  100. }
  101. else
  102. *ep = (char *) string;
  103. return 0;
  104. }
  105. /*
  106. Local Variables:
  107. c-file-style: "gnu"
  108. End:
  109. */