setenv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #if !defined(HAVE_GNU_LD) && !defined (__ELF__)
  19. #define __environ environ
  20. #endif
  21. #if defined(_REENTRENT) || defined(_THREAD_SAFE)
  22. # include <pthread.h>
  23. /* We need to initialize the mutex. For this we use a method provided
  24. by pthread function 'pthread_once'. For this we need a once block. */
  25. static pthread_once__t _once_block = pthread_once_init;
  26. /* This is the mutex which protects the global environment of simultaneous
  27. modifications. */
  28. static pthread_mutex_t _setenv_mutex;
  29. static void
  30. DEFUN_VOID(_init_setenv_mutex)
  31. {
  32. pthread_mutex_init(&_setenv_mutex, pthread_mutexattr_default);
  33. }
  34. # define LOCK() \
  35. do { pthread_once(&_once_block, _init_setenv_mutex);
  36. pthread_mutex_lock(&_setenv_mutex); } while (0)
  37. # define UNLOCK() pthread_mutex_unlock(&_setenv_mutex)
  38. #else /* !_REENTRENT && !_THREAD_SAFE */
  39. # define LOCK()
  40. # define UNLOCK()
  41. #endif /* _REENTRENT || _THREAD_SAFE */
  42. int setenv(const char *name, const char *value, int replace)
  43. {
  44. register char **ep;
  45. register size_t size;
  46. const size_t namelen = strlen (name);
  47. const size_t vallen = strlen (value);
  48. int result = 0;
  49. LOCK();
  50. size = 0;
  51. for (ep = __environ; *ep != NULL; ++ep)
  52. if (!memcmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  53. break;
  54. else
  55. ++size;
  56. if (*ep == NULL)
  57. {
  58. static char **last_environ = NULL;
  59. char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
  60. if (new_environ == NULL)
  61. {
  62. result = -1;
  63. goto do_return;
  64. }
  65. (void) memcpy((__ptr_t) new_environ, (__ptr_t) __environ, size * sizeof(char *));
  66. new_environ[size] = malloc (namelen + 1 + vallen + 1);
  67. if (new_environ[size] == NULL)
  68. {
  69. free (new_environ);
  70. errno = ENOMEM;
  71. result = -1;
  72. goto do_return;
  73. }
  74. memcpy (new_environ[size], name, namelen);
  75. new_environ[size][namelen] = '=';
  76. memcpy (&new_environ[size][namelen + 1], value, vallen + 1);
  77. new_environ[size + 1] = NULL;
  78. if (last_environ != NULL)
  79. free ((__ptr_t) last_environ);
  80. last_environ = new_environ;
  81. __environ = new_environ;
  82. }
  83. else if (replace)
  84. {
  85. size_t len = strlen (*ep);
  86. if (len < namelen + 1 + vallen)
  87. {
  88. char *new = malloc (namelen + 1 + vallen + 1);
  89. if (new == NULL)
  90. {
  91. result = -1;
  92. goto do_return;
  93. }
  94. *ep = new;
  95. memcpy (*ep, name, namelen);
  96. (*ep)[namelen] = '=';
  97. }
  98. memcpy (&(*ep)[namelen + 1], value, vallen + 1);
  99. }
  100. do_return:
  101. UNLOCK();
  102. return result;
  103. }
  104. void unsetenv(const char *name)
  105. {
  106. register char **ep;
  107. register char **dp;
  108. const size_t namelen = strlen (name);
  109. LOCK();
  110. for (dp = ep = __environ; *ep != NULL; ++ep)
  111. if (memcmp (*ep, name, namelen) || (*ep)[namelen] != '=')
  112. {
  113. *dp = *ep;
  114. ++dp;
  115. }
  116. *dp = NULL;
  117. UNLOCK();
  118. }