-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunker.c
47 lines (40 loc) · 1.23 KB
/
chunker.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
#include <stdio.h>
#include <stdlib.h>
#define EC_EOF_REACHED 1
#define EC_LIMIT_REACHED 0
#define EC_ERROR 2
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "No chunk size given!\n");
return EC_ERROR;
}
// how big are the chunks?
long unsigned int chunk_size = strtoul(argv[1], NULL, 10);
// how much data is (possibly) left to process?
long unsigned int left = chunk_size;
int returncode = EC_LIMIT_REACHED;
// allocate an input buffer
void *buffer = malloc( BUFFER_SIZE );
int readBytes = 0;
while (left > 0) {
/* how many bytes can we read? We cannot read more than
* our buffer can handle or our limit allows.
*/
int toread = BUFFER_SIZE < left ? BUFFER_SIZE : left;
readBytes = read( 0, buffer, toread );
if (readBytes == 0) {
returncode = EC_EOF_REACHED;
break;
} else if (readBytes < 0) {
returncode = EC_LIMIT_REACHED;
fprintf(stderr, "Read error\n");
break;
} else {
write(1, buffer, readBytes );
left -= readBytes;
}
}
free(buffer);
return returncode;
}