1 /* pr - print files Author: Michiel Huisjes */
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
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>
63 #define TAB_WIDTH 8 /* fixed tab_width */
65 /* Used to compute next (fixed) tabstop */
66 #define TO_TAB(x) (( (x) + TAB_WIDTH ) & ~07 )
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 *))
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. */
89 short width
= DEF_WIDTH
;
90 short length
= DEF_LENGTH
;
92 char *line_buf
; /* used in format for multi-column output */
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
);
111 int index
= 1; /* index is one ahead of argc */
114 setbuf(stdout
, output
);
116 if (argc
== index
) /* No arguments (left) */
121 start_page
= atoi(++ptr
);
124 if (*ptr
!= '-') { /* no flags */
128 if (*++ptr
>= '0' && *ptr
<= '9') {
130 if (columns
<= 0) columns
= 1;
131 continue; /* Fetch next flag */
133 while (*ptr
) switch (*ptr
++) {
134 case 't': no_header
= TRUE
; break;
137 minix_number
= FALSE
;
144 header
= argv
[index
++];
145 ext_header_set
= TRUE
;
148 if ((width
= atoi(ptr
)) <= 0) width
= DEF_WIDTH
;
152 if ((length
= atoi(ptr
)) <= 0) length
= DEF_LENGTH
;
155 case 'b': /* back_space correction off */
158 case 'f': /* do not fold lines */
162 fprintf(stderr
, "Usage: %s [+page] [-columns] [-h header] [-w<width>] [-l<length>] [-nMt] [files]\n", argv
[0]);
165 continue; /* Scan for next flags */
168 /* ============== flags are read. Print the file(s) ========= */
172 if (!no_header
) length
-= 10;
175 cwidth
= width
/ columns
+ 1;
176 if (columns
> width
) {
177 fprintf(stderr
, "Too many columns for page width.\n");
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
;
189 fprintf(stderr
, "Minimal length should be %d\n", no_header
?
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
= "";
201 if ((file
= fopen(argv
[index
], "r")) == (FILE *) 0) {
202 fprintf(stderr
, "Cannot open %s\n", argv
[index
++]);
205 if (!ext_header_set
) header
= argv
[index
];
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;
221 ext_header_set
= FALSE
;
222 if (columns
) free(line_buf
);
223 } while (index
<= argc
); /* "pr -l60" should work too */
225 (void) fflush(stdout
);
229 char skip_page(lines
, width
, filep
)
239 if (number
) /* first lines are shorter */
240 if (!columns
|| /* called from print(file) */
241 !(lines
% columns
)) /* called from format(file) */
245 while ((c
= getc(filep
)) != '\n' && c
!= EOF
&& char_cnt
< w
) {
246 /* Calculate if this line is longer than "width (w)"
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
);
255 if (dont_fold
&& c
!= '\n' && c
!= EOF
) EAT(filep
);
257 if (c
== '\n') linenr
++;
258 } while (lines
> 0 && c
!= EOF
);
260 return c
; /* last char read */
268 short index
, lines
, i
;
269 short page_number
= 0;
270 short maxcol
= columns
;
275 /* Check printing of page */
278 if (page_number
< start_page
&& c
!= EOF
) {
279 c
= (char) skip_page(columns
* length
, cwidth
, filep
);
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
;
296 if (number
&& !col
) /* need room for numbers */
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
++) {
304 if (c
== '\n' || c
== EOF
) break;
306 if (c
== '\b' && back_space
) {
308 if (--i
< 0) { /* just in case ... */
312 } else if (c
== '\t') {
316 for (cnt
= i
; cnt
< max
; cnt
++)
320 buf
[index
++] = (char) c
;
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
);
338 if (dont_fold
&& c
!= '\n' && c
!= EOF
) EAT(filep
);
339 lines
--; /* line ready for output */
341 maxcol
= columns
- lines
/ length
;
343 } while (c
!= EOF
&& lines
);
344 print_page(page_number
, maxcol
);
348 void print_page(pagenr
, maxcol
)
349 short pagenr
, maxcol
;
356 linenr
= (pagenr
- 1) * length
+ 1;
360 if (!no_header
) out_header(pagenr
);
362 for (i
= 0; i
< length
; i
++) {
363 for (j
= 0; j
< maxcol
; j
++) {
365 if (number
&& j
== 0) { /* first columns */
366 printf("%7.7d ", linenr
++); /* 7 == NUM_WIDTH-1 */
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)
377 if (!no_header
) printf("\n\n\n\n\n");
384 short page_number
= 0;
388 BOOL pr_number
= TRUE
; /* only real lines are numbered, not folded
392 if (number
) w
-= NUM_WIDTH
;
395 /* Check printing of page */
398 if (page_number
< start_page
&& c
!= EOF
) {
400 c
= skip_page(length
, w
, filep
);
401 if (c
== '\n') pr_number
= TRUE
;
404 if (c
== EOF
) return;
406 if (minix_number
) linenr
= (page_number
- 1) * length
+ 1;
408 if (page_number
== start_page
) c
= getc(filep
);
412 while (lines
&& c
!= EOF
) {
413 if (lines
== length
&& !no_header
) out_header(page_number
);
416 printf("%7.7d ", linenr
++); /* 7 == NUM_WIDTH-1 */
418 printf("%7c ", ' '); /* 7 == NUM_WIDTH-1 */
421 while (c
!= '\n' && c
!= EOF
&& cnt
< w
) {
425 for (i
= cnt
; i
< max
; i
++) putchar(' ');
427 } else if (c
== '\b' && back_space
) {
436 if (dont_fold
&& c
!= '\n' && c
!= EOF
) EAT(filep
);
443 if (lines
== length
) /* We never printed anything on this
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");
451 if (page_number
>= start_page
) {
452 while (lines
--) putchar('\n');
456 static char *myalloc(size
)
457 size_t size
; /* How many bytes */
463 fprintf(stderr
, "malloc returned NULL\n");
469 void out_header(page
)
475 if (strlen(header
)) {
476 stat(header
, &buf
); /* use last modify time for file */
479 (void) time(&t
); /* use current time for stdin */
481 printf(" %s Page %d\n\n\n", header
, page
);
485 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
486 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
489 /* Print the date. */
497 printf("\n\n%s %2d %2d:%02d %d",