forked from michaelforney/sbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.c
52 lines (46 loc) · 824 Bytes
/
cat.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
/* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-u] [file ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
int fd, ret = 0;
ARGBEGIN {
case 'u':
break;
default:
usage();
} ARGEND
if (!argc) {
if (concat(0, "<stdin>", 1, "<stdout>") < 0)
ret = 1;
} else {
for (; *argv; argc--, argv++) {
if (!strcmp(*argv, "-")) {
*argv = "<stdin>";
fd = 0;
} else if ((fd = open(*argv, O_RDONLY)) < 0) {
weprintf("open %s:", *argv);
ret = 1;
continue;
}
switch (concat(fd, *argv, 1, "<stdout>")) {
case -1:
ret = 1;
break;
case -2:
return 1; /* exit on write error */
}
if (fd != 0)
close(fd);
}
}
return ret;
}