-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrusty.c
39 lines (31 loc) · 956 Bytes
/
crusty.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#if !defined(__unix__)
#error "This platform is currently not supported"
#else
#include <sys/stat.h>
#endif
int main(int argc, char **argv) {
/* print usage */
if(argc < 2) {
/* TODO: this is possibly not cross-platform */
fprintf(stderr, "%s: [1;31merror[0m: no input files\n", argv[0]);
return 1;
}
extern const char _binary_stub_start, _binary_stub_end;
const char *stub_start = &_binary_stub_start;
const char *stub_end = &_binary_stub_end;
FILE *source = fopen(argv[1], "r");
FILE *output = fopen("main", "wb");
fwrite(stub_start, stub_end - stub_start, 1, output);
fclose(output);
/* TODO: this may not be cross-platform */
mode_t mode = (S_IRWXU) | (S_IRGRP | S_IXGRP) | (S_IROTH | S_IXOTH);
if(chmod("main", mode)) {
fprintf(stderr, "[1;31minternal error[0m: failed to chmod main\n");
return 1;
}
fclose(source);
return 0;
}