setenv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C 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. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #if !defined(HAVE_GNU_LD) && !defined (__ELF__)
  20. #define __environ environ
  21. #endif
  22. #if defined(_REENTRENT) || defined(_THREAD_SAFE)
  23. # include <pthread.h>
  24. /* We need to initialize the mutex. For this we use a method provided
  25. by pthread function 'pthread_once'. For this we need a once block. */
  26. static pthread_once__t _once_block = pthread_once_init;
  27. /* This is the mutex which protects the global environment of simultaneous
  28. modifications. */
  29. static pthread_mutex_t _setenv_mutex;
  30. static void
  31. DEFUN_VOID(_init_setenv_mutex)
  32. {
  33. pthread_mutex_init(&_setenv_mutex, pthread_mutexattr_default);
  34. }
  35. # define LOCK() \
  36. do { pthread_once(&_once_block, _init_setenv_mutex);
  37. pthread_mutex_lock(&_setenv_mutex); } while (0)
  38. # define UNLOCK() pthread_mutex_unlock(&_setenv_mutex)
  39. #else /* !_REENTRENT && !_THREAD_SAFE */
  40. # define LOCK()
  41. # define UNLOCK()
  42. #endif /* _REENTRENT || _THREAD_SAFE */
  43. int setenv(const char *name, const char *value, int replace)
  44. {
  45. register char **ep;
  46. register size_t size;
  47. const size_t namelen = strlen (name);
  48. const size_t vallen = strlen (value);
  49. int result = 0;
  50. LOCK();
  51. size = 0;
  52. for (ep = __environ; *ep != NULL; ++ep)
  53. if (!memcmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  54. break;
  55. else
  56. ++size;
  57. if (*ep == NULL)
  58. {
  59. static char **last_environ = NULL;
  60. char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
  61. if (new_environ == NULL)
  62. {
  63. result = -1;
  64. goto do_return;
  65. }
  66. (void) memcpy((__ptr_t) new_environ, (__ptr_t) __environ, size * sizeof(char *));
  67. new_environ[size] = malloc (namelen + 1 + vallen + 1);
  68. if (new_environ[size] == NULL)
  69. {
  70. free (new_environ);
  71. errno = ENOMEM;
  72. result = -1;
  73. goto do_return;
  74. }
  75. memcpy (new_environ[size], name, namelen);
  76. new_environ[size][namelen] = '=';
  77. memcpy (&new_environ[size][namelen + 1], value, vallen + 1);
  78. new_environ[size + 1] = NULL;
  79. if (last_environ != NULL)
  80. free ((__ptr_t) last_environ);
  81. last_environ = new_environ;
  82. __environ = new_environ;
  83. }
  84. else if (replace)
  85. {
  86. size_t len = strlen (*ep);
  87. if (len < namelen + 1 + vallen)
  88. {
  89. char *new = malloc (namelen + 1 + vallen + 1);
  90. if (new == NULL)
  91. {
  92. result = -1;
  93. goto do_return;
  94. }
  95. *ep = new;
  96. memcpy (*ep, name, namelen);
  97. (*ep)[namelen] = '=';
  98. }
  99. memcpy (&(*ep)[namelen + 1], value, vallen + 1);
  100. }
  101. do_return:
  102. UNLOCK();
  103. return result;
  104. }
  105. void unsetenv(const char *name)
  106. {
  107. register char **ep;
  108. register char **dp;
  109. const size_t namelen = strlen (name);
  110. LOCK();
  111. for (dp = ep = __environ; *ep != NULL; ++ep)
  112. if (memcmp (*ep, name, namelen) || (*ep)[namelen] != '=')
  113. {
  114. *dp = *ep;
  115. ++dp;
  116. }
  117. *dp = NULL;
  118. UNLOCK();
  119. }