summaryrefslogtreecommitdiff
path: root/src/stdlib/getenv.c
blob: a27479981c6d436df898820965c098185d42d32e (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
#include <stdlib.h>
#include <string.h>
#include "_stdlib.h"
#include "_readonly.h"

/** get an environment variable **/

char * getenv(const char * name)
{
	size_t len = 0;
	size_t i = 0;
	static char *variable = NULL;

	SIGNAL_SAFE(0);
	if (variable == NULL) {
		variable = __readonly(RO_ALLOC, "getenv");
	}

	len = strlen(name);
	for (i = 0; __stdlib_h.environ[i] != NULL; i++) {
		if (!strncmp(__stdlib_h.environ[i], name, len) && __stdlib_h.environ[i][len] == '=') {
			__readonly(RO_UNLOCK, variable);
			strcpy(variable, __stdlib_h.environ[i] + len + 1);
			__readonly(RO_LOCK, variable);
			return variable;
		}
	}

	return NULL;
}

/***
read the environment variable ARGUMENT(name) from the host environment.
***/

/*
UNDEFINED(Modifying the returned string)
IMPLEMENTATION(The set of environment variable names)
IMPLEMENTATION(The method of altering the environment)
RETURN_FAILURE(CONSTANT(NULL))
RETURN_SUCCESS(a pointer to the environment string)
STDC(1)
*/