* reordered a little bit
[mascara-docs.git] / i86 / elks / elkscmd / minix3 / tee.c
blob3dcc3048e8c3412f0b51a0ec17f6e0b589d2c0c4
1 /* tee - pipe fitting Author: Paul Polderman */
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <fcntl.h>
6 #include <signal.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #if 0
10 #include <minix/minlib.h>
11 #endif
12 #include "defs.h"
14 #define MAXFD 18
15 #define CHUNK_SIZE 4096
17 int fd[MAXFD];
19 _PROTOTYPE(int main, (int argc, char **argv));
21 int main(argc, argv)
22 int argc;
23 char **argv;
25 char iflag = 0, aflag = 0;
26 char buf[CHUNK_SIZE];
27 int i, s, n;
29 argv++;
30 --argc;
31 while (argc > 0 && argv[0][0] == '-') {
32 switch (argv[0][1]) {
33 case 'i': /* Interrupt turned off. */
34 iflag++;
35 break;
36 case 'a': /* Append to outputfile(s), instead of
37 * overwriting them. */
38 aflag++;
39 break;
40 default:
41 fprintf(stderr,"Usage: tee [-i] [-a] [files].\n");
42 exit(1);
44 argv++;
45 --argc;
47 fd[0] = 1; /* Always output to stdout. */
48 for (s = 1; s < MAXFD && argc > 0; --argc, argv++, s++) {
49 if (aflag && (fd[s] = open(*argv, O_RDWR)) >= 0) {
50 lseek(fd[s], 0L, SEEK_END);
51 continue;
52 } else {
53 if ((fd[s] = creat(*argv, 0666)) >= 0) continue;
55 fprintf(stderr,"Cannot open output file: ");
56 fprintf(stderr,*argv);
57 fprintf(stderr,"\n");
58 exit(2);
61 if (iflag) signal(SIGINT, SIG_IGN);
63 while ((n = read(0, buf, CHUNK_SIZE)) > 0) {
64 for (i = 0; i < s; i++) write(fd[i], buf, n);
67 for (i = 0; i < s; i++) /* Close all fd's */
68 close(fd[i]);
69 return(0);