1#ifndef lint
2static char *rcsid = "$Id: setenv.c,v 1.1 2003/06/04 00:27:01 marka Exp $";
3#endif
4
5/*
6 * Copyright (c) 2002 Japan Network Information Center.
7 * All rights reserved.
8 *
9 * By using this file, you agree to the terms and conditions set forth bellow.
10 *
11 * 			LICENSE TERMS AND CONDITIONS
12 *
13 * The following License Terms and Conditions apply, unless a different
14 * license is obtained from Japan Network Information Center ("JPNIC"),
15 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
16 * Chiyoda-ku, Tokyo 101-0047, Japan.
17 *
18 * 1. Use, Modification and Redistribution (including distribution of any
19 *    modified or derived work) in source and/or binary forms is permitted
20 *    under this License Terms and Conditions.
21 *
22 * 2. Redistribution of source code must retain the copyright notices as they
23 *    appear in each source code file, this License Terms and Conditions.
24 *
25 * 3. Redistribution in binary form must reproduce the Copyright Notice,
26 *    this License Terms and Conditions, in the documentation and/or other
27 *    materials provided with the distribution.  For the purposes of binary
28 *    distribution the "Copyright Notice" refers to the following language:
29 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
30 *
31 * 4. The name of JPNIC may not be used to endorse or promote products
32 *    derived from this Software without specific prior written approval of
33 *    JPNIC.
34 *
35 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
36 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
39 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
44 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 */
47
48#include <stddef.h>
49#include <string.h>
50
51/*
52 * We don't include <stdlib.h> here.
53 * Also <stdlib.h> may declare `environ' and its type might be different
54 * from ours.
55 */
56extern char **environ;
57
58typedef struct myenv myenv_t;
59
60struct myenv {
61	char *pointer;
62	myenv_t *next;
63	myenv_t *prev;
64};
65
66static myenv_t *myenvs = NULL;
67
68void
69myunsetenv(const char *name) {
70	char **e;
71	myenv_t *mye;
72	size_t namelen;
73	extern void free(void *);
74
75	namelen = strlen(name);
76	for (e = environ; *e != NULL; e++) {
77		if (strncmp(*e, name, namelen) == 0 && (*e)[namelen] == '=')
78			break;
79	}
80	if (*e == NULL)
81		return;
82
83	for (mye = myenvs; mye != NULL; mye = mye->next) {
84		if (mye->pointer == *e) {
85			if (mye->next != NULL)
86				mye->next->prev = mye->prev;
87			if (mye->prev != NULL)
88				mye->prev->next = mye->next;
89			if (mye->next == NULL && mye->prev == NULL)
90				myenvs = NULL;
91			free(mye);
92			free(*e);
93			break;
94		}
95	}
96
97	for ( ; *e != NULL; e++)
98		*e = *(e + 1);
99}
100
101#include <stdlib.h>
102
103int
104mysetenv(const char *name, const char *value, int overwrite) {
105	myenv_t *mye;
106	char *buffer;
107	int result;
108
109	if (getenv(name) != NULL && !overwrite)
110		return 0;
111
112	buffer = (char *) malloc(strlen(name) + strlen(value) + 2);
113	if (buffer == NULL)
114		return -1;
115	strcpy(buffer, name);
116	strcat(buffer, "=");
117	strcat(buffer, value);
118
119	myunsetenv(name);
120
121	mye = (myenv_t *) malloc(sizeof(myenv_t));
122	if (mye == NULL)
123		return -1;
124	mye->pointer = buffer;
125	mye->next = myenvs;
126	mye->prev = NULL;
127	if (myenvs != NULL)
128		myenvs->prev = mye;
129	myenvs = mye;
130
131	result = putenv(buffer);
132
133	return result;
134}
135