setenv.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* $OpenBSD: setenv.c,v 1.13 2010/08/23 22:31:50 millert Exp $ */
  2. /*
  3. * Copyright (c) 1987 Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
  31. #include "includes.h"
  32. #if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
  33. #include <errno.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. extern char **environ;
  37. static char **lastenv; /* last value of environ */
  38. /* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */
  39. /*
  40. * __findenv --
  41. * Returns pointer to value associated with name, if any, else NULL.
  42. * Starts searching within the environmental array at offset.
  43. * Sets offset to be the offset of the name/value combination in the
  44. * environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
  45. * Explicitly removes '=' in argument name.
  46. *
  47. * This routine *should* be a static; don't use it.
  48. */
  49. static char *
  50. __findenv(const char *name, int len, int *offset)
  51. {
  52. extern char **environ;
  53. int i;
  54. const char *np;
  55. char **p, *cp;
  56. if (name == NULL || environ == NULL)
  57. return (NULL);
  58. for (p = environ + *offset; (cp = *p) != NULL; ++p) {
  59. for (np = name, i = len; i && *cp; i--)
  60. if (*cp++ != *np++)
  61. break;
  62. if (i == 0 && *cp++ == '=') {
  63. *offset = p - environ;
  64. return (cp);
  65. }
  66. }
  67. return (NULL);
  68. }
  69. #if 0 /* nothing uses putenv */
  70. /*
  71. * putenv --
  72. * Add a name=value string directly to the environmental, replacing
  73. * any current value.
  74. */
  75. int
  76. putenv(char *str)
  77. {
  78. char **P, *cp;
  79. size_t cnt;
  80. int offset = 0;
  81. for (cp = str; *cp && *cp != '='; ++cp)
  82. ;
  83. if (*cp != '=') {
  84. errno = EINVAL;
  85. return (-1); /* missing `=' in string */
  86. }
  87. if (__findenv(str, (int)(cp - str), &offset) != NULL) {
  88. environ[offset++] = str;
  89. /* could be set multiple times */
  90. while (__findenv(str, (int)(cp - str), &offset)) {
  91. for (P = &environ[offset];; ++P)
  92. if (!(*P = *(P + 1)))
  93. break;
  94. }
  95. return (0);
  96. }
  97. /* create new slot for string */
  98. for (P = environ; *P != NULL; P++)
  99. ;
  100. cnt = P - environ;
  101. P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
  102. if (!P)
  103. return (-1);
  104. if (lastenv != environ)
  105. memcpy(P, environ, cnt * sizeof(char *));
  106. lastenv = environ = P;
  107. environ[cnt] = str;
  108. environ[cnt + 1] = NULL;
  109. return (0);
  110. }
  111. #endif
  112. #ifndef HAVE_SETENV
  113. /*
  114. * setenv --
  115. * Set the value of the environmental variable "name" to be
  116. * "value". If rewrite is set, replace any current value.
  117. */
  118. int
  119. setenv(const char *name, const char *value, int rewrite)
  120. {
  121. char *C, **P;
  122. const char *np;
  123. int l_value, offset = 0;
  124. for (np = name; *np && *np != '='; ++np)
  125. ;
  126. #ifdef notyet
  127. if (*np) {
  128. errno = EINVAL;
  129. return (-1); /* has `=' in name */
  130. }
  131. #endif
  132. l_value = strlen(value);
  133. if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
  134. int tmpoff = offset + 1;
  135. if (!rewrite)
  136. return (0);
  137. #if 0 /* XXX - existing entry may not be writable */
  138. if (strlen(C) >= l_value) { /* old larger; copy over */
  139. while ((*C++ = *value++))
  140. ;
  141. return (0);
  142. }
  143. #endif
  144. /* could be set multiple times */
  145. while (__findenv(name, (int)(np - name), &tmpoff)) {
  146. for (P = &environ[tmpoff];; ++P)
  147. if (!(*P = *(P + 1)))
  148. break;
  149. }
  150. } else { /* create new slot */
  151. size_t cnt;
  152. for (P = environ; *P != NULL; P++)
  153. ;
  154. cnt = P - environ;
  155. P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
  156. if (!P)
  157. return (-1);
  158. if (lastenv != environ)
  159. memcpy(P, environ, cnt * sizeof(char *));
  160. lastenv = environ = P;
  161. offset = cnt;
  162. environ[cnt + 1] = NULL;
  163. }
  164. if (!(environ[offset] = /* name + `=' + value */
  165. malloc((size_t)((int)(np - name) + l_value + 2))))
  166. return (-1);
  167. for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
  168. ;
  169. for (*C++ = '='; (*C++ = *value++); )
  170. ;
  171. return (0);
  172. }
  173. #endif /* HAVE_SETENV */
  174. #ifndef HAVE_UNSETENV
  175. /*
  176. * unsetenv(name) --
  177. * Delete environmental variable "name".
  178. */
  179. int
  180. unsetenv(const char *name)
  181. {
  182. char **P;
  183. const char *np;
  184. int offset = 0;
  185. if (!name || !*name) {
  186. errno = EINVAL;
  187. return (-1);
  188. }
  189. for (np = name; *np && *np != '='; ++np)
  190. ;
  191. if (*np) {
  192. errno = EINVAL;
  193. return (-1); /* has `=' in name */
  194. }
  195. /* could be set multiple times */
  196. while (__findenv(name, (int)(np - name), &offset)) {
  197. for (P = &environ[offset];; ++P)
  198. if (!(*P = *(P + 1)))
  199. break;
  200. }
  201. return (0);
  202. }
  203. #endif /* HAVE_UNSETENV */
  204. #endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */