3 * Copyright (c) 1996, Paul Slootman
5 * Author: Paul Slootman
6 * (paul@wurtel.hobby.nl, paul@murphy.nl, paulS@toecompst.nl)
8 * This source code is released into the public domain. It is provided on an
9 * as-is basis and no responsibility is accepted for its failure to perform
10 * as expected. It is worth at least as much as you paid for it!
12 * tee.c - pipe fitting
14 * tee reads stdin, and writes what it reads to each of the specified
15 * files. The primary reason of existence for this version is a quick
16 * and dirty implementation to distribute with Vim, to make one of the
17 * most useful features of Vim possible on OS/2: quickfix.
19 * Of course, not using tee but instead redirecting make's output directly
20 * into a temp file and then processing that is possible, but if we have a
21 * system capable of correctly piping (unlike DOS, for example), why not
22 * use it as well as possible? This tee should also work on other systems,
23 * but it's not been tested there, only on OS/2.
25 * tee is also available in the GNU shellutils package, which is available
26 * precompiled for OS/2. That one probably works better.
37 \ttee [-a] file ... file_n\n\
39 \t-a\tappend to files instead of truncating\n\
40 \nTee reads its input, and writes to each of the specified files,\n\
41 as well as to the standard output.\n\
43 This version supplied with Vim 4.2 to make ':make' possible.\n\
44 For a more complete and stable version, consider getting\n\
45 [a port of] the GNU shellutils package.\n\
50 * fread only returns when count is read or at EOF.
51 * We could use fgets, but I want to be able to handle binary blubber.
55 myfread(char *buf
, int elsize
/*ignored*/, int max
, FILE *fp
)
60 while ((n
< max
) && ((c
= getchar()) != EOF
))
64 if (c
== '\n' || c
== '\r')
72 main(int argc
, char *argv
[])
84 while ((opt
= getopt(argc
, argv
, "a")) != EOF
)
95 numfiles
= argc
- optind
;
99 fprintf(stderr
, "doesn't make much sense using tee without any file name arguments...\n");
104 maxfiles
= sysconf(_SC_OPEN_MAX
); /* or fill in 10 or so */
107 if (numfiles
+ 3 > maxfiles
) /* +3 accounts for stdin, out, err */
109 fprintf(stderr
, "Sorry, there is a limit of max %d files.\n", maxfiles
- 3);
112 filepointers
= calloc(numfiles
, sizeof(FILE *));
113 if (filepointers
== NULL
)
115 fprintf(stderr
, "Error allocating memory for %d files\n", numfiles
);
118 for (i
= 0; i
< numfiles
; i
++)
120 filepointers
[i
] = fopen(argv
[i
+optind
], append
? "ab" : "wb");
121 if (filepointers
[i
] == NULL
)
123 fprintf(stderr
, "Can't open \"%s\"\n", argv
[i
+optind
]);
127 _fsetmode(stdin
, "b");
128 fflush(stdout
); /* needed for _fsetmode(stdout) */
129 _fsetmode(stdout
, "b");
131 while ((n
= myfread(buf
, sizeof(char), sizeof(buf
), stdin
)) > 0)
133 fwrite(buf
, sizeof(char), n
, stdout
);
135 for (i
= 0; i
< numfiles
; i
++)
137 if (filepointers
[i
] &&
138 fwrite(buf
, sizeof(char), n
, filepointers
[i
]) != n
)
140 fprintf(stderr
, "Error writing to file \"%s\"\n", argv
[i
+optind
]);
141 fclose(filepointers
[i
]);
142 filepointers
[i
] = NULL
;
146 for (i
= 0; i
< numfiles
; i
++)
149 fclose(filepointers
[i
]);