aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/cat/cat.c
blob: 847ec0c2a33a33431c720be4db37b20379b599b2 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <u.h>
#include <libn.h>

static
void
cat(Stream *f, byte *name)
{
    long n;
    byte buf[8192];

    while(n = io·read(f, 1, arrlen(buf), buf), n > 0) {
        if (io·write(stdout, 1, n, buf) != n) {
            panicf("failed to write while copying stream '%s'", name);
        }
    }

    if (n < 0) {
        panicf("failed to read from buffer '%s'", name);
    }
}


int
main(int argc, char *argv[])
{
    int i;
    Stream *f;

    if (argc == 1)  {
        f = stdin;
        cat(f, "<stdin>");
    } else for (i = 1; i < argc; i++) {
        if (f = io·open(argv[i], "r"), f == nil) 
            panicf("can't open %s", argv[i]);
        cat(f, argv[i]);
        io·close(f);
    }
    
    exit(0);
}