-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronsh_loadenv.c
57 lines (50 loc) · 918 Bytes
/
cronsh_loadenv.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#define SETENV_OVERWRITE 1
int main(int argc, char** argv)
{
FILE * envfile;
char buf[4096];
char * s;
char * val;
if(argc > 2)
{
if(envfile = fopen(argv[1], "r"))
{
while(fgets(buf, sizeof(buf), envfile))
{
if((buf[0] != '#') && (s = strchr(buf, '=')))
{
*s = '\0';
val = s + 1;
if(s = strchr(val, '\n')) *s = '\0';
if(strlen(val) > 0) {
if(setenv(buf, val, SETENV_OVERWRITE)!=0) goto fail;
}
else {
if(unsetenv(buf)!=0) goto fail;
}
}
}
}
else
{
if(errno != ENOENT) /* ignore if file does not exist */
{
perror("fopen");
goto fail;
}
}
execvp(argv[2], &argv[2]);
goto fail;
}
else
{
warnx("Usage: loadenv [env-file] [command] [arguments [...]]");
}
fail:
return errno == 0 ? -1 : errno;
}