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
|
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "panic.h"
void cat(int fd, char * name)
{
char buffer[4096];
ssize_t length;
while (0 < (length=read(fd, buffer, sizeof(buffer))))
if (length != write(1, buffer, length))
panic("write: %s: %s", name, strerror(errno));
if (0 > length)
panic("read: %s: %s", name, strerror(errno));
}
int main(int argc, char * argv[])
{
int i;
int fd;
if (1 == argc)
cat(0, "<stdin>");
else for (i = 1; i < argc; ++i)
{
fd = open(argv[i], O_RDONLY);
if (0 > fd)
panic("open: %s: %s", argv[i], strerror(errno));
else
{
cat(fd, argv[i]);
close(fd);
}
}
}
|