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
|
#include "../lib/io/io.h"
#include "../lib/container/container.h"
#include "../lib/cstr/cstr.h"
#include "../lib/tctl/tctl.h"
#define BUFFER_SIZE 128
char buf[BUFFER_SIZE];
int main(int argc, const char **argv)
{
u64 size;
if (argc > 3) {
wf(STDERR_FD, "write [-nobuf] <filename>\n");
return -1;
}
if (argc == 3) {
if (cstr_compare(argv[1], "-nobuf") == 0) {
int fd = open(argv[2], OPEN_WRITE_ONLY | OPEN_CREATE, MODE_USER_READ | MODE_USER_WRITE | MODE_OTHER_READ | MODE_GROUP_READ);
while ((size = read(STDIN_FD, buf, BUFFER_SIZE))) {
write(fd, buf, BUFFER_SIZE);
}
close(fd);
} else {
wstd("write [-nobuf] <filename>\n");
return -1;
}
} else {
int fd = open(argv[1], OPEN_WRITE_ONLY | OPEN_CREATE, MODE_USER_READ | MODE_USER_WRITE | MODE_OTHER_READ | MODE_GROUP_READ);
container_t *string = new_container(sizeof(char));
while ((size = read(STDIN_FD, buf, BUFFER_SIZE))) {
container_append(string, buf, size);
}
container_push(string, 0);
write(fd, string->data, string->size);
close(fd);
}
return 0;
}
|