diff options
Diffstat (limited to '7.8/fileprint.c')
-rw-r--r-- | 7.8/fileprint.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/7.8/fileprint.c b/7.8/fileprint.c new file mode 100644 index 0000000..0ab0959 --- /dev/null +++ b/7.8/fileprint.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include "fileprint.h" + +#define MAXLINE 100 +#define MAXBOT 3 +#define MAXHDR 5 +#define MAXPAGE 66 + +static int heading(char *fname, int pageno); + +void fileprint(FILE *fp, char *fname) +{ + /* before making sense of this code read this first + * https://en.wikipedia.org/wiki/Page_break + * this code was helpful for printing text files + * this is not a clone of less, it just puts \f at the end + * of a page so the printer can print the rest on a new page */ + + char line[MAXLINE]; + int lineno, pageno = 1; + + lineno = heading(fname, pageno); + while (fgets(line, MAXLINE, fp)) { + if (lineno == 1) { + fprintf(stdout, "\f"); + lineno = heading(fname, ++pageno); + } + + if (lineno++ > MAXPAGE - MAXBOT) + lineno = 1; + fputs(line, stdout); + } + fprintf(stdout, "\f"); +} + +static int heading(char *fname, int pageno) +{ + int ln = 3; + + fprintf(stdout, "\n\n"); + fprintf(stdout, "%s page %d\n", fname, pageno); + + while (ln++ < MAXHDR) + fprintf(stdout, "\n"); + return ln; +} |