xref: /netbsd-src/lib/libc/stdlib/setenv.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: setenv.c,v 1.22 2003/04/07 13:41:14 kleink 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.22 2003/04/07 13:41:14 kleink 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 #endif
57 
58 #ifdef _REENTRANT
59 extern rwlock_t __environ_lock;
60 #endif
61 
62 extern char **environ;
63 
64 /*
65  * setenv --
66  *	Set the value of the environmental variable "name" to be
67  *	"value".  If rewrite is set, replace any current value.
68  */
69 int
70 setenv(name, value, rewrite)
71 	const char *name;
72 	const char *value;
73 	int rewrite;
74 {
75 	static int alloced;			/* if allocated space before */
76 	char *c;
77 	const char *cc;
78 	size_t l_value;
79 	int offset;
80 
81 	_DIAGASSERT(name != NULL);
82 	_DIAGASSERT(value != NULL);
83 
84 	if (*value == '=')			/* no `=' in value */
85 		++value;
86 	l_value = strlen(value);
87 	rwlock_wrlock(&__environ_lock);
88 	/* find if already exists */
89 	if ((c = __findenv(name, &offset)) != NULL) {
90 		if (!rewrite) {
91 			rwlock_unlock(&__environ_lock);
92 			return (0);
93 		}
94 		if (strlen(c) >= l_value) {	/* old larger; copy over */
95 			while ((*c++ = *value++) != '\0');
96 			rwlock_unlock(&__environ_lock);
97 			return (0);
98 		}
99 	} else {					/* create new slot */
100 		int cnt;
101 		char **p;
102 
103 		for (p = environ, cnt = 0; *p; ++p, ++cnt);
104 		if (alloced) {			/* just increase size */
105 			environ = realloc(environ,
106 			    (size_t)(sizeof(char *) * (cnt + 2)));
107 			if (!environ) {
108 				rwlock_unlock(&__environ_lock);
109 				return (-1);
110 			}
111 		}
112 		else {				/* get new space */
113 			alloced = 1;		/* copy old entries into it */
114 			p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
115 			if (!p) {
116 				rwlock_unlock(&__environ_lock);
117 				return (-1);
118 			}
119 			memcpy(p, environ, cnt * sizeof(char *));
120 			environ = p;
121 		}
122 		environ[cnt + 1] = NULL;
123 		offset = cnt;
124 	}
125 	for (cc = name; *cc && *cc != '='; ++cc)/* no `=' in name */
126 		continue;
127 	if (!(environ[offset] =			/* name + `=' + value */
128 	    malloc((size_t)((int)(cc - name) + l_value + 2)))) {
129 		rwlock_unlock(&__environ_lock);
130 		return (-1);
131 	}
132 	for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
133 	for (*c++ = '='; (*c++ = *value++) != '\0'; );
134 	rwlock_unlock(&__environ_lock);
135 	return (0);
136 }
137