pr.c revision 1.22 1 /* $NetBSD: pr.c,v 1.22 2012/03/12 18:06:24 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1991 Keith Muller.
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1993\
39 The Regents of the University of California. All rights reserved.");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 from: static char sccsid[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93";
45 #else
46 __RCSID("$NetBSD: pr.c,v 1.22 2012/03/12 18:06:24 christos Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <sys/stat.h>
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <unistd.h>
62 #include <util.h>
63
64 #include "pr.h"
65 #include "extern.h"
66
67 /*
68 * pr: a printing and pagination filter. If multiple input files
69 * are specified, each is read, formatted, and written to standard
70 * output. By default, input is separated into 66-line pages, each
71 * with a header that includes the page number, date, time and the
72 * files pathname.
73 *
74 * Complies with posix P1003.2/D11
75 */
76
77 /*
78 * parameter variables
79 */
80 static int pgnm; /* starting page number */
81 static int clcnt; /* number of columns */
82 static int colwd; /* column data width - multiple columns */
83 static int across; /* mult col flag; write across page */
84 static int dspace; /* double space flag */
85 static char inchar; /* expand input char */
86 static int ingap; /* expand input gap */
87 static int formfeed; /* use formfeed as trailer */
88 static char *header; /* header name instead of file name */
89 static char ochar; /* contract output char */
90 static int ogap; /* contract output gap */
91 static int lines; /* number of lines per page */
92 static int merge; /* merge multiple files in output */
93 static char nmchar; /* line numbering append char */
94 static int nmwd; /* width of line number field */
95 static int offst; /* number of page offset spaces */
96 static int nodiag; /* do not report file open errors */
97 static char schar; /* text column separation character */
98 static int sflag; /* -s option for multiple columns */
99 static int nohead; /* do not write head and trailer */
100 static int pgwd; /* page width with multiple col output */
101 static const char *timefrmt = TIMEFMT; /* time conversion string */
102
103 /*
104 * misc globals
105 */
106 static FILE *errf; /* error message file pointer */
107 static int addone; /* page length is odd with double space */
108 static int errcnt; /* error count on file processing */
109 static const char digs[] = "0123456789"; /* page number translation map */
110
111 static void addnum(char *, int, int);
112 static void flsh_errs(void);
113 static int horzcol(int, char **);
114 static int inln(FILE *, char *, int, int *, int, int *);
115 static int inskip(FILE *, int, int);
116 static void mfail(void);
117 static int mulfile(int, char **);
118 static FILE *nxtfile(int, char **, const char **, char *, int);
119 static int onecol(int, char **);
120 static int otln(char *, int, int *, int *, int);
121 static void pfail(void);
122 static int prhead(char *, const char *, int);
123 static int prtail(int, int);
124 static int setup(int, char **);
125 __dead static void terminate(int);
126 static void usage(void);
127 static int vertcol(int, char **);
128
129 int
130 main(int argc, char *argv[])
131 {
132 int ret_val;
133
134 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
135 (void)signal(SIGINT, terminate);
136 ret_val = setup(argc, argv);
137 if (!ret_val) {
138 /*
139 * select the output format based on options
140 */
141 if (merge)
142 ret_val = mulfile(argc, argv);
143 else if (clcnt == 1)
144 ret_val = onecol(argc, argv);
145 else if (across)
146 ret_val = horzcol(argc, argv);
147 else
148 ret_val = vertcol(argc, argv);
149 } else
150 usage();
151 flsh_errs();
152 if (errcnt || ret_val)
153 exit(1);
154 return(0);
155 }
156
157 /*
158 * onecol: print files with only one column of output.
159 * Line length is unlimited.
160 */
161 static int
162 onecol(int argc, char *argv[])
163 {
164 int cnt = -1;
165 int off;
166 int lrgln;
167 int linecnt;
168 int num;
169 int lncnt;
170 int pagecnt;
171 int ips;
172 int ops;
173 int cps;
174 char *obuf = NULL;
175 char *lbuf;
176 char *nbuf;
177 char *hbuf = NULL;
178 char *ohbuf;
179 FILE *inf = NULL;
180 const char *fname;
181 int mor;
182 int error = 1;
183
184 if (nmwd)
185 num = nmwd + 1;
186 else
187 num = 0;
188 off = num + offst;
189
190 /*
191 * allocate line buffer
192 */
193 if ((obuf = malloc((unsigned)(LBUF + off)*sizeof(char))) == NULL)
194 goto oomem;
195 /*
196 * allocate header buffer
197 */
198 if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
199 goto oomem;
200
201 ohbuf = hbuf + offst;
202 nbuf = obuf + offst;
203 lbuf = nbuf + num;
204 if (num)
205 nbuf[--num] = nmchar;
206 if (offst) {
207 (void)memset(obuf, (int)' ', offst);
208 (void)memset(hbuf, (int)' ', offst);
209 }
210
211 /*
212 * loop by file
213 */
214 while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
215 if (pgnm) {
216 /*
217 * skip to specified page
218 */
219 if (inskip(inf, pgnm, lines))
220 continue;
221 pagecnt = pgnm;
222 } else
223 pagecnt = 1;
224 lncnt = 0;
225
226 /*
227 * loop by page
228 */
229 for(;;) {
230 linecnt = 0;
231 lrgln = 0;
232 ops = 0;
233 ips = 0;
234 cps = 0;
235
236 /*
237 * loop by line
238 */
239 while (linecnt < lines) {
240 /*
241 * input next line
242 */
243 if ((cnt = inln(inf,lbuf,LBUF,&cps,0,&mor)) < 0)
244 break;
245 if (!linecnt && !nohead &&
246 prhead(hbuf, fname, pagecnt))
247 goto out;
248
249 /*
250 * start of new line.
251 */
252 if (!lrgln) {
253 if (num)
254 addnum(nbuf, num, ++lncnt);
255 if (otln(obuf,cnt+off, &ips, &ops, mor))
256 goto out;
257 } else if (otln(lbuf, cnt, &ips, &ops, mor))
258 goto out;
259
260 /*
261 * if line bigger than buffer, get more
262 */
263 if (mor) {
264 lrgln = 1;
265 continue;
266 }
267
268 /*
269 * whole line rcvd. reset tab proc. state
270 */
271 ++linecnt;
272 lrgln = 0;
273 ops = 0;
274 ips = 0;
275 }
276
277 /*
278 * fill to end of page
279 */
280 if (linecnt && prtail(lines-linecnt-lrgln, lrgln))
281 goto out;
282
283 /*
284 * On EOF go to next file
285 */
286 if (cnt < 0)
287 break;
288 ++pagecnt;
289 }
290 if (inf != stdin)
291 (void)fclose(inf);
292 }
293 if (eoptind < argc)
294 goto out;
295 error = 0;
296 goto out;
297 oomem:
298 mfail();
299 out:
300 free(obuf);
301 free(hbuf);
302 if (inf != NULL && inf != stdin)
303 (void)fclose(inf);
304 return error;
305 }
306
307 /*
308 * vertcol: print files with more than one column of output down a page
309 */
310 static int
311 vertcol(int argc, char *argv[])
312 {
313 char *ptbf;
314 char **lstdat = NULL;
315 int i;
316 int j;
317 int cnt = -1;
318 int pln;
319 int *indy = NULL;
320 int cvc;
321 int *lindy = NULL;
322 int lncnt;
323 int stp;
324 int pagecnt;
325 int col = colwd + 1;
326 int mxlen = pgwd + offst + 1;
327 int mclcnt = clcnt - 1;
328 struct vcol *vc = NULL;
329 int mvc;
330 int tvc;
331 int cw = nmwd + 1;
332 int fullcol;
333 char *buf = NULL;
334 char *hbuf = NULL;
335 char *ohbuf;
336 const char *fname;
337 FILE *inf = NULL;
338 int ips = 0;
339 int cps = 0;
340 int ops = 0;
341 int mor = 0;
342 int error = 1;
343
344 /*
345 * allocate page buffer
346 */
347 if ((buf = malloc((unsigned)lines*mxlen*sizeof(char))) == NULL)
348 goto oomem;
349
350 /*
351 * allocate page header
352 */
353 if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
354 goto oomem;
355 ohbuf = hbuf + offst;
356 if (offst)
357 (void)memset(hbuf, (int)' ', offst);
358
359 /*
360 * col pointers when no headers
361 */
362 mvc = lines * clcnt;
363 if ((vc = malloc((unsigned)mvc*sizeof(struct vcol))) == NULL)
364 goto oomem;
365
366 /*
367 * pointer into page where last data per line is located
368 */
369 if ((lstdat = malloc((unsigned)lines*sizeof(char *))) == NULL)
370 goto oomem;
371
372 /*
373 * fast index lookups to locate start of lines
374 */
375 if ((indy = malloc((unsigned)lines*sizeof(int))) == NULL)
376 goto oomem;
377 if ((lindy = malloc((unsigned)lines*sizeof(int))) == NULL)
378 goto oomem;
379
380 if (nmwd)
381 fullcol = col + cw;
382 else
383 fullcol = col;
384
385 /*
386 * initialize buffer lookup indexes and offset area
387 */
388 for (j = 0; j < lines; ++j) {
389 lindy[j] = j * mxlen;
390 indy[j] = lindy[j] + offst;
391 if (offst) {
392 ptbf = buf + lindy[j];
393 (void)memset(ptbf, (int)' ', offst);
394 ptbf += offst;
395 } else
396 ptbf = buf + indy[j];
397 lstdat[j] = ptbf;
398 }
399
400 /*
401 * loop by file
402 */
403 while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
404 if (pgnm) {
405 /*
406 * skip to requested page
407 */
408 if (inskip(inf, pgnm, lines))
409 continue;
410 pagecnt = pgnm;
411 } else
412 pagecnt = 1;
413 lncnt = 0;
414
415 /*
416 * loop by page
417 */
418 for(;;) {
419 /*
420 * loop by column
421 */
422 cvc = 0;
423 for (i = 0; i < clcnt; ++i) {
424 j = 0;
425 /*
426 * if last column, do not pad
427 */
428 if (i == mclcnt)
429 stp = 1;
430 else
431 stp = 0;
432 /*
433 * loop by line
434 */
435 for(;;) {
436 /*
437 * is this first column
438 */
439 if (!i) {
440 ptbf = buf + indy[j];
441 lstdat[j] = ptbf;
442 } else
443 ptbf = lstdat[j];
444 vc[cvc].pt = ptbf;
445
446 /*
447 * add number
448 */
449 if (nmwd) {
450 addnum(ptbf, nmwd, ++lncnt);
451 ptbf += nmwd;
452 *ptbf++ = nmchar;
453 }
454
455 /*
456 * input next line
457 */
458 cnt = inln(inf,ptbf,colwd,&cps,1,&mor);
459 vc[cvc++].cnt = cnt;
460 if (cnt < 0)
461 break;
462 ptbf += cnt;
463
464 /*
465 * pad all but last column on page
466 */
467 if (!stp) {
468 /*
469 * pad to end of column
470 */
471 if (sflag)
472 *ptbf++ = schar;
473 else if ((pln = col-cnt) > 0) {
474 (void)memset(ptbf,
475 (int)' ',pln);
476 ptbf += pln;
477 }
478 }
479 /*
480 * remember last char in line
481 */
482 lstdat[j] = ptbf;
483 if (++j >= lines)
484 break;
485 }
486 if (cnt < 0)
487 break;
488 }
489
490 /*
491 * when -t (no header) is specified the spec requires
492 * the min number of lines. The last page may not have
493 * balanced length columns. To fix this we must reorder
494 * the columns. This is a very slow technique so it is
495 * only used under limited conditions. Without -t, the
496 * balancing of text columns is unspecified. To NOT
497 * balance the last page, add the global variable
498 * nohead to the if statement below e.g.
499 *
500 * if ((cnt < 0) && nohead && cvc ......
501 */
502 --cvc;
503
504 /*
505 * check to see if last page needs to be reordered
506 */
507 if ((cnt < 0) && cvc && ((mvc-cvc) >= clcnt)){
508 pln = cvc/clcnt;
509 if (cvc % clcnt)
510 ++pln;
511
512 /*
513 * print header
514 */
515 if (!nohead && prhead(hbuf, fname, pagecnt))
516 goto out;
517 for (i = 0; i < pln; ++i) {
518 ips = 0;
519 ops = 0;
520 if (offst&& otln(buf,offst,&ips,&ops,1)) {
521 error = 1;
522 goto out;
523 }
524 tvc = i;
525
526 for (j = 0; j < clcnt; ++j) {
527 /*
528 * determine column length
529 */
530 if (j == mclcnt) {
531 /*
532 * last column
533 */
534 cnt = vc[tvc].cnt;
535 if (nmwd)
536 cnt += cw;
537 } else if (sflag) {
538 /*
539 * single ch between
540 */
541 cnt = vc[tvc].cnt + 1;
542 if (nmwd)
543 cnt += cw;
544 } else
545 cnt = fullcol;
546 if (otln(vc[tvc].pt, cnt, &ips,
547 &ops, 1))
548 goto out;
549 tvc += pln;
550 if (tvc >= cvc)
551 break;
552 }
553 /*
554 * terminate line
555 */
556 if (otln(buf, 0, &ips, &ops, 0))
557 goto out;
558 }
559 /*
560 * pad to end of page
561 */
562 if (prtail((lines - pln), 0))
563 goto out;
564 /*
565 * done with output, go to next file
566 */
567 break;
568 }
569
570 /*
571 * determine how many lines to output
572 */
573 if (i > 0)
574 pln = lines;
575 else
576 pln = j;
577
578 /*
579 * print header
580 */
581 if (pln && !nohead && prhead(hbuf, fname, pagecnt))
582 goto out;
583
584 /*
585 * output each line
586 */
587 for (i = 0; i < pln; ++i) {
588 ptbf = buf + lindy[i];
589 if ((j = lstdat[i] - ptbf) <= offst)
590 break;
591 if (otln(ptbf, j, &ips, &ops, 0))
592 goto out;
593 }
594
595 /*
596 * pad to end of page
597 */
598 if (pln && prtail((lines - pln), 0))
599 goto out;
600
601 /*
602 * if EOF go to next file
603 */
604 if (cnt < 0)
605 break;
606 ++pagecnt;
607 }
608 if (inf != stdin)
609 (void)fclose(inf);
610 }
611 if (eoptind < argc)
612 goto out;
613 error = 0;
614 goto out;
615 oomem:
616 mfail();
617 out:
618 free(buf);
619 free(hbuf);
620 free(vc);
621 free(lstdat);
622 free(lindy);
623 if (inf != NULL && inf != stdin)
624 (void)fclose(inf);
625 return error;
626 }
627
628 /*
629 * horzcol: print files with more than one column of output across a page
630 */
631 static int
632 horzcol(int argc, char *argv[])
633 {
634 char *ptbf;
635 int pln;
636 int cnt = -1;
637 char *lstdat;
638 int col = colwd + 1;
639 int j;
640 int i;
641 int lncnt;
642 int pagecnt;
643 char *buf = NULL;
644 char *hbuf = NULL;
645 char *ohbuf;
646 const char *fname;
647 FILE *inf = NULL;
648 int ips = 0;
649 int cps = 0;
650 int ops = 0;
651 int mor = 0;
652 int error = 1;
653
654 if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL)
655 goto oomem;
656
657 /*
658 * page header
659 */
660 if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
661 goto oomem;
662 ohbuf = hbuf + offst;
663 if (offst) {
664 (void)memset(buf, (int)' ', offst);
665 (void)memset(hbuf, (int)' ', offst);
666 }
667
668 /*
669 * loop by file
670 */
671 while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
672 if (pgnm) {
673 if (inskip(inf, pgnm, lines))
674 continue;
675 pagecnt = pgnm;
676 } else
677 pagecnt = 1;
678 lncnt = 0;
679
680 /*
681 * loop by page
682 */
683 for(;;) {
684 /*
685 * loop by line
686 */
687 for (i = 0; i < lines; ++i) {
688 ptbf = buf + offst;
689 lstdat = ptbf;
690 j = 0;
691 /*
692 * loop by col
693 */
694 for(;;) {
695 if (nmwd) {
696 /*
697 * add number to column
698 */
699 addnum(ptbf, nmwd, ++lncnt);
700 ptbf += nmwd;
701 *ptbf++ = nmchar;
702 }
703 /*
704 * input line
705 */
706 if ((cnt = inln(inf,ptbf,colwd,&cps,1,
707 &mor)) < 0)
708 break;
709 ptbf += cnt;
710 lstdat = ptbf;
711
712 /*
713 * if last line skip padding
714 */
715 if (++j >= clcnt)
716 break;
717
718 /*
719 * pad to end of column
720 */
721 if (sflag)
722 *ptbf++ = schar;
723 else if ((pln = col - cnt) > 0) {
724 (void)memset(ptbf,(int)' ',pln);
725 ptbf += pln;
726 }
727 }
728
729 /*
730 * determine line length
731 */
732 if ((j = lstdat - buf) <= offst)
733 break;
734 if (!i && !nohead &&
735 prhead(hbuf, fname, pagecnt))
736 goto out;
737 /*
738 * output line
739 */
740 if (otln(buf, j, &ips, &ops, 0))
741 goto out;
742 }
743
744 /*
745 * pad to end of page
746 */
747 if (i && prtail(lines-i, 0))
748 goto out;
749
750 /*
751 * if EOF go to next file
752 */
753 if (cnt < 0)
754 break;
755 ++pagecnt;
756 }
757 if (inf != stdin)
758 (void)fclose(inf);
759 }
760 if (eoptind < argc)
761 goto out;
762 error = 0;
763 goto out;
764 oomem:
765 mfail();
766 out:
767 free(buf);
768 free(hbuf);
769 if (inf != NULL && inf != stdin)
770 (void)fclose(inf);
771 return error;
772 }
773
774 /*
775 * mulfile: print files with more than one column of output and
776 * more than one file concurrently
777 */
778 static int
779 mulfile(int argc, char *argv[])
780 {
781 char *ptbf;
782 int j;
783 int pln;
784 int cnt;
785 char *lstdat;
786 int i;
787 FILE **fbuf = NULL;
788 int actf;
789 int lncnt;
790 int col;
791 int pagecnt;
792 int fproc;
793 char *buf = NULL;
794 char *hbuf = NULL;
795 char *ohbuf;
796 const char *fname;
797 int ips = 0;
798 int cps = 0;
799 int ops = 0;
800 int mor = 0;
801 int error = 1;
802
803 /*
804 * array of FILE *, one for each operand
805 */
806 if ((fbuf = calloc(clcnt, sizeof(FILE *))) == NULL)
807 goto oomem;
808
809 /*
810 * page header
811 */
812 if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
813 goto oomem;
814 ohbuf = hbuf + offst;
815
816 /*
817 * do not know how many columns yet. The number of operands provide an
818 * upper bound on the number of columns. We use the number of files
819 * we can open successfully to set the number of columns. The operation
820 * of the merge operation (-m) in relation to unsuccesful file opens
821 * is unspecified by posix.
822 */
823 j = 0;
824 while (j < clcnt) {
825 if ((fbuf[j] = nxtfile(argc, argv, &fname, ohbuf, 1)) == NULL)
826 break;
827 if (pgnm && (inskip(fbuf[j], pgnm, lines)))
828 fbuf[j] = NULL;
829 ++j;
830 }
831
832 /*
833 * if no files, exit
834 */
835 if (!j)
836 goto out;
837
838 /*
839 * calculate page boundries based on open file count
840 */
841 clcnt = j;
842 if (nmwd) {
843 colwd = (pgwd - clcnt - nmwd)/clcnt;
844 pgwd = ((colwd + 1) * clcnt) - nmwd - 2;
845 } else {
846 colwd = (pgwd + 1 - clcnt)/clcnt;
847 pgwd = ((colwd + 1) * clcnt) - 1;
848 }
849 if (colwd < 1) {
850 (void)fprintf(errf,
851 "pr: page width too small for %d columns\n", clcnt);
852 goto out;
853 }
854 actf = clcnt;
855 col = colwd + 1;
856
857 /*
858 * line buffer
859 */
860 if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL)
861 goto out;
862 if (offst) {
863 (void)memset(buf, (int)' ', offst);
864 (void)memset(hbuf, (int)' ', offst);
865 }
866 if (pgnm)
867 pagecnt = pgnm;
868 else
869 pagecnt = 1;
870 lncnt = 0;
871
872 /*
873 * continue to loop while any file still has data
874 */
875 while (actf > 0) {
876 /*
877 * loop by line
878 */
879 for (i = 0; i < lines; ++i) {
880 ptbf = buf + offst;
881 lstdat = ptbf;
882 if (nmwd) {
883 /*
884 * add line number to line
885 */
886 addnum(ptbf, nmwd, ++lncnt);
887 ptbf += nmwd;
888 *ptbf++ = nmchar;
889 }
890 j = 0;
891 fproc = 0;
892
893 /*
894 * loop by column
895 */
896 for (j = 0; j < clcnt; ++j) {
897 if (fbuf[j] == NULL) {
898 /*
899 * empty column; EOF
900 */
901 cnt = 0;
902 } else if ((cnt = inln(fbuf[j], ptbf, colwd,
903 &cps, 1, &mor)) < 0) {
904 /*
905 * EOF hit; no data
906 */
907 if (fbuf[j] != stdin)
908 (void)fclose(fbuf[j]);
909 fbuf[j] = NULL;
910 --actf;
911 cnt = 0;
912 } else {
913 /*
914 * process file data
915 */
916 ptbf += cnt;
917 lstdat = ptbf;
918 fproc++;
919 }
920
921 /*
922 * if last ACTIVE column, done with line
923 */
924 if (fproc >= actf)
925 break;
926
927 /*
928 * pad to end of column
929 */
930 if (sflag) {
931 *ptbf++ = schar;
932 } else if ((pln = col - cnt) > 0) {
933 (void)memset(ptbf, (int)' ', pln);
934 ptbf += pln;
935 }
936 }
937
938 /*
939 * calculate data in line
940 */
941 if ((j = lstdat - buf) <= offst)
942 break;
943
944 if (!i && !nohead && prhead(hbuf, fname, pagecnt))
945 goto out;
946
947 /*
948 * output line
949 */
950 if (otln(buf, j, &ips, &ops, 0))
951 goto out;
952
953 /*
954 * if no more active files, done
955 */
956 if (actf <= 0) {
957 ++i;
958 break;
959 }
960 }
961
962 /*
963 * pad to end of page
964 */
965 if (i && prtail(lines-i, 0))
966 goto out;
967 ++pagecnt;
968 }
969 if (eoptind < argc)
970 goto out;
971 error = 0;
972 goto out;
973 oomem:
974 mfail();
975 out:
976 if (fbuf) {
977 for (j = 0; j < clcnt; j++)
978 if (fbuf[j] && fbuf[j] != stdin)
979 (void)fclose(fbuf[j]);
980 free(fbuf);
981 }
982 free(hbuf);
983 free(buf);
984 return error;
985 }
986
987 /*
988 * inln(): input a line of data (unlimited length lines supported)
989 * Input is optionally expanded to spaces
990 *
991 * inf: file
992 * buf: buffer
993 * lim: buffer length
994 * cps: column positon 1st char in buffer (large line support)
995 * trnc: throw away data more than lim up to \n
996 * mor: set if more data in line (not truncated)
997 */
998 static int
999 inln(FILE *inf, char *buf, int lim, int *cps, int trnc, int *mor)
1000 {
1001 int col;
1002 int gap = ingap;
1003 int ch = EOF;
1004 char *ptbuf;
1005 int chk = (int)inchar;
1006
1007 ptbuf = buf;
1008
1009 if (gap) {
1010 /*
1011 * expanding input option
1012 */
1013 while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
1014 /*
1015 * is this the input "tab" char
1016 */
1017 if (ch == chk) {
1018 /*
1019 * expand to number of spaces
1020 */
1021 col = (ptbuf - buf) + *cps;
1022 col = gap - (col % gap);
1023
1024 /*
1025 * if more than this line, push back
1026 */
1027 if ((col > lim) && (ungetc(ch, inf) == EOF))
1028 return(1);
1029
1030 /*
1031 * expand to spaces
1032 */
1033 while ((--col >= 0) && (--lim >= 0))
1034 *ptbuf++ = ' ';
1035 continue;
1036 }
1037 if (ch == '\n')
1038 break;
1039 *ptbuf++ = ch;
1040 }
1041 } else {
1042 /*
1043 * no expansion
1044 */
1045 while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
1046 if (ch == '\n')
1047 break;
1048 *ptbuf++ = ch;
1049 }
1050 }
1051 col = ptbuf - buf;
1052 if (ch == EOF) {
1053 *mor = 0;
1054 *cps = 0;
1055 if (!col)
1056 return(-1);
1057 return(col);
1058 }
1059 if (ch == '\n') {
1060 /*
1061 * entire line processed
1062 */
1063 *mor = 0;
1064 *cps = 0;
1065 return(col);
1066 }
1067
1068 /*
1069 * line was larger than limit
1070 */
1071 if (trnc) {
1072 /*
1073 * throw away rest of line
1074 */
1075 while ((ch = getc(inf)) != EOF) {
1076 if (ch == '\n')
1077 break;
1078 }
1079 *cps = 0;
1080 *mor = 0;
1081 } else {
1082 /*
1083 * save column offset if not truncated
1084 */
1085 *cps += col;
1086 *mor = 1;
1087 }
1088
1089 return(col);
1090 }
1091
1092 /*
1093 * otln(): output a line of data. (Supports unlimited length lines)
1094 * output is optionally contracted to tabs
1095 *
1096 * buf: output buffer with data
1097 * cnt: number of chars of valid data in buf
1098 * svips: buffer input column position (for large lines)
1099 * svops: buffer output column position (for large lines)
1100 * mor: output line not complete in this buf; more data to come.
1101 * 1 is more, 0 is complete, -1 is no \n's
1102 */
1103 static int
1104 otln(char *buf, int cnt, int *svips, int *svops, int mor)
1105 {
1106 int ops; /* last col output */
1107 int ips; /* last col in buf examined */
1108 int gap = ogap;
1109 int tbps;
1110 char *endbuf;
1111
1112 if (ogap) {
1113 /*
1114 * contracting on output
1115 */
1116 endbuf = buf + cnt;
1117 ops = *svops;
1118 ips = *svips;
1119 while (buf < endbuf) {
1120 /*
1121 * count number of spaces and ochar in buffer
1122 */
1123 if (*buf == ' ') {
1124 ++ips;
1125 ++buf;
1126 continue;
1127 }
1128
1129 /*
1130 * simulate ochar processing
1131 */
1132 if (*buf == ochar) {
1133 ips += gap - (ips % gap);
1134 ++buf;
1135 continue;
1136 }
1137
1138 /*
1139 * got a non space char; contract out spaces
1140 */
1141 while (ips - ops > 1) {
1142 /*
1143 * use as many ochar as will fit
1144 */
1145 if ((tbps = ops + gap - (ops % gap)) > ips)
1146 break;
1147 if (putchar(ochar) == EOF) {
1148 pfail();
1149 return(1);
1150 }
1151 ops = tbps;
1152 }
1153
1154 while (ops < ips) {
1155 /*
1156 * finish off with spaces
1157 */
1158 if (putchar(' ') == EOF) {
1159 pfail();
1160 return(1);
1161 }
1162 ++ops;
1163 }
1164
1165 /*
1166 * output non space char
1167 */
1168 if (putchar(*buf++) == EOF) {
1169 pfail();
1170 return(1);
1171 }
1172 ++ips;
1173 ++ops;
1174 }
1175
1176 if (mor > 0) {
1177 /*
1178 * if incomplete line, save position counts
1179 */
1180 *svops = ops;
1181 *svips = ips;
1182 return(0);
1183 }
1184
1185 if (mor < 0) {
1186 while (ips - ops > 1) {
1187 /*
1188 * use as many ochar as will fit
1189 */
1190 if ((tbps = ops + gap - (ops % gap)) > ips)
1191 break;
1192 if (putchar(ochar) == EOF) {
1193 pfail();
1194 return(1);
1195 }
1196 ops = tbps;
1197 }
1198 while (ops < ips) {
1199 /*
1200 * finish off with spaces
1201 */
1202 if (putchar(' ') == EOF) {
1203 pfail();
1204 return(1);
1205 }
1206 ++ops;
1207 }
1208 return(0);
1209 }
1210 } else {
1211 /*
1212 * output is not contracted
1213 */
1214 if (cnt && (fwrite(buf, sizeof(char), cnt, stdout) <= 0)) {
1215 pfail();
1216 return(1);
1217 }
1218 if (mor != 0)
1219 return(0);
1220 }
1221
1222 /*
1223 * process line end and double space as required
1224 */
1225 if ((putchar('\n') == EOF) || (dspace && (putchar('\n') == EOF))) {
1226 pfail();
1227 return(1);
1228 }
1229 return(0);
1230 }
1231
1232 /*
1233 * inskip(): skip over pgcnt pages with lncnt lines per page
1234 * file is closed at EOF (if not stdin).
1235 *
1236 * inf FILE * to read from
1237 * pgcnt number of pages to skip
1238 * lncnt number of lines per page
1239 */
1240 static int
1241 inskip(FILE *inf, int pgcnt, int lncnt)
1242 {
1243 int c;
1244 int cnt;
1245
1246 while(--pgcnt > 0) {
1247 cnt = lncnt;
1248 while ((c = getc(inf)) != EOF) {
1249 if ((c == '\n') && (--cnt == 0))
1250 break;
1251 }
1252 if (c == EOF) {
1253 if (inf != stdin)
1254 (void)fclose(inf);
1255 return(1);
1256 }
1257 }
1258 return(0);
1259 }
1260
1261 /*
1262 * nxtfile: returns a FILE * to next file in arg list and sets the
1263 * time field for this file (or current date).
1264 *
1265 * buf array to store proper date for the header.
1266 * dt if set skips the date processing (used with -m)
1267 */
1268 static FILE *
1269 nxtfile(int argc, char **argv, const char **fname, char *buf, int dt)
1270 {
1271 FILE *inf = NULL;
1272 struct timeval tv;
1273 struct timezone tz;
1274 struct tm *timeptr = NULL;
1275 struct stat statbuf;
1276 time_t curtime;
1277 static int twice = -1;
1278
1279 ++twice;
1280 if (eoptind >= argc) {
1281 /*
1282 * no file listed; default, use standard input
1283 */
1284 if (twice)
1285 return(NULL);
1286 clearerr(stdin);
1287 inf = stdin;
1288 if (header != NULL)
1289 *fname = header;
1290 else
1291 *fname = FNAME;
1292 if (nohead)
1293 return(inf);
1294 if (gettimeofday(&tv, &tz) < 0) {
1295 ++errcnt;
1296 (void)fprintf(errf, "pr: cannot get time of day, %s\n",
1297 strerror(errno));
1298 eoptind = argc - 1;
1299 return(NULL);
1300 }
1301 curtime = tv.tv_sec;
1302 timeptr = localtime(&curtime);
1303 }
1304 for (; eoptind < argc; ++eoptind) {
1305 if (strcmp(argv[eoptind], "-") == 0) {
1306 /*
1307 * process a "-" for filename
1308 */
1309 clearerr(stdin);
1310 inf = stdin;
1311 if (header != NULL)
1312 *fname = header;
1313 else
1314 *fname = FNAME;
1315 ++eoptind;
1316 if (nohead || (dt && twice))
1317 return(inf);
1318 if (gettimeofday(&tv, &tz) < 0) {
1319 ++errcnt;
1320 (void)fprintf(errf,
1321 "pr: cannot get time of day, %s\n",
1322 strerror(errno));
1323 return(NULL);
1324 }
1325 curtime = tv.tv_sec;
1326 timeptr = localtime(&curtime);
1327 } else {
1328 /*
1329 * normal file processing
1330 */
1331 if ((inf = fopen(argv[eoptind], "r")) == NULL) {
1332 ++errcnt;
1333 if (nodiag)
1334 continue;
1335 (void)fprintf(errf, "pr: Cannot open %s, %s\n",
1336 argv[eoptind], strerror(errno));
1337 continue;
1338 }
1339 if (header != NULL)
1340 *fname = header;
1341 else if (dt)
1342 *fname = FNAME;
1343 else
1344 *fname = argv[eoptind];
1345 ++eoptind;
1346 if (nohead || (dt && twice))
1347 return(inf);
1348
1349 if (dt) {
1350 if (gettimeofday(&tv, &tz) < 0) {
1351 ++errcnt;
1352 (void)fprintf(errf,
1353 "pr: cannot get time of day, %s\n",
1354 strerror(errno));
1355 return(NULL);
1356 }
1357 curtime = tv.tv_sec;
1358 timeptr = localtime(&curtime);
1359 } else {
1360 if (fstat(fileno(inf), &statbuf) < 0) {
1361 ++errcnt;
1362 (void)fclose(inf);
1363 (void)fprintf(errf,
1364 "pr: Cannot stat %s, %s\n",
1365 argv[eoptind], strerror(errno));
1366 return(NULL);
1367 }
1368 timeptr = localtime(&(statbuf.st_mtime));
1369 }
1370 }
1371 break;
1372 }
1373 if (inf == NULL)
1374 return(NULL);
1375
1376 /*
1377 * set up time field used in header
1378 */
1379 if (strftime(buf, HDBUF, timefrmt, timeptr) <= 0) {
1380 ++errcnt;
1381 if (inf != stdin)
1382 (void)fclose(inf);
1383 (void)fputs("pr: time conversion failed\n", errf);
1384 return(NULL);
1385 }
1386 return(inf);
1387 }
1388
1389 /*
1390 * addnum(): adds the line number to the column
1391 * Truncates from the front or pads with spaces as required.
1392 * Numbers are right justified.
1393 *
1394 * buf buffer to store the number
1395 * wdth width of buffer to fill
1396 * line line number
1397 *
1398 * NOTE: numbers occupy part of the column. The posix
1399 * spec does not specify if -i processing should or should not
1400 * occur on number padding. The spec does say it occupies
1401 * part of the column. The usage of addnum currently treats
1402 * numbers as part of the column so spaces may be replaced.
1403 */
1404 void
1405 addnum(char *buf, int wdth, int line)
1406 {
1407 char *pt = buf + wdth;
1408
1409 do {
1410 *--pt = digs[line % 10];
1411 line /= 10;
1412 } while (line && (pt > buf));
1413
1414 /*
1415 * pad with space as required
1416 */
1417 while (pt > buf)
1418 *--pt = ' ';
1419 }
1420
1421 /*
1422 * prhead(): prints the top of page header
1423 *
1424 * buf buffer with time field (and offset)
1425 * cnt number of chars in buf
1426 * fname fname field for header
1427 * pagcnt page number
1428 */
1429 static int
1430 prhead(char *buf, const char *fname, int pagcnt)
1431 {
1432 int ips = 0;
1433 int ops = 0;
1434
1435 if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) {
1436 pfail();
1437 return(1);
1438 }
1439 /*
1440 * posix is not clear if the header is subject to line length
1441 * restrictions. The specification for header line format
1442 * in the spec clearly does not limit length. No pr currently
1443 * restricts header length. However if we need to truncate in
1444 * an reasonable way, adjust the length of the printf by
1445 * changing HDFMT to allow a length max as an argument printf.
1446 * buf (which contains the offset spaces and time field could
1447 * also be trimmed
1448 *
1449 * note only the offset (if any) is processed for tab expansion
1450 */
1451 if (offst && otln(buf, offst, &ips, &ops, -1))
1452 return(1);
1453 (void)printf(HDFMT,buf+offst, fname, pagcnt);
1454 return(0);
1455 }
1456
1457 /*
1458 * prtail(): pad page with empty lines (if required) and print page trailer
1459 * if requested
1460 *
1461 * cnt number of lines of padding needed
1462 * incomp was a '\n' missing from last line output
1463 */
1464 static int
1465 prtail(int cnt, int incomp)
1466 {
1467 if (nohead) {
1468 /*
1469 * only pad with no headers when incomplete last line
1470 */
1471 if (!incomp)
1472 return(0);
1473 if ((dspace && (putchar('\n') == EOF)) ||
1474 (putchar('\n') == EOF)) {
1475 pfail();
1476 return(1);
1477 }
1478 return(0);
1479 }
1480
1481 /*
1482 * if double space output two \n
1483 */
1484 if (dspace)
1485 cnt *= 2;
1486
1487 /*
1488 * if an odd number of lines per page, add an extra \n
1489 */
1490 if (addone)
1491 ++cnt;
1492
1493 /*
1494 * pad page
1495 */
1496 if (formfeed) {
1497 if ((incomp && (putchar('\n') == EOF)) ||
1498 (putchar('\f') == EOF)) {
1499 pfail();
1500 return(1);
1501 }
1502 return(0);
1503 }
1504 cnt += TAILLEN;
1505 while (--cnt >= 0) {
1506 if (putchar('\n') == EOF) {
1507 pfail();
1508 return(1);
1509 }
1510 }
1511 return(0);
1512 }
1513
1514 /*
1515 * terminate(): when a SIGINT is recvd
1516 */
1517 static void
1518 terminate(int which_sig)
1519 {
1520 flsh_errs();
1521 (void)raise_default_signal(which_sig);
1522 exit(1);
1523 }
1524
1525
1526 /*
1527 * flsh_errs(): output saved up diagnostic messages after all normal
1528 * processing has completed
1529 */
1530 static void
1531 flsh_errs(void)
1532 {
1533 char buf[BUFSIZ];
1534
1535 (void)fflush(stdout);
1536 (void)fflush(errf);
1537 if (errf == stderr)
1538 return;
1539 rewind(errf);
1540 while (fgets(buf, BUFSIZ, errf) != NULL)
1541 (void)fputs(buf, stderr);
1542 }
1543
1544 static void
1545 mfail(void)
1546 {
1547 (void)fputs("pr: memory allocation failed\n", errf);
1548 }
1549
1550 static void
1551 pfail(void)
1552 {
1553 (void)fprintf(errf, "pr: write failure, %s\n", strerror(errno));
1554 }
1555
1556 static void
1557 usage(void)
1558 {
1559 (void)fputs(
1560 "usage: pr [+page] [-col] [-adFmrt] [-e[ch][gap]] [-h header]\n",errf);
1561 (void)fputs(
1562 " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",errf);
1563 (void)fputs(
1564 " [-s[ch]] [-w width] [-] [file ...]\n", errf);
1565 }
1566
1567 /*
1568 * setup: Validate command args, initialize and perform sanity
1569 * checks on options
1570 */
1571 static int
1572 setup(int argc, char **argv)
1573 {
1574 int c;
1575 int eflag = 0;
1576 int iflag = 0;
1577 int wflag = 0;
1578 int cflag = 0;
1579
1580 if (isatty(fileno(stdout))) {
1581 /*
1582 * defer diagnostics until processing is done
1583 */
1584 if ((errf = tmpfile()) == NULL) {
1585 (void)fputs("Cannot defer diagnostic messages\n",stderr);
1586 return(1);
1587 }
1588 } else
1589 errf = stderr;
1590 while ((c = egetopt(argc, argv, "#adFmrte?h:i?l:n?o:s?T:w:")) != -1) {
1591 switch (c) {
1592 case '+':
1593 if ((pgnm = atoi(eoptarg)) < 1) {
1594 (void)fputs("pr: +page number must be 1 or more\n",
1595 errf);
1596 return(1);
1597 }
1598 break;
1599 case '-':
1600 if ((clcnt = atoi(eoptarg)) < 1) {
1601 (void)fputs("pr: -columns must be 1 or more\n",errf);
1602 return(1);
1603 }
1604 if (clcnt > 1)
1605 ++cflag;
1606 break;
1607 case 'a':
1608 ++across;
1609 break;
1610 case 'd':
1611 ++dspace;
1612 break;
1613 case 'e':
1614 ++eflag;
1615 if ((eoptarg != NULL) &&
1616 !isdigit((unsigned char)*eoptarg))
1617 inchar = *eoptarg++;
1618 else
1619 inchar = INCHAR;
1620 if ((eoptarg != NULL) &&
1621 isdigit((unsigned char)*eoptarg)) {
1622 if ((ingap = atoi(eoptarg)) < 0) {
1623 (void)fputs(
1624 "pr: -e gap must be 0 or more\n", errf);
1625 return(1);
1626 }
1627 if (ingap == 0)
1628 ingap = INGAP;
1629 } else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
1630 (void)fprintf(errf,
1631 "pr: invalid value for -e %s\n", eoptarg);
1632 return(1);
1633 } else
1634 ingap = INGAP;
1635 break;
1636 case 'F':
1637 ++formfeed;
1638 break;
1639 case 'h':
1640 header = eoptarg;
1641 break;
1642 case 'i':
1643 ++iflag;
1644 if ((eoptarg != NULL) &&
1645 !isdigit((unsigned char)*eoptarg))
1646 ochar = *eoptarg++;
1647 else
1648 ochar = OCHAR;
1649 if ((eoptarg != NULL) &&
1650 isdigit((unsigned char)*eoptarg)) {
1651 if ((ogap = atoi(eoptarg)) < 0) {
1652 (void)fputs(
1653 "pr: -i gap must be 0 or more\n", errf);
1654 return(1);
1655 }
1656 if (ogap == 0)
1657 ogap = OGAP;
1658 } else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
1659 (void)fprintf(errf,
1660 "pr: invalid value for -i %s\n", eoptarg);
1661 return(1);
1662 } else
1663 ogap = OGAP;
1664 break;
1665 case 'l':
1666 if (!isdigit((unsigned char)*eoptarg) ||
1667 ((lines=atoi(eoptarg)) < 1)) {
1668 (void)fputs(
1669 "pr: Number of lines must be 1 or more\n",errf);
1670 return(1);
1671 }
1672 break;
1673 case 'm':
1674 ++merge;
1675 break;
1676 case 'n':
1677 if ((eoptarg != NULL) &&
1678 !isdigit((unsigned char)*eoptarg))
1679 nmchar = *eoptarg++;
1680 else
1681 nmchar = NMCHAR;
1682 if ((eoptarg != NULL) &&
1683 isdigit((unsigned char)*eoptarg)) {
1684 if ((nmwd = atoi(eoptarg)) < 1) {
1685 (void)fputs(
1686 "pr: -n width must be 1 or more\n",errf);
1687 return(1);
1688 }
1689 } else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
1690 (void)fprintf(errf,
1691 "pr: invalid value for -n %s\n", eoptarg);
1692 return(1);
1693 } else
1694 nmwd = NMWD;
1695 break;
1696 case 'o':
1697 if (!isdigit((unsigned char)*eoptarg) ||
1698 ((offst = atoi(eoptarg))< 1)){
1699 (void)fputs("pr: -o offset must be 1 or more\n",
1700 errf);
1701 return(1);
1702 }
1703 break;
1704 case 'r':
1705 ++nodiag;
1706 break;
1707 case 's':
1708 ++sflag;
1709 if (eoptarg == NULL)
1710 schar = SCHAR;
1711 else
1712 schar = *eoptarg++;
1713 if (eoptarg && *eoptarg != '\0') {
1714 (void)fprintf(errf,
1715 "pr: invalid value for -s %s\n", eoptarg);
1716 return(1);
1717 }
1718 break;
1719 case 'T':
1720 timefrmt = eoptarg;
1721 break;
1722 case 't':
1723 ++nohead;
1724 break;
1725 case 'w':
1726 ++wflag;
1727 if (!isdigit((unsigned char)*eoptarg) ||
1728 ((pgwd = atoi(eoptarg)) < 1)){
1729 (void)fputs(
1730 "pr: -w width must be 1 or more \n",errf);
1731 return(1);
1732 }
1733 break;
1734 case '?':
1735 default:
1736 return(1);
1737 }
1738 }
1739
1740 /*
1741 * default and sanity checks
1742 */
1743 if (!clcnt) {
1744 if (merge) {
1745 if ((clcnt = argc - eoptind) <= 1) {
1746 clcnt = CLCNT;
1747 merge = 0;
1748 }
1749 } else
1750 clcnt = CLCNT;
1751 }
1752 if (across) {
1753 if (clcnt == 1) {
1754 (void)fputs("pr: -a flag requires multiple columns\n",
1755 errf);
1756 return(1);
1757 }
1758 if (merge) {
1759 (void)fputs("pr: -m cannot be used with -a\n", errf);
1760 return(1);
1761 }
1762 }
1763 if (!wflag) {
1764 if (sflag)
1765 pgwd = SPGWD;
1766 else
1767 pgwd = PGWD;
1768 }
1769 if (cflag || merge) {
1770 if (!eflag) {
1771 inchar = INCHAR;
1772 ingap = INGAP;
1773 }
1774 if (!iflag) {
1775 ochar = OCHAR;
1776 ogap = OGAP;
1777 }
1778 }
1779 if (cflag) {
1780 if (merge) {
1781 (void)fputs(
1782 "pr: -m cannot be used with multiple columns\n", errf);
1783 return(1);
1784 }
1785 if (nmwd) {
1786 colwd = (pgwd + 1 - (clcnt * (nmwd + 2)))/clcnt;
1787 pgwd = ((colwd + nmwd + 2) * clcnt) - 1;
1788 } else {
1789 colwd = (pgwd + 1 - clcnt)/clcnt;
1790 pgwd = ((colwd + 1) * clcnt) - 1;
1791 }
1792 if (colwd < 1) {
1793 (void)fprintf(errf,
1794 "pr: page width is too small for %d columns\n",clcnt);
1795 return(1);
1796 }
1797 }
1798 if (!lines)
1799 lines = LINES;
1800
1801 /*
1802 * make sure long enough for headers. if not disable
1803 */
1804 if (lines <= HEADLEN + TAILLEN)
1805 ++nohead;
1806 else if (!nohead)
1807 lines -= HEADLEN + TAILLEN;
1808
1809 /*
1810 * adjust for double space on odd length pages
1811 */
1812 if (dspace) {
1813 if (lines == 1)
1814 dspace = 0;
1815 else {
1816 if (lines & 1)
1817 ++addone;
1818 lines /= 2;
1819 }
1820 }
1821
1822 return(0);
1823 }
1824