vm: restore stacktrace on SIGSEGV
[minix.git] / commands / tee / tee.c
blob54fadb20e74fbfdfbac0249d781835d185a7ecef
1 /* tee - pipe fitting Author: Paul Polderman */
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>
10 #define MAXFD 18
11 #define CHUNK_SIZE 4096
13 int fd[MAXFD];
15 int main(int argc, char **argv);
17 int main(argc, argv)
18 int argc;
19 char **argv;
21 char iflag = 0, aflag = 0;
22 char buf[CHUNK_SIZE];
23 int i, s, n;
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);
40 argv++;
41 --argc;
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;
51 std_err("Cannot open output file: ");
52 std_err(*argv);
53 std_err("\n");
54 exit(2);
57 if (iflag) signal(SIGINT, SIG_IGN);
59 while ((n = read(0, buf, CHUNK_SIZE)) > 0) {
60 for (i = 0; i < s; i++) write(fd[i], buf, n);
63 for (i = 0; i < s; i++) /* Close all fd's */
64 close(fd[i]);
65 return(0);