-
Notifications
You must be signed in to change notification settings - Fork 1
/
reprint.c
53 lines (40 loc) · 959 Bytes
/
reprint.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
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <reprint/reprint.h>
int main(int argc, const char* argv[]){
if(argc < 3){
fprintf(stderr, "Usage: %s DATA_LEN EXPR\n", argv[0]);
return 1;
}
/* Get expected length of data. */
unsigned len = strtoul(argv[1], NULL, 10);
/* Just limit to 8k, for the hell of it and my lack of time. */
if(len > 8192)
return 2;
/* Read data from stdin. */
uint8_t buffer[len];
unsigned acc = 0;
while(acc < len){
int more = read(0, buffer + acc, len - acc);
if(more < 0)
return 3;
if(more == 0)
return 4;
acc += more;
}
close(0);
reprint_state rs;
reprint_init(&rs, argv[2], buffer);
uint8_t output[8192];
int ret = reprint_cb(&rs, output, sizeof(output));
if(ret < 0)
return 5;
/* This is supposed to be simple....just bail on overlong output */
if(sizeof(output) == ret)
return 6;
if(write(1, output, ret))
return 7;
return 0;
}