1 /* $NetBSD: setenv.c,v 1.20 2002/11/11 20:39:12 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93"; 40 #else 41 __RCSID("$NetBSD: setenv.c,v 1.20 2002/11/11 20:39:12 thorpej Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 47 #include <assert.h> 48 #include <errno.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include "local.h" 52 #include "reentrant.h" 53 54 #ifdef __weak_alias 55 __weak_alias(setenv,_setenv) 56 __weak_alias(unsetenv,_unsetenv) 57 #endif 58 59 #ifdef _REENT 60 extern rwlock_t __environ_lock; 61 #endif 62 63 extern char **environ; 64 65 /* 66 * setenv -- 67 * Set the value of the environmental variable "name" to be 68 * "value". If rewrite is set, replace any current value. 69 */ 70 int 71 setenv(name, value, rewrite) 72 const char *name; 73 const char *value; 74 int rewrite; 75 { 76 static int alloced; /* if allocated space before */ 77 char *c; 78 const char *cc; 79 size_t l_value; 80 int offset; 81 82 _DIAGASSERT(name != NULL); 83 _DIAGASSERT(value != NULL); 84 85 if (*value == '=') /* no `=' in value */ 86 ++value; 87 l_value = strlen(value); 88 rwlock_wrlock(&__environ_lock); 89 /* find if already exists */ 90 if ((c = __findenv(name, &offset)) != NULL) { 91 if (!rewrite) { 92 rwlock_unlock(&__environ_lock); 93 return (0); 94 } 95 if (strlen(c) >= l_value) { /* old larger; copy over */ 96 while ((*c++ = *value++) != '\0'); 97 rwlock_unlock(&__environ_lock); 98 return (0); 99 } 100 } else { /* create new slot */ 101 int cnt; 102 char **p; 103 104 for (p = environ, cnt = 0; *p; ++p, ++cnt); 105 if (alloced) { /* just increase size */ 106 environ = realloc(environ, 107 (size_t)(sizeof(char *) * (cnt + 2))); 108 if (!environ) { 109 rwlock_unlock(&__environ_lock); 110 return (-1); 111 } 112 } 113 else { /* get new space */ 114 alloced = 1; /* copy old entries into it */ 115 p = malloc((size_t)(sizeof(char *) * (cnt + 2))); 116 if (!p) { 117 rwlock_unlock(&__environ_lock); 118 return (-1); 119 } 120 memcpy(p, environ, cnt * sizeof(char *)); 121 environ = p; 122 } 123 environ[cnt + 1] = NULL; 124 offset = cnt; 125 } 126 for (cc = name; *cc && *cc != '='; ++cc)/* no `=' in name */ 127 continue; 128 if (!(environ[offset] = /* name + `=' + value */ 129 malloc((size_t)((int)(cc - name) + l_value + 2)))) { 130 rwlock_unlock(&__environ_lock); 131 return (-1); 132 } 133 for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); 134 for (*c++ = '='; (*c++ = *value++) != '\0'; ); 135 rwlock_unlock(&__environ_lock); 136 return (0); 137 } 138 139 /* 140 * unsetenv(name) -- 141 * Delete environmental variable "name". 142 */ 143 void 144 unsetenv(name) 145 const char *name; 146 { 147 char **p; 148 int offset; 149 150 _DIAGASSERT(name != NULL); 151 152 rwlock_wrlock(&__environ_lock); 153 while (__findenv(name, &offset)) /* if set multiple times */ 154 for (p = &environ[offset];; ++p) 155 if (!(*p = *(p + 1))) 156 break; 157 rwlock_unlock(&__environ_lock); 158 } 159