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