-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvload.c
49 lines (40 loc) · 1.18 KB
/
envload.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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char **argv) {
if (argc < 3) {
fprintf(stderr, "%s <dump_file> <cmd> [args...]\n", argv[0]);
return 1;
}
FILE *fd = fopen(argv[1], "rb");
fseek(fd, 0, SEEK_END);
long int env_len = ftell(fd);
fseek(fd, 0, SEEK_SET);
char* env = (char*)malloc(env_len);
fread(env, sizeof(char), env_len, fd);
fclose(fd);
unsigned int envp_count = 0;
for (char* ev = env; ev < env + env_len; ev++) {
envp_count++;
ev += strlen(ev);
}
char** envp = (char**)malloc((envp_count + 1) * sizeof(char*));
char* ev = env;
for (unsigned int i = 0; i < envp_count; i++) {
envp[i] = ev;
ev += strlen(ev) + 1;
}
envp[envp_count] = (char*)NULL;
int eargc = argc - 2;
char** eargv = (char**)malloc((eargc + 1) * sizeof(char*));
for (int i = 0; i < eargc; i++)
eargv[i] = argv[i + 2];
eargv[eargc] = (char*)NULL;
execve(argv[2], eargv, envp);
int e = errno;
fprintf(stderr, "Failed to exec!\n");
fprintf(stderr, "%d %s\n", e, strerror(e));
return e;
}