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