aboutsummaryrefslogtreecommitdiffstats
path: root/usr/tee.c
blob: dc086ce57d3456d043a7f8f4b8a8fd2648028d22 (plain) (blame)
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
#include "gbos.h"
/* tee FILE: copy stdin to stdout AND to FILE. The missing half of a pipeline:
 * `cat big | tee copy | wc -l` keeps the data and counts it in one pass. */
void main(void) {
    char *argv[4];
    unsigned char argc = argv_parse(argv, 4);
    unsigned char fd = NOFD;
    char c;
    if (argc > 0) {
        fd = open(argv[0], O_WRITE);
        if (fd > 3) {
            puts("tee: cannot create ");
            puts(argv[0]);
            nl();
            sexit(2);
        }
    }
    for (;;) {
        c = readc();
        if (c == EOF) break;
        putc(c);
        if (fd != NOFD) fputc(fd, c);
    }
    if (fd != NOFD) close(fd);
}