I have written a pager in C that is supposed to be used in systems where curses
is not installed (or broken). It can also be used as a system-wide pager, it works with man
pages (only on BSD, though). I want to spot any portability or output problems, as there may be some due to the dependence on buffered input:
#include <stdio.h>
#include <stdlib.h>
/*
* A very SIMPLE pager that does not require curses. If your curses library
* is broken, this is practically the only portable one you can easily get.
* Written by Daniel Roskams, in the public domain
*/
int
main(
int argc,
char *argv[])
{
FILE *fp;
int ch;
if (argc < 2) {
fp = stdin;
} else if ((fp = fopen(argv[1], "r")) == NULL) {
perror(argv[1]);
return EXIT_FAILURE;
}
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') {
getchar();
} else {
putchar(ch);
}
}
if (ferror(fp)) {
perror(argv[1]);
return EXIT_FAILURE;
}
fclose(fp);
return EXIT_SUCCESS;
}
I don't think there are any code problems themselves, since it compiles with the -Weverything
flag with clang
.