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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
int
libexec_putenv(struct libexec_command *cmd, enum libexec_insert_mode how, const char *string)
{
char **env;
char *copy, *eqsign;
size_t i, n, len;
int environ_copied = 0;
void *new;
if (!cmd || !string) {
errno = EINVAL;
return -1;
}
env = cmd->environ ? cmd->environ : environ;
if (how == LIBEXEC_APPEND || how == LIBEXEC_PREPEND) {
n = 0;
while (env[n])
n++;
just_add:
copy = strdup(string);
if (!copy)
return -1;
if (!cmd->environ) {
if (libexec_copy_environ(cmd, NULL)) {
free(copy);
return -1;
}
environ_copied = 1;
}
new = realloc(cmd->environ, (n + 2) * sizeof(*cmd->environ));
if (!new) {
if (environ_copied) {
while (n)
free(cmd->environ[--n]);
free(cmd->environ);
cmd->environ = NULL;
}
free(copy);
return -1;
}
cmd->environ = new;
if (how == LIBEXEC_APPEND) {
cmd->environ[n] = copy;
cmd->environ[n + 1] = NULL;
} else {
memmove(&cmd->environ[1], cmd->environ, (n + 1) * sizeof(*cmd->environ));
cmd->environ[0] = copy;
}
} else {
eqsign = strchr(string, '=');
if (!eqsign || (how != LIBEXEC_REPLACE && how != LIBEXEC_NOCLOBBER && how != LIBEXEC_NOREPLACE)) {
errno = EINVAL;
return -1;
}
len = (size_t)(eqsign - string) + 1;
for (i = 0; env[i]; i++)
if (!strncmp(env[i], string, len))
break;
if (!env[i]) {
n = i;
how = LIBEXEC_APPEND;
goto just_add;
}
if (how == LIBEXEC_NOREPLACE || !strcmp(env[i], string)) {
if (!cmd->environ)
if (libexec_copy_environ(cmd, NULL))
return -1;
return 0;
}
if (how == LIBEXEC_NOCLOBBER)
return -1; /* do not change errno */
copy = strdup(string);
if (!copy)
return -1;
if (!cmd->environ) {
if (libexec_copy_environ(cmd, NULL)) {
free(copy);
return -1;
}
}
free(cmd->environ[i]);
cmd->environ[i] = copy;
}
return 0;
}
#else
int
main(void)
{
/* Correct usage is tested via
* libexec_putenv_append.c,
* libexec_putenv_noclobber.c,
* libexec_putenv_noreplace.c,
* libexec_putenv_prepend.c, and
* libexec_putenv_replace.c */
struct libexec_command cmd, ref;
cmd = LIBEXEC_COMMAND_INIT;
memcpy(&ref, &cmd, sizeof(cmd));
errno = 0;
ASSERT_EQ_INT(libexec_putenv(&cmd, (enum libexec_insert_mode)-1, "A=B"), -1);
ASSERT_EQ_INT(errno, EINVAL);
ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
errno = 0;
ASSERT_EQ_INT(libexec_putenv(&cmd, (enum libexec_insert_mode)LIBEXEC_INSERT_MODE__COUNT__, "A=B"), -1);
ASSERT_EQ_INT(errno, EINVAL);
ASSERT_IS_TRUE(!memcmp(&cmd, &ref, sizeof(cmd)));
return 0;
}
#endif
|