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