blob: bf618ccab5f48eca62a14c0fbe1674e2a252c865 (
plain)
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
|
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define error(...) ({ \
printf(__VA_ARGS__); \
exit(EXIT_FAILURE); \
})
static void cat(char *s) {
int fd = STDIN_FILENO;
ssize_t in, out;
char buf[1024];
if(strcmp(s, "-")) {
fd = open(s, O_RDONLY);
if(fd < 0)
error("Error opening %s: %s\n", s, strerror(errno));
}
for(;;) {
in = read(fd, buf, sizeof(buf));
if(in < 0)
error("Error reading %s: %s\n", s, strerror(errno));
out = write(STDOUT_FILENO, buf, in);
if(out < in)
error("Error writiing output: %s\n", strerror(errno));
if(!in)
return;
}
}
int main(int argc, char **argv) {
if(argc == 1) {
cat("-");
return 0;
}
while(argc-- > 1)
cat(*(++argv));
return 0;
}
|