-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrepl.c
More file actions
104 lines (86 loc) · 2.56 KB
/
Copy pathcrepl.c
File metadata and controls
104 lines (86 loc) · 2.56 KB
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h>
char RBQCodeBuffer[100000];
char RBQExecBuffer[100000];
char RBQSymbol[100];
int main() {
void *handle;
char *error;
int (*ExecFunction)();
FILE *FangPi;
unsigned int ExecId = 0;
FangPi = fopen("/tmp/crepltmp.c", "w"); // create and empty the file
fclose(FangPi);
while (1) {
if (fgets(RBQCodeBuffer, sizeof(RBQCodeBuffer), stdin) != NULL) {
if (RBQCodeBuffer[0] == 'i' && RBQCodeBuffer[1] == 'n' && RBQCodeBuffer[2] == 't') {
// define function
FangPi = fopen("/tmp/crepltmptest.c", "w");
if (FangPi == NULL) {
printf("ERROR: Cannot open temporary file.\n");
return 1;
}
fprintf(FangPi, "%s", RBQCodeBuffer);
fclose(FangPi);
if (system("gcc -shared -fPIC /tmp/crepltmptest.c -o /tmp/crepltmptest.so -ldl") != 0) {
printf("ERROR: Function definition contains error(s).\n");
continue;
}
FangPi = fopen("/tmp/crepltmp.c", "a+");
if (FangPi == NULL) {
printf("ERROR: Cannot open temporary file.\n");
return 1;
}
fprintf(FangPi, "%s", RBQCodeBuffer);
fclose(FangPi);
}
else {
// Expression
if (RBQCodeBuffer[strlen(RBQCodeBuffer) - 1] == '\n')
RBQCodeBuffer[strlen(RBQCodeBuffer) - 1] = '\0';
RBQExecBuffer[0] = '\0';
RBQSymbol[0] = '\0';
sprintf(RBQSymbol, "__execwrapper_%d", ExecId);
sprintf(RBQExecBuffer, "int %s(){return %s;}", RBQSymbol, RBQCodeBuffer);
ExecId += 1;
if (access("/tmp/crepltmp.c", F_OK) != -1) {
if (system("cp /tmp/crepltmp.c /tmp/crepltmpexec.c") != 0) {
printf("ERROR: Copy failed.\n");
return 1;
}
}
FangPi = fopen("/tmp/crepltmpexec.c", "a+");
if (FangPi == NULL) {
printf("ERROR: Cannot open temporary file.\n");
continue;
}
fprintf(FangPi, "%s", RBQExecBuffer);
fclose(FangPi);
if (system("gcc -shared -fPIC /tmp/crepltmpexec.c -o /tmp/crepltmpexec.so -ldl") != 0) {
printf("ERROR: Compilation failure.\n");
return 1;
}
handle = dlopen("/tmp/crepltmpexec.so", RTLD_LAZY);
if (!handle) {
printf("ERROR: Failed to load temporary module.\n");
return 1;
}
ExecFunction = dlsym(handle, RBQSymbol);
if ((error = dlerror()) != NULL) {
printf("ERROR: Failed to find symbol.\n");
printf("Detail: %s\n", error);
return 1;
}
printf(">> %s = %d\n", RBQCodeBuffer, (*ExecFunction)());
dlclose(handle);
}
}
else {
return 0;
}
}
return 0;
}