-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetenv_multiple.c
35 lines (30 loc) · 902 Bytes
/
getenv_multiple.c
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
#include <string.h> /* strchr(3), strncmp(3) */
#include "getenv_multiple.h"
/* we declare this as const for race monitoring purposes, to mean that we
won't modify the environment, just read it */
extern const char *const * environ;
#if !1
/* example */
#define N (sizeof env_vars / sizeof *env_vars)
const char
*const env_vars[] = { "ITERM_SESSION_ID", "TERM" },
* env_vals[N] = { NULL };
getenv_multiple(env_vars, env_vals, N);
#endif
extern void __attribute__((access(read_only, 1, 3), access(write_only, 2, 3)))
getenv_multiple(const char *const in[], const char * out[], size_t size)
{
const char *const * envptr = environ;
for (; *envptr; envptr++) {
size_t i = 0;
const char *const equals = strchr(*envptr, '=');
if (!equals)
continue;
for (; i < size; i++) {
if (strncmp(in[i], *envptr, equals - *envptr) == 0) {
out[i] = equals + 1;
break;
}
}
}
}