aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/core/cat.c
blob: e9b770bd9c5b7e312658889fcb0379c3c5190e8e (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
#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)
{
    long n;
    char buf[8192];

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

    if(n<0)
        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((fd = open(*argv, O_RDONLY))<0)
            fmt·panic("can't open %s: %r", *argv);

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

        argv++;
    }
}