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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
const char *
libexec_getenv(struct libexec_command *cmd, const char *name)
{
char **env;
size_t i, len;
if (!cmd || !name) {
errno = EINVAL;
return NULL;
}
if (strchr(name, '='))
return NULL;
env = cmd->environ ? cmd->environ : environ;
len = strlen(name);
for (i = 0; env[i]; i++)
if (!strncmp(env[i], name, len) && env[i][len] == '=')
return &env[i][len + 1];
return NULL;
}
#else
int
main(void)
{
struct libexec_command cmd, ref;
char **env;
cmd = LIBEXEC_COMMAND_INIT;
memcpy(&ref, &cmd, sizeof(cmd));
env = environ;
environ = calloc(12, sizeof(*environ));
ASSERT_NOT_NULL(environ);
environ[0] = strdup("AA=aa");
environ[1] = strdup("A=a0");
environ[2] = strdup("B=b0");
environ[3] = strdup("A=a1");
environ[4] = strdup("B=b1");
environ[5] = strdup("A=a2");
environ[6] = strdup("B=b2");
environ[7] = strdup("A=a3");
environ[8] = strdup("B=b3");
environ[9] = strdup("BB=bb");
environ[10] = strdup("X=Y=Z");
ASSERT_NOT_NULL(environ[0]);
ASSERT_NOT_NULL(environ[1]);
ASSERT_NOT_NULL(environ[2]);
ASSERT_NOT_NULL(environ[3]);
ASSERT_NOT_NULL(environ[4]);
ASSERT_NOT_NULL(environ[5]);
ASSERT_NOT_NULL(environ[6]);
ASSERT_NOT_NULL(environ[7]);
ASSERT_NOT_NULL(environ[8]);
ASSERT_NOT_NULL(environ[9]);
ASSERT_NOT_NULL(environ[10]);
ASSERT_IS_NULL(environ[11]);
errno = 0;
ASSERT_IS_NULL(libexec_getenv(&cmd, "PATH"));
ASSERT_IS_TRUE(errno == 0);
errno = 0;
ASSERT_IS_NULL(libexec_getenv(NULL, "x"));
ASSERT_EQ_INT(errno, EINVAL);
errno = 0;
ASSERT_IS_NULL(libexec_getenv(&cmd, "x"));
ASSERT_IS_TRUE(errno == 0);
errno = 0;
ASSERT_IS_NULL(libexec_getenv(&cmd, NULL));
ASSERT_EQ_INT(errno, EINVAL);
errno = 0;
ASSERT_IS_NULL(libexec_getenv(&cmd, "X=Y"));
ASSERT_IS_TRUE(errno == 0 || errno == EINVAL);
errno = 0;
ASSERT_IS_NULL(libexec_getenv(&cmd, ""));
ASSERT_IS_TRUE(errno == 0);
ASSERT_EQ_STR(libexec_getenv(&cmd, "X"), "Y=Z");
ASSERT_EQ_STR(libexec_getenv(&cmd, "AA"), "aa");
ASSERT_EQ_STR(libexec_getenv(&cmd, "BB"), "bb");
ASSERT_EQ_STR(libexec_getenv(&cmd, "A"), "a0");
ASSERT_EQ_STR(libexec_getenv(&cmd, "B"), "b0");
ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
libexec_destroy_command(&cmd);
ASSERT_IS_NULL(libexec_getenv(&cmd, "PATH"));
ASSERT_IS_NULL(libexec_getenv(NULL, "x"));
ASSERT_IS_NULL(libexec_getenv(&cmd, "x"));
ASSERT_IS_NULL(libexec_getenv(&cmd, NULL));
ASSERT_IS_NULL(libexec_getenv(&cmd, "X=Y"));
ASSERT_IS_NULL(libexec_getenv(&cmd, ""));
ASSERT_EQ_STR(libexec_getenv(&cmd, "X"), "Y=Z");
ASSERT_EQ_STR(libexec_getenv(&cmd, "AA"), "aa");
ASSERT_EQ_STR(libexec_getenv(&cmd, "BB"), "bb");
ASSERT_EQ_STR(libexec_getenv(&cmd, "A"), "a0");
ASSERT_EQ_STR(libexec_getenv(&cmd, "B"), "b0");
libexec_set_environ(&cmd, environ);
environ = env;
return 0;
}
#endif
|