diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2023-04-14 11:00:26 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2023-04-14 11:00:26 +0200 |
commit | 6638fe908eedd0d3fd7531a9af2ed10dd18ed5c1 (patch) | |
tree | 639e1ff84f31a1359f2f8e7c6030dc56d342023c /more.c | |
parent | 9d3b555393735e08c27b04a8b616f2736afe26de (diff) | |
download | abase-6638fe908eedd0d3fd7531a9af2ed10dd18ed5c1.tar.gz abase-6638fe908eedd0d3fd7531a9af2ed10dd18ed5c1.tar.bz2 |
implemented simplest possible version of "more"
Diffstat (limited to 'more.c')
-rw-r--r-- | more.c | 77 |
1 files changed, 75 insertions, 2 deletions
@@ -1,7 +1,15 @@ /* See LICENSE file for copyright and license details. */ +#include <fcntl.h> +#include <sys/stat.h> +#include <unistd.h> +#include <stdio.h> #include "util.h" +#define COLUMNS 80 +#define ROWS 25 +#define BUFSIZE 10 + static void usage(void) { @@ -11,12 +19,77 @@ usage(void) int main(int argc, char *argv[]) { - int ret = 0; + int fd; + char c; + int col; + int row; + int buf[BUFSIZE]; ARGBEGIN { default: usage(); } ARGEND + + if (argc < 1 && isatty(STDIN_FILENO)) + usage(); + + if (isatty(STDIN_FILENO)) { + if ((fd = open(*argv, O_RDONLY)) < 0) { + weprintf("open %s:", *argv); + return 0; + + } + } else { + fd = STDIN_FILENO; + } + + col = 0; + row = 0; + while (read(fd, &c, 1) != 0) { + switch (c) { + case '\r': + col = 0; + break; + + case '\n': + col = 0; + row++; + break; + + case '\t': + col = ((col+1) | 0x07)+1; + break; + + case '\b': + if (col>0) { + col--; + } + break; + + default: + col++; + break; + } + putchar(c); + + if (row<ROWS) { + continue; + } + + puts("--more--"); + + if (read(STDIN_FILENO, &buf, BUFSIZE) < 0) { + goto END; + } - return ret; + col = 0; + row = 0; + } + +END: + if (fd > STDERR_FILENO) { + close(fd); + } + + return 0; } |