From 00044c933f8e30b14c0a68936beec96b72e3e2e2 Mon Sep 17 00:00:00 2001 From: Greg Reagle Date: Sat, 17 Sep 2016 11:54:11 -0400 Subject: [PATCH 2/2] slpg: conform to coding style and add manpage --- slpg.1 | 15 +++++++++++++++ slpg.c | 11 ++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 slpg.1 diff --git a/slpg.1 b/slpg.1 new file mode 100644 index 0000000..b986729 --- /dev/null +++ b/slpg.1 @@ -0,0 +1,15 @@ +.Dd 2016-09-17 +.Dt SLPG 1 +.Os sbase +.Sh NAME +.Nm slpg +.Nd simple pager +.Sh SYNOPSIS +.Nm +.Sh DESCRIPTION +.Nm +copies standard input to standard output one page at a time. +.Nm +tries to determine the size of the screen automatically: first it checks for the environment variables LINES and COLUMNS, then it queries the operating system. If neither of those work, it defaults to 80x24. + +Hit enter to go to next page and Control-c to stop. diff --git a/slpg.c b/slpg.c index 83fedb0..6021d43 100755 --- a/slpg.c +++ b/slpg.c @@ -7,9 +7,8 @@ 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; + int line_count, col_count, ch, env_num; + char *env_string; FILE *tty; struct winsize ws; @@ -21,7 +20,7 @@ main(int argc, char *argv[]) env_num = strtol(env_string, NULL, 10); if (env_num > 0) page_cols = env_num; } - if ((tty = fopen("/dev/tty", "r")) == NULL) { + if (! (tty = fopen("/dev/tty", "r"))) { perror("Error opening /dev/tty"); return errno; } @@ -30,9 +29,11 @@ main(int argc, char *argv[]) page_cols = ws.ws_col; } + line_count = col_count = 0; ch = getchar(); - for (line_count = 0, col_count = 1; ch != EOF; ++col_count) { + while (ch != EOF) { putchar(ch); + ++col_count; if (ch == '\n' || col_count >= page_cols) { ++line_count; col_count = 0; -- 2.1.4