diff options
-rw-r--r-- | 7.8/Makefile | 13 | ||||
-rw-r--r-- | 7.8/fileprint.c | 46 | ||||
-rw-r--r-- | 7.8/fileprint.h | 3 | ||||
-rw-r--r-- | 7.8/main.c | 22 |
4 files changed, 84 insertions, 0 deletions
diff --git a/7.8/Makefile b/7.8/Makefile new file mode 100644 index 0000000..98452fc --- /dev/null +++ b/7.8/Makefile @@ -0,0 +1,13 @@ +OBJECTS = main.o fileprint.o +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +print : $(OBJECTS) + $(CC) $(CFLAGS) -o print $(OBJECTS) + +main.o : fileprint.h + +.PHONY : clean + +clean : + rm -f print $(OBJECTS) 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; +} diff --git a/7.8/fileprint.h b/7.8/fileprint.h new file mode 100644 index 0000000..ed4e7c5 --- /dev/null +++ b/7.8/fileprint.h @@ -0,0 +1,3 @@ +#include <stdio.h> + +void fileprint(FILE *fp, char *fname); diff --git a/7.8/main.c b/7.8/main.c new file mode 100644 index 0000000..02799a1 --- /dev/null +++ b/7.8/main.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> +#include "fileprint.h" + +int main(int argc, char *argv[]) +{ + FILE *fp; + + if (argc == 1) { + fileprint(stdin, ""); + } else { + while (--argc > 0 ) { + if (!(fp = fopen(*++argv, "r"))) { + fprintf(stderr, "print: can't open %s\n", *argv); + exit(1); + } else { + fileprint(fp, *argv); + fclose(fp); + } + } + } +} |