1 /* $NetBSD: pr.c,v 1.19 2008/07/21 14:19:24 lukem Exp $ */
4 * Copyright (c) 1991 Keith Muller.
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1993\
39 The Regents of the University of California. All rights reserved.");
44 from
: static char sccsid
[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93";
46 __RCSID("$NetBSD: pr.c,v 1.19 2008/07/21 14:19:24 lukem Exp $");
50 #include <sys/types.h>
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
74 * Complies with posix P1003.2/D11
80 int pgnm
; /* starting page number */
81 int clcnt
; /* number of columns */
82 int colwd
; /* column data width - multiple columns */
83 int across
; /* mult col flag; write across page */
84 int dspace
; /* double space flag */
85 char inchar
; /* expand input char */
86 int ingap
; /* expand input gap */
87 int formfeed
; /* use formfeed as trailer */
88 char *header
; /* header name instead of file name */
89 char ochar
; /* contract output char */
90 int ogap
; /* contract output gap */
91 int lines
; /* number of lines per page */
92 int merge
; /* merge multiple files in output */
93 char nmchar
; /* line numbering append char */
94 int nmwd
; /* width of line number field */
95 int offst
; /* number of page offset spaces */
96 int nodiag
; /* do not report file open errors */
97 char schar
; /* text column separation character */
98 int sflag
; /* -s option for multiple columns */
99 int nohead
; /* do not write head and trailer */
100 int pgwd
; /* page width with multiple col output */
101 const char *timefrmt
= TIMEFMT
; /* time conversion string */
106 FILE *err
; /* error message file pointer */
107 int addone
; /* page length is odd with double space */
108 int errcnt
; /* error count on file processing */
109 char digs
[] = "0123456789"; /* page number translation map */
111 int main
__P((int, char **));
120 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
121 (void)signal(SIGINT
, terminate
);
122 ret_val
= setup(argc
, argv
);
125 * select the output format based on options
128 ret_val
= mulfile(argc
, argv
);
130 ret_val
= onecol(argc
, argv
);
132 ret_val
= horzcol(argc
, argv
);
134 ret_val
= vertcol(argc
, argv
);
138 if (errcnt
|| ret_val
)
144 * onecol: print files with only one column of output.
145 * Line length is unlimited.
179 * allocate line buffer
181 if ((obuf
= malloc((unsigned)(LBUF
+ off
)*sizeof(char))) == NULL
)
184 * allocate header buffer
186 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
189 ohbuf
= hbuf
+ offst
;
193 nbuf
[--num
] = nmchar
;
195 (void)memset(obuf
, (int)' ', offst
);
196 (void)memset(hbuf
, (int)' ', offst
);
202 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
205 * skip to specified page
207 if (inskip(inf
, pgnm
, lines
))
227 while (linecnt
< lines
) {
231 if ((cnt
= inln(inf
,lbuf
,LBUF
,&cps
,0,&mor
)) < 0)
233 if (!linecnt
&& !nohead
&&
234 prhead(hbuf
, fname
, pagecnt
))
242 addnum(nbuf
, num
, ++lncnt
);
243 if (otln(obuf
,cnt
+off
, &ips
, &ops
, mor
))
245 } else if (otln(lbuf
, cnt
, &ips
, &ops
, mor
))
249 * if line bigger than buffer, get more
257 * whole line rcvd. reset tab proc. state
266 * fill to end of page
268 if (linecnt
&& prtail(lines
-linecnt
-lrgln
, lrgln
))
272 * On EOF go to next file
292 if (inf
!= NULL
&& inf
!= stdin
)
298 * vertcol: print files with more than one column of output down a page
306 char **lstdat
= NULL
;
318 int mxlen
= pgwd
+ offst
+ 1;
319 int mclcnt
= clcnt
- 1;
320 struct vcol
*vc
= NULL
;
337 * allocate page buffer
339 if ((buf
= malloc((unsigned)lines
*mxlen
*sizeof(char))) == NULL
)
343 * allocate page header
345 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
347 ohbuf
= hbuf
+ offst
;
349 (void)memset(hbuf
, (int)' ', offst
);
352 * col pointers when no headers
355 if ((vc
= malloc((unsigned)mvc
*sizeof(struct vcol
))) == NULL
)
359 * pointer into page where last data per line is located
361 if ((lstdat
= malloc((unsigned)lines
*sizeof(char *))) == NULL
)
365 * fast index lookups to locate start of lines
367 if ((indy
= malloc((unsigned)lines
*sizeof(int))) == NULL
)
369 if ((lindy
= malloc((unsigned)lines
*sizeof(int))) == NULL
)
378 * initialize buffer lookup indexes and offset area
380 for (j
= 0; j
< lines
; ++j
) {
381 lindy
[j
] = j
* mxlen
;
382 indy
[j
] = lindy
[j
] + offst
;
384 ptbf
= buf
+ lindy
[j
];
385 (void)memset(ptbf
, (int)' ', offst
);
388 ptbf
= buf
+ indy
[j
];
395 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
398 * skip to requested page
400 if (inskip(inf
, pgnm
, lines
))
415 for (i
= 0; i
< clcnt
; ++i
) {
418 * if last column, do not pad
429 * is this first column
432 ptbf
= buf
+ indy
[j
];
442 addnum(ptbf
, nmwd
, ++lncnt
);
450 cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,&mor
);
457 * pad all but last column on page
461 * pad to end of column
465 else if ((pln
= col
-cnt
) > 0) {
472 * remember last char in line
483 * when -t (no header) is specified the spec requires
484 * the min number of lines. The last page may not have
485 * balanced length columns. To fix this we must reorder
486 * the columns. This is a very slow technique so it is
487 * only used under limited conditions. Without -t, the
488 * balancing of text columns is unspecified. To NOT
489 * balance the last page, add the global variable
490 * nohead to the if statement below e.g.
492 * if ((cnt < 0) && nohead && cvc ......
497 * check to see if last page needs to be reordered
499 if ((cnt
< 0) && cvc
&& ((mvc
-cvc
) >= clcnt
)){
507 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
509 for (i
= 0; i
< pln
; ++i
) {
512 if (offst
&& otln(buf
,offst
,&ips
,&ops
,1)) {
518 for (j
= 0; j
< clcnt
; ++j
) {
520 * determine column length
533 cnt
= vc
[tvc
].cnt
+ 1;
538 if (otln(vc
[tvc
].pt
, cnt
, &ips
,
548 if (otln(buf
, 0, &ips
, &ops
, 0))
554 if (prtail((lines
- pln
), 0))
557 * done with output, go to next file
563 * determine how many lines to output
573 if (pln
&& !nohead
&& prhead(hbuf
, fname
, pagecnt
))
579 for (i
= 0; i
< pln
; ++i
) {
580 ptbf
= buf
+ lindy
[i
];
581 if ((j
= lstdat
[i
] - ptbf
) <= offst
)
583 if (otln(ptbf
, j
, &ips
, &ops
, 0))
590 if (pln
&& prtail((lines
- pln
), 0))
594 * if EOF go to next file
620 if (inf
!= NULL
&& inf
!= stdin
)
626 * horzcol: print files with more than one column of output across a page
653 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
)
659 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
661 ohbuf
= hbuf
+ offst
;
663 (void)memset(buf
, (int)' ', offst
);
664 (void)memset(hbuf
, (int)' ', offst
);
670 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
672 if (inskip(inf
, pgnm
, lines
))
686 for (i
= 0; i
< lines
; ++i
) {
696 * add number to column
698 addnum(ptbf
, nmwd
, ++lncnt
);
705 if ((cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,
712 * if last line skip padding
718 * pad to end of column
722 else if ((pln
= col
- cnt
) > 0) {
723 (void)memset(ptbf
,(int)' ',pln
);
729 * determine line length
731 if ((j
= lstdat
- buf
) <= offst
)
734 prhead(hbuf
, fname
, pagecnt
))
739 if (otln(buf
, j
, &ips
, &ops
, 0))
746 if (i
&& prtail(lines
-i
, 0))
750 * if EOF go to next file
770 if (inf
!= NULL
&& inf
!= stdin
)
776 * mulfile: print files with more than one column of output and
777 * more than one file concurrently
807 * array of FILE *, one for each operand
809 if ((fbuf
= calloc(clcnt
, sizeof(FILE *))) == NULL
)
815 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
817 ohbuf
= hbuf
+ offst
;
820 * do not know how many columns yet. The number of operands provide an
821 * upper bound on the number of columns. We use the number of files
822 * we can open successfully to set the number of columns. The operation
823 * of the merge operation (-m) in relation to unsuccesful file opens
824 * is unspecified by posix.
828 if ((fbuf
[j
] = nxtfile(argc
, argv
, &fname
, ohbuf
, 1)) == NULL
)
830 if (pgnm
&& (inskip(fbuf
[j
], pgnm
, lines
)))
842 * calculate page boundries based on open file count
846 colwd
= (pgwd
- clcnt
- nmwd
)/clcnt
;
847 pgwd
= ((colwd
+ 1) * clcnt
) - nmwd
- 2;
849 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
850 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
854 "pr: page width too small for %d columns\n", clcnt
);
863 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
)
866 (void)memset(buf
, (int)' ', offst
);
867 (void)memset(hbuf
, (int)' ', offst
);
876 * continue to loop while any file still has data
882 for (i
= 0; i
< lines
; ++i
) {
887 * add line number to line
889 addnum(ptbf
, nmwd
, ++lncnt
);
899 for (j
= 0; j
< clcnt
; ++j
) {
900 if (fbuf
[j
] == NULL
) {
905 } else if ((cnt
= inln(fbuf
[j
], ptbf
, colwd
,
906 &cps
, 1, &mor
)) < 0) {
910 if (fbuf
[j
] != stdin
)
911 (void)fclose(fbuf
[j
]);
925 * if last ACTIVE column, done with line
931 * pad to end of column
935 } else if ((pln
= col
- cnt
) > 0) {
936 (void)memset(ptbf
, (int)' ', pln
);
942 * calculate data in line
944 if ((j
= lstdat
- buf
) <= offst
)
947 if (!i
&& !nohead
&& prhead(hbuf
, fname
, pagecnt
))
953 if (otln(buf
, j
, &ips
, &ops
, 0))
957 * if no more active files, done
968 if (i
&& prtail(lines
-i
, 0))
980 for (j
= 0; j
< clcnt
; j
++)
981 if (fbuf
[j
] && fbuf
[j
] != stdin
)
982 (void)fclose(fbuf
[j
]);
993 * inln(): input a line of data (unlimited length lines supported)
994 * Input is optionally expanded to spaces
999 * cps: column positon 1st char in buffer (large line support)
1000 * trnc: throw away data more than lim up to \n
1001 * mor: set if more data in line (not truncated)
1004 inln(inf
, buf
, lim
, cps
, trnc
, mor
)
1016 int chk
= (int)inchar
;
1022 * expanding input option
1024 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1026 * is this the input "tab" char
1030 * expand to number of spaces
1032 col
= (ptbuf
- buf
) + *cps
;
1033 col
= gap
- (col
% gap
);
1036 * if more than this line, push back
1038 if ((col
> lim
) && (ungetc(ch
, inf
) == EOF
))
1044 while ((--col
>= 0) && (--lim
>= 0))
1056 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1072 * entire line processed
1080 * line was larger than limit
1084 * throw away rest of line
1086 while ((ch
= getc(inf
)) != EOF
) {
1094 * save column offset if not truncated
1104 * otln(): output a line of data. (Supports unlimited length lines)
1105 * output is optionally contracted to tabs
1107 * buf: output buffer with data
1108 * cnt: number of chars of valid data in buf
1109 * svips: buffer input column position (for large lines)
1110 * svops: buffer output column position (for large lines)
1111 * mor: output line not complete in this buf; more data to come.
1112 * 1 is more, 0 is complete, -1 is no \n's
1115 otln(buf
, cnt
, svips
, svops
, mor
)
1122 int ops
; /* last col output */
1123 int ips
; /* last col in buf examined */
1130 * contracting on output
1135 while (buf
< endbuf
) {
1137 * count number of spaces and ochar in buffer
1146 * simulate ochar processing
1148 if (*buf
== ochar
) {
1149 ips
+= gap
- (ips
% gap
);
1155 * got a non space char; contract out spaces
1159 * use one space if necessary
1161 if (ips
- ops
== 1) {
1166 * use as many ochar as will fit
1168 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1170 if (putchar(ochar
) == EOF
) {
1179 * finish off with spaces
1181 if (putchar(' ') == EOF
) {
1189 * output non space char
1191 if (putchar(*buf
++) == EOF
) {
1201 * if incomplete line, save position counts
1211 * use one space if necessary
1213 if (ips
- ops
== 1) {
1218 * use as many ochar as will fit
1220 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1222 if (putchar(ochar
) == EOF
) {
1230 * finish off with spaces
1232 if (putchar(' ') == EOF
) {
1242 * output is not contracted
1244 if (cnt
&& (fwrite(buf
, sizeof(char), cnt
, stdout
) <= 0)) {
1253 * process line end and double space as required
1255 if ((putchar('\n') == EOF
) || (dspace
&& (putchar('\n') == EOF
))) {
1263 * inskip(): skip over pgcnt pages with lncnt lines per page
1264 * file is closed at EOF (if not stdin).
1266 * inf FILE * to read from
1267 * pgcnt number of pages to skip
1268 * lncnt number of lines per page
1271 inskip(inf
, pgcnt
, lncnt
)
1279 while(--pgcnt
> 0) {
1281 while ((c
= getc(inf
)) != EOF
) {
1282 if ((c
== '\n') && (--cnt
== 0))
1295 * nxtfile: returns a FILE * to next file in arg list and sets the
1296 * time field for this file (or current date).
1298 * buf array to store proper date for the header.
1299 * dt if set skips the date processing (used with -m)
1302 nxtfile(argc
, argv
, fname
, buf
, dt
)
1312 struct tm
*timeptr
= NULL
;
1313 struct stat statbuf
;
1315 static int twice
= -1;
1318 if (eoptind
>= argc
) {
1320 * no file listed; default, use standard input
1332 if (gettimeofday(&tv
, &tz
) < 0) {
1334 (void)fprintf(err
, "pr: cannot get time of day, %s\n",
1339 curtime
= tv
.tv_sec
;
1340 timeptr
= localtime(&curtime
);
1342 for (; eoptind
< argc
; ++eoptind
) {
1343 if (strcmp(argv
[eoptind
], "-") == 0) {
1345 * process a "-" for filename
1354 if (nohead
|| (dt
&& twice
))
1356 if (gettimeofday(&tv
, &tz
) < 0) {
1359 "pr: cannot get time of day, %s\n",
1363 curtime
= tv
.tv_sec
;
1364 timeptr
= localtime(&curtime
);
1367 * normal file processing
1369 if ((inf
= fopen(argv
[eoptind
], "r")) == NULL
) {
1373 (void)fprintf(err
, "pr: Cannot open %s, %s\n",
1374 argv
[eoptind
], strerror(errno
));
1382 *fname
= argv
[eoptind
];
1384 if (nohead
|| (dt
&& twice
))
1388 if (gettimeofday(&tv
, &tz
) < 0) {
1391 "pr: cannot get time of day, %s\n",
1395 curtime
= tv
.tv_sec
;
1396 timeptr
= localtime(&curtime
);
1398 if (fstat(fileno(inf
), &statbuf
) < 0) {
1402 "pr: Cannot stat %s, %s\n",
1403 argv
[eoptind
], strerror(errno
));
1406 timeptr
= localtime(&(statbuf
.st_mtime
));
1415 * set up time field used in header
1417 if (strftime(buf
, HDBUF
, timefrmt
, timeptr
) <= 0) {
1421 (void)fputs("pr: time conversion failed\n", err
);
1428 * addnum(): adds the line number to the column
1429 * Truncates from the front or pads with spaces as required.
1430 * Numbers are right justified.
1432 * buf buffer to store the number
1433 * wdth width of buffer to fill
1436 * NOTE: numbers occupy part of the column. The posix
1437 * spec does not specify if -i processing should or should not
1438 * occur on number padding. The spec does say it occupies
1439 * part of the column. The usage of addnum currently treats
1440 * numbers as part of the column so spaces may be replaced.
1443 addnum(buf
, wdth
, line
)
1448 char *pt
= buf
+ wdth
;
1451 *--pt
= digs
[line
% 10];
1453 } while (line
&& (pt
> buf
));
1456 * pad with space as required
1463 * prhead(): prints the top of page header
1465 * buf buffer with time field (and offset)
1466 * cnt number of chars in buf
1467 * fname fname field for header
1468 * pagcnt page number
1471 prhead(buf
, fname
, pagcnt
)
1479 if ((putchar('\n') == EOF
) || (putchar('\n') == EOF
)) {
1484 * posix is not clear if the header is subject to line length
1485 * restrictions. The specification for header line format
1486 * in the spec clearly does not limit length. No pr currently
1487 * restricts header length. However if we need to truncate in
1488 * an reasonable way, adjust the length of the printf by
1489 * changing HDFMT to allow a length max as an argument printf.
1490 * buf (which contains the offset spaces and time field could
1493 * note only the offset (if any) is processed for tab expansion
1495 if (offst
&& otln(buf
, offst
, &ips
, &ops
, -1))
1497 (void)printf(HDFMT
,buf
+offst
, fname
, pagcnt
);
1502 * prtail(): pad page with empty lines (if required) and print page trailer
1505 * cnt number of lines of padding needed
1506 * incomp was a '\n' missing from last line output
1515 * only pad with no headers when incomplete last line
1519 if ((dspace
&& (putchar('\n') == EOF
)) ||
1520 (putchar('\n') == EOF
)) {
1528 * if double space output two \n
1534 * if an odd number of lines per page, add an extra \n
1543 if ((incomp
&& (putchar('\n') == EOF
)) ||
1544 (putchar('\f') == EOF
)) {
1551 while (--cnt
>= 0) {
1552 if (putchar('\n') == EOF
) {
1561 * terminate(): when a SIGINT is recvd
1564 terminate(which_sig
)
1568 (void)raise_default_signal(which_sig
);
1574 * flsh_errs(): output saved up diagnostic messages after all normal
1575 * processing has completed
1582 (void)fflush(stdout
);
1587 while (fgets(buf
, BUFSIZ
, err
) != NULL
)
1588 (void)fputs(buf
, stderr
);
1594 (void)fputs("pr: memory allocation failed\n", err
);
1600 (void)fprintf(err
, "pr: write failure, %s\n", strerror(errno
));
1607 "usage: pr [+page] [-col] [-adFmrt] [-e[ch][gap]] [-h header]\n",err
);
1609 " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",err
);
1611 " [-s[ch]] [-w width] [-] [file ...]\n", err
);
1615 * setup: Validate command args, initialize and perform sanity
1629 if (isatty(fileno(stdout
))) {
1631 * defer diagnostics until processing is done
1633 if ((err
= tmpfile()) == NULL
) {
1634 (void)fputs("Cannot defer diagnostic messages\n",stderr
);
1639 while ((c
= egetopt(argc
, argv
, "#adFmrte?h:i?l:n?o:s?T:w:")) != -1) {
1642 if ((pgnm
= atoi(eoptarg
)) < 1) {
1643 (void)fputs("pr: +page number must be 1 or more\n",
1649 if ((clcnt
= atoi(eoptarg
)) < 1) {
1650 (void)fputs("pr: -columns must be 1 or more\n",err
);
1664 if ((eoptarg
!= NULL
) &&
1665 !isdigit((unsigned char)*eoptarg
))
1666 inchar
= *eoptarg
++;
1669 if ((eoptarg
!= NULL
) &&
1670 isdigit((unsigned char)*eoptarg
)) {
1671 if ((ingap
= atoi(eoptarg
)) < 0) {
1673 "pr: -e gap must be 0 or more\n", err
);
1678 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1680 "pr: invalid value for -e %s\n", eoptarg
);
1693 if ((eoptarg
!= NULL
) &&
1694 !isdigit((unsigned char)*eoptarg
))
1698 if ((eoptarg
!= NULL
) &&
1699 isdigit((unsigned char)*eoptarg
)) {
1700 if ((ogap
= atoi(eoptarg
)) < 0) {
1702 "pr: -i gap must be 0 or more\n", err
);
1707 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1709 "pr: invalid value for -i %s\n", eoptarg
);
1715 if (!isdigit((unsigned char)*eoptarg
) ||
1716 ((lines
=atoi(eoptarg
)) < 1)) {
1718 "pr: Number of lines must be 1 or more\n",err
);
1726 if ((eoptarg
!= NULL
) &&
1727 !isdigit((unsigned char)*eoptarg
))
1728 nmchar
= *eoptarg
++;
1731 if ((eoptarg
!= NULL
) &&
1732 isdigit((unsigned char)*eoptarg
)) {
1733 if ((nmwd
= atoi(eoptarg
)) < 1) {
1735 "pr: -n width must be 1 or more\n",err
);
1738 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1740 "pr: invalid value for -n %s\n", eoptarg
);
1746 if (!isdigit((unsigned char)*eoptarg
) ||
1747 ((offst
= atoi(eoptarg
))< 1)){
1748 (void)fputs("pr: -o offset must be 1 or more\n",
1758 if (eoptarg
== NULL
)
1762 if (eoptarg
&& *eoptarg
!= '\0') {
1764 "pr: invalid value for -s %s\n", eoptarg
);
1776 if (!isdigit((unsigned char)*eoptarg
) ||
1777 ((pgwd
= atoi(eoptarg
)) < 1)){
1779 "pr: -w width must be 1 or more \n",err
);
1790 * default and sanity checks
1794 if ((clcnt
= argc
- eoptind
) <= 1) {
1803 (void)fputs("pr: -a flag requires multiple columns\n",
1808 (void)fputs("pr: -m cannot be used with -a\n", err
);
1818 if (cflag
|| merge
) {
1831 "pr: -m cannot be used with multiple columns\n", err
);
1835 colwd
= (pgwd
+ 1 - (clcnt
* (nmwd
+ 2)))/clcnt
;
1836 pgwd
= ((colwd
+ nmwd
+ 2) * clcnt
) - 1;
1838 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
1839 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
1843 "pr: page width is too small for %d columns\n",clcnt
);
1851 * make sure long enough for headers. if not disable
1853 if (lines
<= HEADLEN
+ TAILLEN
)
1856 lines
-= HEADLEN
+ TAILLEN
;
1859 * adjust for double space on odd length pages