I made slpg.c conform more to suckless coding style, and added manpage.
From 211310cf6b76cc742025199578c44ed7d0d27a6a Mon Sep 17 00:00:00 2001
From: Greg Reagle <greg.rea...@umbc.edu>
Date: Fri, 16 Sep 2016 23:49:54 -0400
Subject: [PATCH 1/2] 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 <ari.mali...@gmail.com>
 © 2014 Brandon Mulcahy <bran...@jangler.info>
 © 2014 Adria Garriga <rhaps...@installgentoo.com>
-© 2014-2015 Greg Reagle <greg.rea...@umbc.edu>
+© 2014-2016 Greg Reagle <greg.rea...@umbc.edu>
 © 2015 Tai Chi Minh Ralph Eastwood <tcmreastw...@gmail.com>
 © 2015 Quentin Rameau <qu...@quinq.eu.org>
 © 2015 Dionysis Grigoropoulos <i...@erethon.com>
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 <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+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

From 00044c933f8e30b14c0a68936beec96b72e3e2e2 Mon Sep 17 00:00:00 2001
From: Greg Reagle <greg.rea...@umbc.edu>
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

Reply via email to