putenv.c 3.1 KB

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