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