From 211310cf6b76cc742025199578c44ed7d0d27a6a Mon Sep 17 00:00:00 2001 From: Greg Reagle Date: Fri, 16 Sep 2016 23:49:54 -0400 Subject: [PATCH] Added slpg, a suckless pager --- LICENSE | 2 +- Makefile | 1 + README | 1 + slpg.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100755 slpg.c diff --git a/LICENSE b/LICENSE index 6c0fc52..8ae1f73 100644 --- a/LICENSE +++ b/LICENSE @@ -54,7 +54,7 @@ Authors/contributors include: © 2014 Ari Malinen © 2014 Brandon Mulcahy © 2014 Adria Garriga -© 2014-2015 Greg Reagle +© 2014-2016 Greg Reagle © 2015 Tai Chi Minh Ralph Eastwood © 2015 Quentin Rameau © 2015 Dionysis Grigoropoulos diff --git a/Makefile b/Makefile index 6b2bfdf..0f34bf7 100644 --- a/Makefile +++ b/Makefile @@ -150,6 +150,7 @@ BIN =\ sha512-224sum\ sha512-256sum\ sleep\ + slpg\ sort\ split\ sponge\ diff --git a/README b/README index d60d8fc..e32ab96 100644 --- a/README +++ b/README @@ -79,6 +79,7 @@ The following tools are implemented: 0=* x sha512-224sum . 0=* x sha512-256sum . 0=*|o sleep . +0= x slpg . 0#*|o sort . 0=*|o split . 0=*|x sponge . diff --git a/slpg.c b/slpg.c new file mode 100755 index 0000000..83fedb0 --- /dev/null +++ b/slpg.c @@ -0,0 +1,48 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +int +main(int argc, char *argv[]) +{ + unsigned short page_lines = 23, page_cols = 80; /*default for VT100*/ + int line_count, col_count; + int ch, env_num; + char* env_string; + FILE *tty; + struct winsize ws; + + if (env_string = getenv("LINES")) { + env_num = strtol(env_string, NULL, 10); + if (env_num > 0) page_lines = env_num - 1; + } + if (env_string = getenv("COLUMNS")) { + env_num = strtol(env_string, NULL, 10); + if (env_num > 0) page_cols = env_num; + } + if ((tty = fopen("/dev/tty", "r")) == NULL) { + perror("Error opening /dev/tty"); + return errno; + } + if (!ioctl(fileno(tty), TIOCGWINSZ, &ws)) { + page_lines = ws.ws_row - 1; + page_cols = ws.ws_col; + } + + ch = getchar(); + for (line_count = 0, col_count = 1; ch != EOF; ++col_count) { + putchar(ch); + if (ch == '\n' || col_count >= page_cols) { + ++line_count; + col_count = 0; + if (line_count >= page_lines) { + while (fgetc(tty) != '\n') ; + line_count = 0; + } + } + ch = getchar(); + } + fclose(tty); + return(0); +} -- 2.1.4