putenv.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 1991, 2000, 2002 Free Software Foundation, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2, or (at your option)
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  13. USA */
  14. #ifdef HAVE_CONFIG_H
  15. #include "libguile/scmconfig.h"
  16. #endif
  17. #include <sys/types.h>
  18. #include <errno.h>
  19. #ifndef errno
  20. extern int errno;
  21. #endif
  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. #ifndef NULL
  46. #define NULL 0
  47. #endif
  48. extern char **environ;
  49. /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
  50. int
  51. putenv (const char *string)
  52. {
  53. char *name_end = strchr (string, '=');
  54. register size_t size;
  55. register char **ep;
  56. if (name_end == NULL)
  57. {
  58. /* Remove the variable from the environment. */
  59. size = strlen (string);
  60. for (ep = environ; *ep != NULL; ++ep)
  61. if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
  62. {
  63. while (ep[1] != NULL)
  64. {
  65. ep[0] = ep[1];
  66. ++ep;
  67. }
  68. *ep = NULL;
  69. return 0;
  70. }
  71. }
  72. size = 0;
  73. for (ep = environ; *ep != NULL; ++ep)
  74. if (!strncmp (*ep, string, name_end - string) &&
  75. (*ep)[name_end - string] == '=')
  76. break;
  77. else
  78. ++size;
  79. if (*ep == NULL)
  80. {
  81. static char **last_environ = NULL;
  82. char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
  83. if (new_environ == NULL)
  84. return -1;
  85. memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *));
  86. new_environ[size] = (char *) string;
  87. new_environ[size + 1] = NULL;
  88. if (last_environ != NULL)
  89. free ((char *) last_environ);
  90. last_environ = new_environ;
  91. environ = new_environ;
  92. }
  93. else
  94. *ep = (char *) string;
  95. return 0;
  96. }
  97. /*
  98. Local Variables:
  99. c-file-style: "gnu"
  100. End:
  101. */