1 /* $NetBSD: setenv.c,v 1.18 2000/01/22 22:19:20 mycroft 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.18 2000/01/22 22:19:20 mycroft 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 /* 64 * setenv -- 65 * Set the value of the environmental variable "name" to be 66 * "value". If rewrite is set, replace any current value. 67 */ 68 int 69 setenv(name, value, rewrite) 70 const char *name; 71 const char *value; 72 int rewrite; 73 { 74 extern char **environ; 75 static int alloced; /* if allocated space before */ 76 char *c; 77 const char *cc; 78 int l_value, offset; 79 80 _DIAGASSERT(name != NULL); 81 _DIAGASSERT(value != NULL); 82 83 if (*value == '=') /* no `=' in value */ 84 ++value; 85 l_value = strlen(value); 86 rwlock_wrlock(&__environ_lock); 87 /* find if already exists */ 88 if ((c = __findenv(name, &offset)) != NULL) { 89 if (!rewrite) { 90 rwlock_unlock(&__environ_lock); 91 return (0); 92 } 93 if (strlen(c) >= l_value) { /* old larger; copy over */ 94 while ((*c++ = *value++) != '\0'); 95 rwlock_unlock(&__environ_lock); 96 return (0); 97 } 98 } else { /* create new slot */ 99 int cnt; 100 char **p; 101 102 for (p = environ, cnt = 0; *p; ++p, ++cnt); 103 if (alloced) { /* just increase size */ 104 environ = realloc(environ, 105 (size_t)(sizeof(char *) * (cnt + 2))); 106 if (!environ) { 107 rwlock_unlock(&__environ_lock); 108 return (-1); 109 } 110 } 111 else { /* get new space */ 112 alloced = 1; /* copy old entries into it */ 113 p = malloc((size_t)(sizeof(char *) * (cnt + 2))); 114 if (!p) { 115 rwlock_unlock(&__environ_lock); 116 return (-1); 117 } 118 memcpy(p, environ, cnt * sizeof(char *)); 119 environ = p; 120 } 121 environ[cnt + 1] = NULL; 122 offset = cnt; 123 } 124 for (cc = name; *cc && *cc != '='; ++cc)/* no `=' in name */ 125 continue; 126 if (!(environ[offset] = /* name + `=' + value */ 127 malloc((size_t)((int)(cc - name) + l_value + 2)))) { 128 rwlock_unlock(&__environ_lock); 129 return (-1); 130 } 131 for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); 132 for (*c++ = '='; (*c++ = *value++) != '\0'; ); 133 rwlock_unlock(&__environ_lock); 134 return (0); 135 } 136 137 /* 138 * unsetenv(name) -- 139 * Delete environmental variable "name". 140 */ 141 void 142 unsetenv(name) 143 const char *name; 144 { 145 extern char **environ; 146 char **p; 147 int offset; 148 149 _DIAGASSERT(name != NULL); 150 151 rwlock_wrlock(&__environ_lock); 152 while (__findenv(name, &offset)) /* if set multiple times */ 153 for (p = &environ[offset];; ++p) 154 if (!(*p = *(p + 1))) 155 break; 156 rwlock_unlock(&__environ_lock); 157 } 158