summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/getenv.c
blob: 41517e2f8a2c36a01b93cf936d5802a080bc62c9 (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
#include <atomic.h> // for LIBC_UNLOCK, LIBC_LOCK
#include <libc.h>   // for libc, libc::(anonymous), weak_reference
#include <stddef.h> // for NULL
#include <stdlib.h> // for getenv, secure_getenv

extern char **environ;

char *getenv(const char *name)
{
	LIBC_LOCK(libc.lock.environ);

	for (char **env = environ; *env; env++) {
		char *eq = NULL;
		for (char *p = *env; *p; p++) {
			if (*p == '=') {
				eq = p;
				break;
			}
			if (name[p - *env] != *p) {
				break;
			}
		}
		if (eq && name[eq - *env] == '\0') {
			LIBC_UNLOCK(libc.lock.environ);
			return eq + 1;
		}
	}

	LIBC_UNLOCK(libc.lock.environ);

	return NULL;
}

weak_reference(getenv, secure_getenv);