Line | |
---|
1 | /* tee - pipe fitting Author: Paul Polderman */
|
---|
2 |
|
---|
3 | #include <sys/types.h>
|
---|
4 | #include <fcntl.h>
|
---|
5 | #include <signal.h>
|
---|
6 | #include <unistd.h>
|
---|
7 | #include <stdlib.h>
|
---|
8 | #include <minix/minlib.h>
|
---|
9 |
|
---|
10 | #define MAXFD 18
|
---|
11 | #define CHUNK_SIZE 4096
|
---|
12 |
|
---|
13 | int fd[MAXFD];
|
---|
14 |
|
---|
15 | _PROTOTYPE(int main, (int argc, char **argv));
|
---|
16 |
|
---|
17 | int main(argc, argv)
|
---|
18 | int argc;
|
---|
19 | char **argv;
|
---|
20 | {
|
---|
21 | char iflag = 0, aflag = 0;
|
---|
22 | char buf[CHUNK_SIZE];
|
---|
23 | int i, s, n;
|
---|
24 |
|
---|
25 | argv++;
|
---|
26 | --argc;
|
---|
27 | while (argc > 0 && argv[0][0] == '-') {
|
---|
28 | switch (argv[0][1]) {
|
---|
29 | case 'i': /* Interrupt turned off. */
|
---|
30 | iflag++;
|
---|
31 | break;
|
---|
32 | case 'a': /* Append to outputfile(s), instead of
|
---|
33 | * overwriting them. */
|
---|
34 | aflag++;
|
---|
35 | break;
|
---|
36 | default:
|
---|
37 | std_err("Usage: tee [-i] [-a] [files].\n");
|
---|
38 | exit(1);
|
---|
39 | }
|
---|
40 | argv++;
|
---|
41 | --argc;
|
---|
42 | }
|
---|
43 | fd[0] = 1; /* Always output to stdout. */
|
---|
44 | for (s = 1; s < MAXFD && argc > 0; --argc, argv++, s++) {
|
---|
45 | if (aflag && (fd[s] = open(*argv, O_RDWR)) >= 0) {
|
---|
46 | lseek(fd[s], 0L, SEEK_END);
|
---|
47 | continue;
|
---|
48 | } else {
|
---|
49 | if ((fd[s] = creat(*argv, 0666)) >= 0) continue;
|
---|
50 | }
|
---|
51 | std_err("Cannot open output file: ");
|
---|
52 | std_err(*argv);
|
---|
53 | std_err("\n");
|
---|
54 | exit(2);
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (iflag) signal(SIGINT, SIG_IGN);
|
---|
58 |
|
---|
59 | while ((n = read(0, buf, CHUNK_SIZE)) > 0) {
|
---|
60 | for (i = 0; i < s; i++) write(fd[i], buf, n);
|
---|
61 | }
|
---|
62 |
|
---|
63 | for (i = 0; i < s; i++) /* Close all fd's */
|
---|
64 | close(fd[i]);
|
---|
65 | return(0);
|
---|
66 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.