aboutsummaryrefslogtreecommitdiff
path: root/7.8/fileprint.c
blob: 0ab0959ad2810716bda536ff80efb5e9d8c5d84e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
}