putenv.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <string.h>
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #if HAVE_CRT_EXTERNS_H
  36. #include <crt_externs.h> /* for Darwin _NSGetEnviron */
  37. #endif
  38. #ifndef NULL
  39. #define NULL 0
  40. #endif
  41. extern char **environ;
  42. /* On Apple Darwin in a shared library there's no "environ" to access
  43. directly, instead the address of that variable must be obtained with
  44. _NSGetEnviron(). */
  45. #if HAVE__NSGETENVIRON && defined (PIC)
  46. #define environ (*_NSGetEnviron())
  47. #endif
  48. /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
  49. int
  50. putenv (const char *string)
  51. {
  52. char *name_end = strchr (string, '=');
  53. register size_t size;
  54. register char **ep;
  55. if (name_end == NULL)
  56. {
  57. /* Remove the variable from the environment. */
  58. size = strlen (string);
  59. for (ep = environ; *ep != NULL; ++ep)
  60. if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
  61. {
  62. while (ep[1] != NULL)
  63. {
  64. ep[0] = ep[1];
  65. ++ep;
  66. }
  67. *ep = NULL;
  68. return 0;
  69. }
  70. }
  71. size = 0;
  72. for (ep = environ; *ep != NULL; ++ep)
  73. if (!strncmp (*ep, string, name_end - string) &&
  74. (*ep)[name_end - string] == '=')
  75. break;
  76. else
  77. ++size;
  78. if (*ep == NULL)
  79. {
  80. static char **last_environ = NULL;
  81. char **new_environ = (char **) scm_malloc ((size + 2) * sizeof (char *));
  82. memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *));
  83. new_environ[size] = (char *) string;
  84. new_environ[size + 1] = NULL;
  85. if (last_environ != NULL)
  86. free ((char *) last_environ);
  87. last_environ = new_environ;
  88. environ = new_environ;
  89. }
  90. else
  91. *ep = (char *) string;
  92. return 0;
  93. }
  94. /*
  95. Local Variables:
  96. c-file-style: "gnu"
  97. End:
  98. */