-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv-set.c
120 lines (103 loc) · 3.19 KB
/
env-set.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
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
// Clears the environment, then reads argv to set
// environment variables before passing exec()'ing
// onto the next command. Requires explicit '--'
// separator between environment variables, which
// are KEY=VALUE strings, and the command to run
// (and its further arguments.)
#define _GNU_SOURCE
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PARSE_ENV 1
#define PARSE_ARG 2
int main(int argc, char const *argv[])
{
// set up variables we need
// we allocate arrays that shouldn't ever have more than argc entries, I hope
char **passed_argv = malloc(sizeof(char *) * argc);
char **passed_envp = malloc(sizeof(char *) * argc);
int passed_argv_idx = 0;
int passed_envp_idx = 0;
// process argv to see what we have
int parseState = PARSE_ENV;
int argIter = 1;
while (argv[argIter])
{
const char *arg = argv[argIter++];
size_t len = strlen(arg);
char *dest = malloc(len + 1);
if (dest == NULL)
{
fprintf(stderr, "failed to allocate %ld bytes\n", len);
abort();
}
strcpy(dest, arg);
switch (parseState)
{
case PARSE_ENV:
if (!strcmp(arg, "--"))
{
parseState = PARSE_ARG;
free(dest); // we don't actually need this anymore
break;
}
passed_envp[passed_envp_idx++] = dest;
{
// attempt to figure out if this is PATH and so should be specially handled
char *eqSep = strchr(dest, '=');
if (eqSep == NULL)
{
fprintf(stderr, "found environment variable without =?: %s\n", dest);
return 1;
}
size_t size = eqSep - dest; // no +1 because we don't want to take =
char *envName = malloc(size + 1); // +1 to null terminate later
strncpy(envName, dest, size);
*(envName + size + 1) = '\0'; // null terminate
// envValue is just eqSep + 1 to the end
char *envValue = malloc(strlen(eqSep + 1) + 1);
if (envValue == NULL)
{
fprintf(stderr, "failed to allocate %ld bytes\n", strlen(eqSep + 1) + 1);
abort();
}
strcpy(envValue, eqSep + 1);
if (!strcmp(envName, "PATH"))
{
// this is PATH, modify the PATH for this binary too
// so that we can execvpe later and it will look at the current
// PATH that it's running in
if (setenv(envName, envValue, 1))
{
fprintf(stderr, "error setting PATH: errno %d: %s\n", errno, strerror(errno));
abort();
}
}
free(envName);
free(envValue);
}
break;
case PARSE_ARG:
passed_argv[passed_argv_idx++] = dest;
break;
default:
fprintf(stderr, "shouldn't reach here\n");
abort();
}
}
if (passed_argv_idx < 1)
{
fprintf(stderr, "no command specified for env-set to run\n");
return 1;
}
// null terminate the arrays
passed_argv[passed_argv_idx] = NULL;
passed_envp[passed_envp_idx] = NULL;
// execvpe the command
execvpe(passed_argv[0], passed_argv, passed_envp);
fprintf(stderr, "failed to execvpe %s: errno %d: %s\n", passed_argv[0], errno, strerror(errno));
return 1;
}