xref: /illumos-gate/usr/src/cmd/make/lib/vroot/setenv.cc (revision 10d63b7d)
1*10d63b7dSRichard Lowe /*
2*10d63b7dSRichard Lowe  * CDDL HEADER START
3*10d63b7dSRichard Lowe  *
4*10d63b7dSRichard Lowe  * The contents of this file are subject to the terms of the
5*10d63b7dSRichard Lowe  * Common Development and Distribution License (the "License").
6*10d63b7dSRichard Lowe  * You may not use this file except in compliance with the License.
7*10d63b7dSRichard Lowe  *
8*10d63b7dSRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10d63b7dSRichard Lowe  * or http://www.opensolaris.org/os/licensing.
10*10d63b7dSRichard Lowe  * See the License for the specific language governing permissions
11*10d63b7dSRichard Lowe  * and limitations under the License.
12*10d63b7dSRichard Lowe  *
13*10d63b7dSRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
14*10d63b7dSRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10d63b7dSRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
16*10d63b7dSRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
17*10d63b7dSRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10d63b7dSRichard Lowe  *
19*10d63b7dSRichard Lowe  * CDDL HEADER END
20*10d63b7dSRichard Lowe  */
21*10d63b7dSRichard Lowe /*
22*10d63b7dSRichard Lowe  * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
23*10d63b7dSRichard Lowe  * Use is subject to license terms.
24*10d63b7dSRichard Lowe  */
25*10d63b7dSRichard Lowe 
26*10d63b7dSRichard Lowe 
27*10d63b7dSRichard Lowe #include <stdlib.h>
28*10d63b7dSRichard Lowe #include <string.h>
29*10d63b7dSRichard Lowe #include <unistd.h>
30*10d63b7dSRichard Lowe 
31*10d63b7dSRichard Lowe extern	char	**environ;
32*10d63b7dSRichard Lowe 
33*10d63b7dSRichard Lowe static	short	setenv_made_new_vector= 0;
34*10d63b7dSRichard Lowe 
setenv(char * name,char * value)35*10d63b7dSRichard Lowe char *setenv(char *name, char *value)
36*10d63b7dSRichard Lowe {	char *p= NULL, **q;
37*10d63b7dSRichard Lowe 	int length= 0, vl;
38*10d63b7dSRichard Lowe 
39*10d63b7dSRichard Lowe 	if ((p= getenv(name)) == NULL) {	/* Allocate new vector */
40*10d63b7dSRichard Lowe 		for (q= environ; *q != NULL; q++, length++);
41*10d63b7dSRichard Lowe 		q= (char **)malloc((unsigned)(sizeof(char *)*(length+2)));
42*10d63b7dSRichard Lowe                 memcpy(((char *)q)+sizeof(char *), (char *)environ, sizeof(char *)*(length+1));
43*10d63b7dSRichard Lowe 		if (setenv_made_new_vector++)
44*10d63b7dSRichard Lowe 			free((char *)environ);
45*10d63b7dSRichard Lowe 		length= strlen(name);
46*10d63b7dSRichard Lowe 		environ= q;}
47*10d63b7dSRichard Lowe 	else { /* Find old slot */
48*10d63b7dSRichard Lowe 		length= strlen(name);
49*10d63b7dSRichard Lowe 		for (q= environ; *q != NULL; q++)
50*10d63b7dSRichard Lowe 			if (!strncmp(*q, name, length))
51*10d63b7dSRichard Lowe 				break;};
52*10d63b7dSRichard Lowe 	vl= strlen(value);
53*10d63b7dSRichard Lowe 	if (!p || (length+vl+1 > strlen(p)))
54*10d63b7dSRichard Lowe 		*q= p= (char *) malloc((unsigned)(length+vl+2));
55*10d63b7dSRichard Lowe 	else
56*10d63b7dSRichard Lowe 		p= *q;
57*10d63b7dSRichard Lowe 	(void)strcpy(p, name); p+= length;
58*10d63b7dSRichard Lowe 	*p++= '=';
59*10d63b7dSRichard Lowe 	(void)strcpy(p, value);
60*10d63b7dSRichard Lowe 	return(value);
61*10d63b7dSRichard Lowe }
62