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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int
tee(int fd)
{
char buf[8192];
int read_bytes = 0;
while((read_bytes = read(0, buf, 8192)) > 0) {
if(read_bytes == -1) {
fprintf(stderr, "tee: %s\n", strerror(errno));
return 1;
}
if(write(fd, buf, read_bytes) == -1) {
fprintf(stderr, "tee: %s\n", strerror(errno));
return 1;
}
if(fd != 1)
if(write(1, buf, read_bytes) == -1) {
fprintf(stderr, "tee: %s\n", strerror(errno));
return 1;
}
}
return 0;
}
int
main(int argc, char *argv[])
{
int c;
int append = 0;
int ignore_signt = 0;
int fd;
int return_value = 0;
int FLAGS = O_WRONLY | O_CREAT; /* yeah, it will overwrite the thing if it
* can't read what's in the file, thanks
* POSIX! */
while((c = getopt(argc, argv, "ai")) != -1) {
switch(c) {
case 'a':
append = 1;
break;
case 'i':
ignore_signt = 1;
break;
}
}
if(argc == optind || argv[optind][0] == '-') {
fd = 1;
} else {
if(append)
FLAGS = O_RDWR | O_APPEND; /* Remember what I said? */
fd = open(argv[optind], FLAGS, 0644);
if(fd == -1) {
fprintf(stderr, "tee: %s: %s\n", argv[optind], strerror(errno));
return 1;
}
}
if(ignore_signt)
signal(SIGINT, SIG_IGN);
return_value = tee(fd);
close(fd);
return return_value;
}
|