mkfs: symlink support
[minix.git] / commands / pr / pr.c
blob2ad7c0574136960894672a6a2f26629d67d67ee3
1 /* pr - print files Author: Michiel Huisjes */
3 /* Pr - print files
5 * Author: Michiel Huisjes.
6 * Modified: Jacob P. Bunschoten. (30 nov 87)
7 * When "columns" is not given and numbering is on:
8 * line numbers are correlated with input lines.
9 * (try pr [-1] -n file )
10 * tabs are accounted for.
11 * When numbering is turned on, width know this.
12 * automatic line-folding. -f to get the original program.
13 * backspaces are accounted for. -b to disable this.
14 * multi-column mode changed.
15 * header can be given and used.
16 * format changed may occur between printing of several files:
17 * pr -l30 file1 -w75 file2
19 * Modified: Rick Thomas. (Sept 12, 1988)
20 * added "-M" option to cover functionality of old "-n" option,
21 * and made "-n" option behavior compatible with system V.
23 * Usage: pr [+page] [-columns] [-h header] [-wwidth] [-llength] [-ntm] [files]
24 * -t : Do not print the 5 line header and trailer at the page.
25 * -n : Turn on line numbering.
26 * -M : Use "Minix" style line numbering -- Each page begins at
27 * a line number that is an even multiple of the page length.
28 * Like the listings in Appendix E of the book.
29 * +page : Start printing at page n.
30 * -columns : Print files in n-columns.
31 * -l length: Take the length of the page to be n instead of 66
32 * -h header: Take next argument as page header.
33 * -w width : Take the width of the page to be n instead of default 79
34 * -f : do not fold lines.
36 * Modified: Lars Fredriksen (Jan 19, 1990)
37 * fixed the program so that
38 * pr -n *.c
39 * would work. The clobal variable 'width' was decremented
40 * by NUM_WIDTH, for each file, resulting in width finally
41 * being so small that nothing was printed. Used the local
42 * variable 'w' for the width adjustment (in print())
44 * Modified: Kenneth J. Hendrickson (10 April 1991)
45 * date in header should be last modification date for files,
46 * and the current time for stdin.
48 * Modified: Kees J. Bot (5 October 1992)
49 * Use localtime(3) to get the date, it knows TZ.
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <string.h>
55 #include <time.h>
56 #include <stdlib.h>
57 #include <stdio.h>
60 #define DEF_LENGTH 66
61 #define DEF_WIDTH 79
62 #define NUM_WIDTH 8
63 #define TAB_WIDTH 8 /* fixed tab_width */
65 /* Used to compute next (fixed) tabstop */
66 #define TO_TAB(x) (( (x) + TAB_WIDTH ) & ~07 )
68 typedef char BOOL;
70 #define FALSE 0
71 #define TRUE 1
73 /* EAT: eat rest of input line */
74 #define EAT(fp) while((c=getc(fp))!='\n' && c!=EOF)
76 /* L_BUF: calculate address of pointer to char (string) used in format */
77 #define L_BUF(i,j) * (char **) (line_buf + (i + j*length)*sizeof(char *))
79 char *header;
80 BOOL no_header;
81 BOOL number = FALSE;
82 BOOL minix_number = FALSE;
83 BOOL ext_header_set = FALSE; /* external header given */
84 BOOL back_space = TRUE; /* back space correction in line width */
85 BOOL dont_fold = FALSE; /* original. If the line does not fit eat it. */
86 short columns;
87 short cwidth;
88 short start_page = 1;
89 short width = DEF_WIDTH;
90 short length = DEF_LENGTH;
91 short linenr;
92 char *line_buf; /* used in format for multi-column output */
94 char output[1024];
96 int main(int argc, char **argv);
97 static char *myalloc(size_t size);
98 char skip_page(int lines, int width, FILE * filep);
99 void format(FILE * filep);
100 void print_page(int pagenr, int maxcol);
101 void print(FILE * filep);
102 void out_header(int page);
103 void print_time(time_t t);
105 int main(argc, argv)
106 int argc;
107 char *argv[];
109 FILE *file;
110 char *ptr;
111 int index = 1; /* index is one ahead of argc */
112 int line, col;
114 setbuf(stdout, output);
115 do {
116 if (argc == index) /* No arguments (left) */
117 goto pr_files;
119 ptr = argv[index++];
120 if (*ptr == '+') {
121 start_page = atoi(++ptr);
122 continue;
124 if (*ptr != '-') { /* no flags */
125 index--;
126 goto pr_files;
128 if (*++ptr >= '0' && *ptr <= '9') {
129 columns = atoi(ptr);
130 if (columns <= 0) columns = 1;
131 continue; /* Fetch next flag */
133 while (*ptr) switch (*ptr++) {
134 case 't': no_header = TRUE; break;
135 case 'n':
136 number = TRUE;
137 minix_number = FALSE;
138 break;
139 case 'M':
140 number = TRUE;
141 minix_number = TRUE;
142 break;
143 case 'h':
144 header = argv[index++];
145 ext_header_set = TRUE;
146 break;
147 case 'w':
148 if ((width = atoi(ptr)) <= 0) width = DEF_WIDTH;
149 *ptr = '\0';
150 break;
151 case 'l':
152 if ((length = atoi(ptr)) <= 0) length = DEF_LENGTH;
153 *ptr = '\0';
154 break;
155 case 'b': /* back_space correction off */
156 back_space = FALSE;
157 break;
158 case 'f': /* do not fold lines */
159 dont_fold = TRUE;
160 break;
161 default:
162 fprintf(stderr, "Usage: %s [+page] [-columns] [-h header] [-w<width>] [-l<length>] [-nMt] [files]\n", argv[0]);
163 exit(1);
165 continue; /* Scan for next flags */
168 /* ============== flags are read. Print the file(s) ========= */
170 pr_files:
172 if (!no_header) length -= 10;
174 if (columns) {
175 cwidth = width / columns + 1;
176 if (columns > width) {
177 fprintf(stderr, "Too many columns for page width.\n");
178 exit(1);
181 /* Allocate piece of mem to hold some pointers */
182 line_buf = myalloc(length * columns * sizeof(char *));
184 for (line = 0; line < length; line++)
185 for (col = 0; col < columns; col++)
186 L_BUF(line, col) = NULL;
188 if (length <= 0) {
189 fprintf(stderr, "Minimal length should be %d\n", no_header ?
190 1 : 11);
191 exit(1);
193 while (index <= argc) { /* print all files, including stdin */
194 if (index < argc && (*argv[index] == '-' || *argv[index] == '+'))
195 break; /* Format change */
197 if (argc == index) { /* no file specified, so stdin */
198 if (!ext_header_set) header = "";
199 file = stdin;
200 } else {
201 if ((file = fopen(argv[index], "r")) == (FILE *) 0) {
202 fprintf(stderr, "Cannot open %s\n", argv[index++]);
203 continue;
205 if (!ext_header_set) header = argv[index];
207 if (columns)
208 format(file);
209 else
210 print(file);
211 fclose(file);
212 if (++index >= argc)
213 break; /* all files (including stdin) done */
215 if (index >= argc) break;
216 /* When control comes here. format changes are to be done.
217 * reinitialize some variables */
218 if (!no_header) length += 10;
220 start_page = 1;
221 ext_header_set = FALSE;
222 if (columns) free(line_buf);
223 } while (index <= argc); /* "pr -l60" should work too */
225 (void) fflush(stdout);
226 return(0);
229 char skip_page(lines, width, filep)
230 int lines, width;
231 FILE *filep;
233 short c;
234 int char_cnt;
235 int w;
237 do {
238 w = width;
239 if (number) /* first lines are shorter */
240 if (!columns || /* called from print(file) */
241 !(lines % columns)) /* called from format(file) */
242 w -= NUM_WIDTH;
244 char_cnt = 0;
245 while ((c = getc(filep)) != '\n' && c != EOF && char_cnt < w) {
246 /* Calculate if this line is longer than "width (w)"
247 * characters */
248 if (c == '\b' && back_space) {
249 if (--char_cnt < 0) char_cnt = 0;
250 } else if (c == '\t')
251 char_cnt = TO_TAB(char_cnt);
252 else
253 char_cnt++;
255 if (dont_fold && c != '\n' && c != EOF) EAT(filep);
256 lines--;
257 if (c == '\n') linenr++;
258 } while (lines > 0 && c != EOF);
260 return c; /* last char read */
263 void format(filep)
264 FILE *filep;
266 char buf[512];
267 short c = '\0';
268 short index, lines, i;
269 short page_number = 0;
270 short maxcol = columns;
271 short wdth;
272 short line, col;
274 do {
275 /* Check printing of page */
276 page_number++;
278 if (page_number < start_page && c != EOF) {
279 c = (char) skip_page(columns * length, cwidth, filep);
280 continue;
282 if (c == EOF) return;
284 lines = columns * length;
285 for (line = 0; line < length; line++)
286 for (col = 0; col < columns; col++) {
287 if (L_BUF(line, col) != NULL)
288 free(L_BUF(line, col));
289 L_BUF(line, col) = (char *) NULL;
291 line = 0;
292 col = 0;
293 do {
294 index = 0;
295 wdth = cwidth - 1;
296 if (number && !col) /* need room for numbers */
297 wdth -= NUM_WIDTH;
299 /* Intermidiate colums are shortened by 1 char */
300 /* Last column not */
301 if (col + 1 == columns) wdth++;
302 for (i = 0; i < wdth - 1; i++) {
303 c = getc(filep);
304 if (c == '\n' || c == EOF) break;
306 if (c == '\b' && back_space) {
307 buf[index++] = '\b';
308 if (--i < 0) { /* just in case ... */
309 i = 0;
310 index = 0;
312 } else if (c == '\t') {
313 int cnt, max;
315 max = TO_TAB(i);
316 for (cnt = i; cnt < max; cnt++)
317 buf[index++] = ' ';
318 i = max - 1;
319 } else
320 buf[index++] = (char) c;
322 buf[index++] = '\0';
323 /* Collected enough chars (or eoln, or EOF) */
325 /* First char is EOF */
326 if (i == 0 && lines == columns * length && c == EOF) return;
328 /* Alloc mem to hold this (sub) string */
329 L_BUF(line, col) = myalloc(index * sizeof(char));
330 strcpy(L_BUF(line, col), buf);
332 line++;
333 line %= length;
334 if (line == 0) {
335 col++;
336 col %= columns;
338 if (dont_fold && c != '\n' && c != EOF) EAT(filep);
339 lines--; /* line ready for output */
340 if (c == EOF) {
341 maxcol = columns - lines / length;
343 } while (c != EOF && lines);
344 print_page(page_number, maxcol);
345 } while (c != EOF);
348 void print_page(pagenr, maxcol)
349 short pagenr, maxcol;
351 short pad, i, j;
352 short width;
353 char *p;
355 if (minix_number)
356 linenr = (pagenr - 1) * length + 1;
357 else
358 linenr = 1;
360 if (!no_header) out_header(pagenr);
362 for (i = 0; i < length; i++) {
363 for (j = 0; j < maxcol; j++) {
364 width = cwidth;
365 if (number && j == 0) { /* first columns */
366 printf("%7.7d ", linenr++); /* 7 == NUM_WIDTH-1 */
367 width -= NUM_WIDTH;
369 pad = 0;
370 if (p = (char *) L_BUF(i, j))
371 for (; pad < width - 1 && *p; pad++) putchar(*p++);
372 if (j < maxcol - 1) while (pad++ < width - 1)
373 putchar(' ');
375 putchar('\n');
377 if (!no_header) printf("\n\n\n\n\n");
380 void print(filep)
381 FILE *filep;
383 short c = '\0';
384 short page_number = 0;
385 short lines;
386 short cnt;
387 short w = width;
388 BOOL pr_number = TRUE; /* only real lines are numbered, not folded
389 * parts */
391 linenr = 1;
392 if (number) w -= NUM_WIDTH;
394 do {
395 /* Check printing of page */
396 page_number++;
398 if (page_number < start_page && c != EOF) {
399 pr_number = FALSE;
400 c = skip_page(length, w, filep);
401 if (c == '\n') pr_number = TRUE;
402 continue;
404 if (c == EOF) return;
406 if (minix_number) linenr = (page_number - 1) * length + 1;
408 if (page_number == start_page) c = getc(filep);
410 /* Print the page */
411 lines = length;
412 while (lines && c != EOF) {
413 if (lines == length && !no_header) out_header(page_number);
414 if (number)
415 if (pr_number)
416 printf("%7.7d ", linenr++); /* 7 == NUM_WIDTH-1 */
417 else
418 printf("%7c ", ' '); /* 7 == NUM_WIDTH-1 */
419 pr_number = FALSE;
420 cnt = 0;
421 while (c != '\n' && c != EOF && cnt < w) {
422 if (c == '\t') {
423 int i, max;
424 max = TO_TAB(cnt);
425 for (i = cnt; i < max; i++) putchar(' ');
426 cnt = max - 1;
427 } else if (c == '\b' && back_space) {
428 putchar('\b');
429 cnt--;
430 } else
431 putchar(c);
432 c = getc(filep);
433 cnt++;
435 putchar('\n');
436 if (dont_fold && c != '\n' && c != EOF) EAT(filep);
437 lines--;
438 if (c == '\n') {
439 c = getc(filep);
440 pr_number = TRUE;
443 if (lines == length) /* We never printed anything on this
444 * page -- */
445 return; /* even the header, so dont try to fill it up */
446 if (!no_header) /* print the trailer -- 5 blank lines */
447 printf("\n\n\n\n\n");
448 } while (c != EOF);
450 /* Fill last page */
451 if (page_number >= start_page) {
452 while (lines--) putchar('\n');
456 static char *myalloc(size)
457 size_t size; /* How many bytes */
459 void *ptr;
461 ptr = malloc(size);
462 if (ptr == NULL) {
463 fprintf(stderr, "malloc returned NULL\n");
464 exit(1);
466 return(char *) ptr;
469 void out_header(page)
470 short page;
472 time_t t;
473 struct stat buf;
475 if (strlen(header)) {
476 stat(header, &buf); /* use last modify time for file */
477 t = buf.st_mtime;
478 } else
479 (void) time(&t); /* use current time for stdin */
480 print_time(t);
481 printf(" %s Page %d\n\n\n", header, page);
484 char *moname[] = {
485 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
486 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
489 /* Print the date. */
490 void print_time(t)
491 time_t t;
493 struct tm *tm;
495 tm = localtime(&t);
497 printf("\n\n%s %2d %2d:%02d %d",
498 moname[tm->tm_mon],
499 tm->tm_mday,
500 tm->tm_hour,
501 tm->tm_min,
502 1900 + tm->tm_year