summaryrefslogtreecommitdiff
path: root/libc/stdlib/putenv.c
blob: b5a202bfd5457be77d15967d8c5b2c6efeaf6533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
 * This file is part of the Linux-8086 C library and is distributed
 * under the GNU Library General Public License.
 */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>

#define ADD_NUM 4

int putenv (char *var)
{
	static char **mall_env = 0;
	static int extras = 0;
	char **p, **d;
	char *r;
	int len;

	r = strchr(var, '=');
	if (r == 0)
		len = strlen(var);
	else
		len = r - var;

	if (!__environ) {
		__environ = (char **) malloc(ADD_NUM * sizeof(char *));
		memset(__environ, 0, sizeof(char *) * ADD_NUM);

		extras = ADD_NUM;
	}

	for (p = __environ; *p; p++) {
		if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') {
			while ((p[0] = p[1]))
				p++;
			extras++;
			break;
		}
	}
	if (r == 0)
		return 0;
	if (extras <= 0) {			/* Need more space */
		d = malloc((p - __environ + 1 + ADD_NUM) * sizeof(char *));

		if (d == 0)
			return -1;

		memcpy((void *) d, (void *) __environ,

			   (p - __environ + 1) * sizeof(char *));
		p = d + (p - __environ);
		extras = ADD_NUM;

		if (mall_env)
			free(mall_env);
		__environ = d;
		mall_env = d;
	}
	*p++ = strdup((char *) var);
	*p = '\0';
	extras--;

	return 0;
}