-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrun.c
131 lines (117 loc) · 3.01 KB
/
rrun.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
121
122
123
124
125
126
127
128
129
130
131
#define CACHE_DIR "/home/navn/bin/opt/rrun_cache/"
#define RUST_STDLIB_PATH_ARG "link-args=-Wl,-rpath,/home/navn/nonssd/rust/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <ctype.h>
#define MD5_LENGTH 32
#include <openssl/evp.h>
#define DIGEST_LENGTH 16
typedef struct Digest {
char value[DIGEST_LENGTH * 2 + 1];
} Digest;
Digest calculate_md5(const char * text, int len) {
unsigned char bytes[DIGEST_LENGTH];
EVP_MD_CTX* context = EVP_MD_CTX_new();
EVP_DigestInit_ex(context, EVP_md5(), NULL);
EVP_DigestUpdate(context, text, len);
unsigned int digestLength = DIGEST_LENGTH;
EVP_DigestFinal_ex(context, bytes, &digestLength);
EVP_MD_CTX_free(context);
/* char digest[DIGEST_LENGTH * 2 + 1]; */
Digest digest;
for (int i = 0; i < DIGEST_LENGTH; i++) {
sprintf(&digest.value[i * 2], "%02x", bytes[i]);
}
return digest;
}
//free the return value.
char * read_file(const char *filename, int * length) {
char * buffer = 0;
FILE * file = fopen (filename, "rb");
int len;
if(file) {
fseek (file, 0, SEEK_END);
len = ftell (file);
fseek (file, 0, SEEK_SET);
buffer = malloc (len + 1);
if (buffer)
{
fread (buffer, 1, len, file);
}
fclose (file);
buffer[len] = '\0';
*length = len;
}
else {
*length = -1;
}
return buffer;
}
int main(int argc, char **argv) {
int length;
char * script_path = argv[1];
char * script_text = read_file(script_path, &length);
char * script_name = strrchr(script_path, '/');
if(script_name == NULL) {
script_name = script_path;
}
else {
script_name += 1;
}
Digest digest = calculate_md5(script_text, length);
free(script_text);
script_text = NULL;
const char * calculated_md5 = digest.value;
char * bin_file_path = malloc(sizeof(char) * (strlen(CACHE_DIR) + strlen(script_name) + MD5_LENGTH + 2)); //1 for _, 1 for \0
if(bin_file_path){
strcpy(bin_file_path, CACHE_DIR);
strcat(bin_file_path,script_name);
strcat(bin_file_path,"_");
strcat(bin_file_path, calculated_md5);
}
else {
perror("malloc failed.");
return 1;
}
if(access( bin_file_path, F_OK) != 0 ) {
//file doesn't exist. Create it.
char * kargs[9];
kargs[0] = "rustc";
kargs[1] = script_path;
kargs[2] = "-C";
kargs[3] = "prefer-dynamic";
kargs[4] = "-C";
kargs[5] = RUST_STDLIB_PATH_ARG;
kargs[6] = "-o";
kargs[7] = bin_file_path;
kargs[8] = NULL;
int child_pid = fork();
if (child_pid == 0) {
//inside child
execvp(kargs[0], kargs);
//shouldn't be reached.
perror(kargs[0]);
return 1;
}
//in parent
wait(NULL);
}
//try running the program
char **jargs = malloc(sizeof(char *) * argc);
for(int i=0,j=1; j<argc; i++,j++) { //argv[0] is rrun so ignore it.
jargs[i] = argv[j];
}
jargs[argc - 1] = NULL;
execvp(bin_file_path, jargs);
//shouldn't be reached.
perror(bin_file_path);
free(bin_file_path);
bin_file_path = NULL;
free(jargs);
jargs = NULL;
}