setenv.c 3.6 KB

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