aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/core/cat.c
blob: 92b082d4b8c7e63f95b1972a20032fd72549ec7b (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <u.h>
#include <base.h>

static void
usage(void)
{
    fmt·panic("usage: %s [-u] [file ...]\n", argv0);
    exits("usage");
}

static void
cat(int fd, char *s)
{
    int err;
    intptr nr, nw;
    char buf[8192];

    while(!(err=sys·read(fd, buf, sizeof(buf), &nr)) && nr>0){
        if(sys·write(1, buf, nr, &nw) || nr != nw)
            fmt·panic("write error copying %s: %r", s);
    }

    if(err)
        fmt·panic("error reading %s: %r", s);
}

int
main(int argc, char *argv[])
{
    int fd;

    ARGBEGIN{
    case 'u': /* ignore */ break;
    default:
        usage();
    }ARGEND;

    if(!argc){
        cat(0, "<stdin>");
        exits(0);
    }

    while(argc-- > 0){
        if(sys·open(*argv, sys·ORead, 0, &fd))
            fmt·panic("can't open %s: %r", *argv);

        cat(fd, *argv);
        sys·close(fd);

        argv++;
    }

    return 0;
}