#include #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; }