summaryrefslogtreecommitdiff
path: root/Userland/cat.cpp
blob: e03801487d367979564e5a6eb1fef0f9d0365f6a (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
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    int fd = argc > 1 ? open(argv[1], O_RDONLY) : 0;
    if (fd == -1) {
        fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
        return 1;
    }
    for (;;) {
        char buf[4096];
        ssize_t nread = read(fd, buf, sizeof(buf));
        if (nread == 0)
            break;
        if (nread < 0) {
            perror("read");
            return 2;
        }
        ssize_t nwritten = write(1, buf, nread);
        if (nwritten < 0) {
            perror("write");
            return 3;
        }
        ASSERT(nwritten == nread);
    }
    return 0;
}