-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysfuck.c
94 lines (74 loc) · 1.48 KB
/
sysfuck.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "sysfuck.h"
#include "callbacks.h"
buf_t buf;
char *data = buf.data;
int g_argc;
char **g_argv;
FILE *sf_in;
FILE *sf_out;
int prog_stdout = 1;
int main (int argc, char **argv) {
g_argc = argc;
g_argv = argv;
sf_in = fdopen(3, "r");
sf_out = fdopen(4, "a");
if (!sf_in || !sf_out) {
fprintf(stderr,
"Error: sysfuck needs to be able to read from file "
"descriptor 3 and write to file descriptor 4.\n"
"You probably want to be using `sfwrap` instead.\n");
exit(1);
}
char *callname = NULL;
size_t n = 0;
while (1) {
int c;
while ((c = getc(sf_in))) {
if (c == EOF)
return 0;
else
write(prog_stdout, &c, 1);
}
int bytesread = getdelim(&callname, &n, 0, sf_in);
if (bytesread == -1)
return 0;
else if (bytesread == 1) {
char null = 0;
write(prog_stdout, &null, 1);
continue;
}
callback_t f = getcallback(callname);
int n = getarg(data);
(*f)(callname, n, &buf);
fflush(sf_out);
}
return 0;
}
int getarg (char *store) {
memset(store, 0, 255);
int arglen = getc(sf_in);
if (arglen == -1)
return 0;
char *p = store;
while (p - store < arglen) {
int c = getc(sf_in);
if (c == EOF) break;
*p++ = c;
}
return arglen;
}
void sendbytes (void *src, int n) {
char *bytes = src;
int i;
for (i = 0; i < n; i++) {
putc(bytes[i], sf_out);
}
}
void sendlong (long n) {
sendbytes(&n, 4);
}