summaryrefslogtreecommitdiffstats
path: root/usr/klibc/__put_env.c
blob: 30d415c96dca39a75ec1f08caf2d3f65fea67b8f (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
66
67
68
69
70
71
72
73
74
/*
 * __put_env.c - common code for putenv() and setenv()
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "env.h"

static size_t __environ_size;
static char **__environ_alloc;

/* str should be a duplicated version of the input string;
   len is the length of the key including the = sign */

int __put_env(char *str, size_t len, int overwrite)
{
	static char *const null_environ = { NULL };
	char **p, *q;
	char **newenv;
	size_t n;

	if (!environ)
		environ = (char **)null_environ;

	n = 1;			/* Include space for final NULL */
	for (p = environ; (q = *p); p++) {
		n++;
		if (!strncmp(q, str, len)) {
			if (!overwrite)
				free(str);
			else
				*p = str;	/* Possible memory leak... */
			return 0;
		}
	}

	if (__environ_alloc && environ != __environ_alloc) {
		free(__environ_alloc);
		__environ_alloc = NULL;
	}

	/* Need to extend the environment */
	if (n < __environ_size) {
		p[1] = NULL;
		*p = str;
		return 0;
	} else {
		if (__environ_alloc) {
			newenv =
			    realloc(__environ_alloc,
				    (__environ_size << 1) * sizeof(char *));
			if (!newenv)
				return -1;

			__environ_size <<= 1;
		} else {
			/* Make a reasonable guess how much more space
			   we need */
			size_t newsize = n + 32;
			newenv = malloc(newsize * sizeof(char *));
			if (!newenv)
				return -1;

			memcpy(newenv, environ, n * sizeof(char *));
			__environ_size = newsize;
		}
		newenv[n-1] = str;	/* Old NULL position */
		newenv[n]   = NULL;
		environ	= newenv;
	}
	return 0;
}