summaryrefslogtreecommitdiff
path: root/Userland/more.cpp
blob: 79c165b540263446981d6796185934924c540ef1 (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
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>

static int key_fd;

void wait_for_key()
{
    printf("\033[7m--[ more ]--\033[0m");
    fflush(stdout);
    char dummy;
    read(key_fd, &dummy, 1);
    printf("\n");
}

int main(int argc, char** argv)
{
    (void)argc;
    (void)argv;

    key_fd = open(ttyname(1), O_RDONLY);
    if (key_fd < 0) {
        perror("open");
        return 1;
    }

    struct winsize ws;
    ioctl(1, TIOCGWINSZ, &ws);

    unsigned lines_printed = 0;
    while (!feof(stdin)) {
        char buffer[BUFSIZ];
        auto* str = fgets(buffer, sizeof(buffer), stdin);
        if (!str)
            break;
        printf("%s", str);
        ++lines_printed;
        if ((lines_printed % (ws.ws_row - 1)) == 0) {
            wait_for_key();
        }
    }

    close(key_fd);
    return 0;
}