]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/pr/pr.c
2 * Copyright (c) 1991 Keith Muller.
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright
[] =
40 "@(#) Copyright (c) 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
46 static char sccsid
[] = "@(#)pr.c 8.2 (Berkeley) 4/16/94";
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD: src/usr.bin/pr/pr.c,v 1.18 2004/07/26 20:24:59 charnier Exp $");
53 #include <sys/types.h>
72 #include <get_compat.h>
73 /* err.h defines err(1) which conflicts with the global below */
74 extern void errx(int, const char *, ...) __dead2
__printflike(2, 3);
76 #define COMPAT_MODE(a,b) (1)
77 #endif /* __APPLE__ */
80 * pr: a printing and pagination filter. If multiple input files
81 * are specified, each is read, formatted, and written to standard
82 * output. By default, input is separated into 66-line pages, each
83 * with a header that includes the page number, date, time and the
86 * Complies with posix P1003.2/D11
92 int pgnm
; /* starting page number */
93 int clcnt
; /* number of columns */
94 int colwd
; /* column data width - multiple columns */
95 int across
; /* mult col flag; write across page */
96 int dspace
; /* double space flag */
97 char inchar
; /* expand input char */
98 int ingap
; /* expand input gap */
99 int pausefst
; /* Pause before first page */
100 int pauseall
; /* Pause before each page */
101 int formfeed
; /* use formfeed as trailer */
102 char *header
; /* header name instead of file name */
103 char ochar
; /* contract output char */
104 int ogap
; /* contract output gap */
105 int lines
; /* number of lines per page */
106 int merge
; /* merge multiple files in output */
107 char nmchar
; /* line numbering append char */
108 int nmwd
; /* width of line number field */
109 int offst
; /* number of page offset spaces */
110 int nodiag
; /* do not report file open errors */
111 char schar
; /* text column separation character */
112 int sflag
; /* -s option for multiple columns */
113 int nohead
; /* do not write head and trailer */
114 int pgwd
; /* page width with multiple col output */
115 const char *timefrmt
; /* time conversion string */
120 FILE *err
; /* error message file pointer */
121 int addone
; /* page length is odd with double space */
122 int errcnt
; /* error count on file processing */
123 char digs
[] = "0123456789"; /* page number translation map */
125 char fnamedefault
[] = FNAME
;
127 static char first_char
; /* first fill character */
130 main(int argc
, char *argv
[])
134 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
135 (void)signal(SIGINT
, terminate
);
136 ret_val
= setup(argc
, argv
);
137 first_char
= (COMPAT_MODE("bin/pr", "Unix2003") ? ochar
: ' ');
140 * select the output format based on options
143 ret_val
= mulfile(argc
, argv
);
145 ret_val
= onecol(argc
, argv
);
147 ret_val
= horzcol(argc
, argv
);
149 ret_val
= vertcol(argc
, argv
);
153 if (errcnt
|| ret_val
)
159 * Check if we should pause and write an alert character and wait for a
160 * carriage return on /dev/tty.
163 ttypause(int pagecnt
)
168 if ((pauseall
|| (pausefst
&& pagecnt
== 1)) &&
169 isatty(STDOUT_FILENO
)) {
170 if ((ttyfp
= fopen("/dev/tty", "r")) != NULL
) {
171 (void)putc('\a', stderr
);
172 while ((pch
= getc(ttyfp
)) != '\n' && pch
!= EOF
)
180 * onecol: print files with only one column of output.
181 * Line length is unlimited.
184 onecol(int argc
, char *argv
[])
212 * allocate line buffer
214 if ((obuf
= malloc((unsigned)(LBUF
+ off
)*sizeof(char))) == NULL
) {
219 * allocate header buffer
221 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
226 ohbuf
= hbuf
+ offst
;
230 nbuf
[--num
] = nmchar
;
232 (void)memset(obuf
, (int)' ', offst
);
233 (void)memset(hbuf
, (int)' ', offst
);
239 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
242 * skip to specified page
244 if (inskip(inf
, pgnm
, lines
))
266 while (linecnt
< lines
) {
270 if ((cnt
= inln(inf
,lbuf
,LBUF
,&cps
,0,&mor
)) < 0)
272 if (!linecnt
&& !nohead
&&
273 prhead(hbuf
, fname
, pagecnt
))
281 addnum(nbuf
, num
, ++lncnt
);
282 if (otln(obuf
,cnt
+off
, &ips
, &ops
, mor
))
284 } else if (otln(lbuf
, cnt
, &ips
, &ops
, mor
))
288 * if line bigger than buffer, get more
296 * whole line rcvd. reset tab proc. state
305 * fill to end of page
307 if (linecnt
&& prtail(lines
-linecnt
-lrgln
, lrgln
))
311 * On EOF go to next file
326 * vertcol: print files with more than one column of output down a page
329 vertcol(int argc
, char *argv
[])
344 int mxlen
= pgwd
+ offst
+ 1;
345 int mclcnt
= clcnt
- 1;
362 * allocate page buffer
364 if ((buf
= malloc((unsigned)lines
*mxlen
*sizeof(char))) == NULL
) {
370 * allocate page header
372 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
376 ohbuf
= hbuf
+ offst
;
378 (void)memset(hbuf
, (int)' ', offst
);
381 * col pointers when no headers
385 (struct vcol
*)malloc((unsigned)mvc
*sizeof(struct vcol
))) == NULL
) {
391 * pointer into page where last data per line is located
393 if ((lstdat
= (char **)malloc((unsigned)lines
*sizeof(char *))) == NULL
){
399 * fast index lookups to locate start of lines
401 if ((indy
= (int *)malloc((unsigned)lines
*sizeof(int))) == NULL
) {
405 if ((lindy
= (int *)malloc((unsigned)lines
*sizeof(int))) == NULL
) {
416 * initialize buffer lookup indexes and offset area
418 for (j
= 0; j
< lines
; ++j
) {
419 lindy
[j
] = j
* mxlen
;
420 indy
[j
] = lindy
[j
] + offst
;
422 ptbf
= buf
+ lindy
[j
];
423 (void)memset(ptbf
, (int)' ', offst
);
426 ptbf
= buf
+ indy
[j
];
433 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
436 * skip to requested page
438 if (inskip(inf
, pgnm
, lines
))
455 for (i
= 0; i
< clcnt
; ++i
) {
458 * if last column, do not pad
469 * is this first column
472 ptbf
= buf
+ indy
[j
];
482 addnum(ptbf
, nmwd
, ++lncnt
);
490 cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,&mor
);
497 * pad all but last column on page
501 * pad to end of column
505 else if ((pln
= col
-cnt
) > 0) {
512 * remember last char in line
523 * when -t (no header) is specified the spec requires
524 * the min number of lines. The last page may not have
525 * balanced length columns. To fix this we must reorder
526 * the columns. This is a very slow technique so it is
527 * only used under limited conditions. Without -t, the
528 * balancing of text columns is unspecified. To NOT
529 * balance the last page, add the global variable
530 * nohead to the if statement below e.g.
532 * if ((cnt < 0) && nohead && cvc ......
537 * check to see if last page needs to be reordered
539 if ((cnt
< 0) && cvc
&& ((mvc
-cvc
) >= clcnt
)){
547 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
549 for (i
= 0; i
< pln
; ++i
) {
552 if (offst
&& otln(buf
,offst
,&ips
,&ops
,1))
556 for (j
= 0; j
< clcnt
; ++j
) {
558 * determine column length
571 cnt
= vc
[tvc
].cnt
+ 1;
576 if (otln(vc
[tvc
].pt
, cnt
, &ips
,
586 if (otln(buf
, 0, &ips
, &ops
, 0))
592 if (prtail((lines
- pln
), 0))
595 * done with output, go to next file
601 * determine how many lines to output
611 if (pln
&& !nohead
&& prhead(hbuf
, fname
, pagecnt
))
617 for (i
= 0; i
< pln
; ++i
) {
618 ptbf
= buf
+ lindy
[i
];
619 if ((j
= lstdat
[i
] - ptbf
) <= offst
)
621 if (otln(ptbf
, j
, &ips
, &ops
, 0))
628 if (pln
&& prtail((lines
- pln
), 0))
632 * if EOF go to next file
647 * horzcol: print files with more than one column of output across a page
650 horzcol(int argc
, char *argv
[])
671 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
) {
679 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
683 ohbuf
= hbuf
+ offst
;
685 (void)memset(buf
, (int)' ', offst
);
686 (void)memset(hbuf
, (int)' ', offst
);
692 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
694 if (inskip(inf
, pgnm
, lines
))
710 for (i
= 0; i
< lines
; ++i
) {
720 * add number to column
722 addnum(ptbf
, nmwd
, ++lncnt
);
729 if ((cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,
736 * if last line skip padding
742 * pad to end of column
746 else if ((pln
= col
- cnt
) > 0) {
747 (void)memset(ptbf
,(int)' ',pln
);
753 * determine line length
755 if ((j
= lstdat
- buf
) <= offst
)
758 prhead(hbuf
, fname
, pagecnt
))
763 if (otln(buf
, j
, &ips
, &ops
, 0))
770 if (i
&& prtail(lines
-i
, 0))
774 * if EOF go to next file
789 * mulfile: print files with more than one column of output and
790 * more than one file concurrently
793 mulfile(int argc
, char *argv
[])
817 * array of FILE *, one for each operand
819 if ((fbuf
= (FILE **)malloc((unsigned)clcnt
*sizeof(FILE *))) == NULL
) {
827 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
831 ohbuf
= hbuf
+ offst
;
834 * do not know how many columns yet. The number of operands provide an
835 * upper bound on the number of columns. We use the number of files
836 * we can open successfully to set the number of columns. The operation
837 * of the merge operation (-m) in relation to unsuccesful file opens
838 * is unspecified by posix.
842 if ((fbuf
[j
] = nxtfile(argc
, argv
, &fname
, ohbuf
, 1)) == NULL
)
844 if (pgnm
&& (inskip(fbuf
[j
], pgnm
, lines
)))
856 * calculate page boundries based on open file count
860 colwd
= (pgwd
- clcnt
- nmwd
)/clcnt
;
861 pgwd
= ((colwd
+ 1) * clcnt
) - nmwd
- 2;
863 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
864 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
868 "pr: page width too small for %d columns\n", clcnt
);
877 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
) {
882 (void)memset(buf
, (int)' ', offst
);
883 (void)memset(hbuf
, (int)' ', offst
);
892 * continue to loop while any file still has data
900 for (i
= 0; i
< lines
; ++i
) {
905 * add line number to line
907 addnum(ptbf
, nmwd
, ++lncnt
);
917 for (j
= 0; j
< clcnt
; ++j
) {
918 if (fbuf
[j
] == NULL
) {
923 } else if ((cnt
= inln(fbuf
[j
], ptbf
, colwd
,
924 &cps
, 1, &mor
)) < 0) {
928 if (fbuf
[j
] != stdin
)
929 (void)fclose(fbuf
[j
]);
943 * if last ACTIVE column, done with line
949 * pad to end of column
953 } else if ((pln
= col
- cnt
) > 0) {
954 (void)memset(ptbf
, (int)' ', pln
);
960 * calculate data in line
962 if ((j
= lstdat
- buf
) <= offst
)
965 if (!i
&& !nohead
&& prhead(hbuf
, fname
, pagecnt
))
971 if (otln(buf
, j
, &ips
, &ops
, 0))
975 * if no more active files, done
986 if (i
&& prtail(lines
-i
, 0))
996 * inln(): input a line of data (unlimited length lines supported)
997 * Input is optionally expanded to spaces
1001 * lim: buffer length
1002 * cps: column positon 1st char in buffer (large line support)
1003 * trnc: throw away data more than lim up to \n
1004 * mor: set if more data in line (not truncated)
1007 inln(FILE *inf
, char *buf
, int lim
, int *cps
, int trnc
, int *mor
)
1013 int chk
= (int)inchar
;
1019 * expanding input option
1021 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1023 * is this the input "tab" char
1027 * expand to number of spaces
1029 col
= (ptbuf
- buf
) + *cps
;
1030 col
= gap
- (col
% gap
);
1033 * if more than this line, push back
1035 if ((col
> lim
) && (ungetc(ch
, inf
) == EOF
))
1041 while ((--col
>= 0) && (--lim
>= 0))
1053 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1062 errx(EX_IOERR
, NULL
);
1072 * entire line processed
1080 * line was larger than limit
1084 * throw away rest of line
1086 while ((ch
= getc(inf
)) != EOF
) {
1091 errx(EX_IOERR
, NULL
);
1097 * save column offset if not truncated
1107 * otln(): output a line of data. (Supports unlimited length lines)
1108 * output is optionally contracted to tabs
1110 * buf: output buffer with data
1111 * cnt: number of chars of valid data in buf
1112 * svips: buffer input column position (for large lines)
1113 * svops: buffer output column position (for large lines)
1114 * mor: output line not complete in this buf; more data to come.
1115 * 1 is more, 0 is complete, -1 is no \n's
1118 otln(char *buf
, int cnt
, int *svips
, int *svops
, int mor
)
1120 int ops
; /* last col output */
1121 int ips
; /* last col in buf examined */
1128 * contracting on output
1133 while (buf
< endbuf
) {
1135 * count number of spaces and ochar in buffer
1144 * simulate ochar processing
1146 if (*buf
== ochar
) {
1147 ips
+= gap
- (ips
% gap
);
1153 * got a non space char; contract out spaces
1155 while (ips
- ops
> 1) {
1157 * use as many ochar as will fit
1159 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1161 if (gap
- 1 == (ops
% gap
)) /* use space to get to immediately following tab stop */
1162 putchar(first_char
);
1163 else if (putchar(ochar
) == EOF
) {
1172 * finish off with spaces
1174 if (putchar(' ') == EOF
) {
1182 * output non space char
1184 if (putchar(*buf
++) == EOF
) {
1194 * if incomplete line, save position counts
1202 while (ips
- ops
> 1) {
1204 * use as many ochar as will fit
1206 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1208 if (putchar(ochar
) == EOF
) {
1216 * finish off with spaces
1218 if (putchar(' ') == EOF
) {
1228 * output is not contracted
1230 if (cnt
&& (fwrite(buf
, sizeof(char), cnt
, stdout
) <= 0)) {
1239 * process line end and double space as required
1241 if ((putchar('\n') == EOF
) || (dspace
&& (putchar('\n') == EOF
))) {
1249 * inskip(): skip over pgcnt pages with lncnt lines per page
1250 * file is closed at EOF (if not stdin).
1252 * inf FILE * to read from
1253 * pgcnt number of pages to skip
1254 * lncnt number of lines per page
1257 inskip(FILE *inf
, int pgcnt
, int lncnt
)
1262 while(--pgcnt
> 0) {
1264 while ((c
= getc(inf
)) != EOF
) {
1265 if ((c
== '\n') && (--cnt
== 0))
1269 errx(EX_IOERR
, NULL
);
1281 * nxtfile: returns a FILE * to next file in arg list and sets the
1282 * time field for this file (or current date).
1284 * buf array to store proper date for the header.
1285 * dt if set skips the date processing (used with -m)
1288 nxtfile(int argc
, char **argv
, const char **fname
, char *buf
, int dt
)
1294 struct tm
*timeptr
= NULL
;
1295 struct stat statbuf
;
1296 static int twice
= -1;
1299 if (eoptind
>= argc
) {
1301 * no file listed; default, use standard input
1310 *fname
= fnamedefault
;
1313 if (gettimeofday(&tv
, &tz
) < 0) {
1315 (void)fprintf(err
, "pr: cannot get time of day, %s\n",
1321 timeptr
= localtime(&tv_sec
);
1323 for (; eoptind
< argc
&& argv
[eoptind
]; ++eoptind
) {
1324 if (strcmp(argv
[eoptind
], "-") == 0) {
1326 * process a "-" for filename
1333 *fname
= fnamedefault
;
1335 if (nohead
|| (dt
&& twice
))
1337 if (gettimeofday(&tv
, &tz
) < 0) {
1340 "pr: cannot get time of day, %s\n",
1345 timeptr
= localtime(&tv_sec
);
1348 * normal file processing
1350 if ((inf
= fopen(argv
[eoptind
], "r")) == NULL
) {
1354 (void)fprintf(err
, "pr: cannot open %s, %s\n",
1355 argv
[eoptind
], strerror(errno
));
1361 *fname
= fnamedefault
;
1363 *fname
= argv
[eoptind
];
1365 if (nohead
|| (dt
&& twice
))
1369 if (gettimeofday(&tv
, &tz
) < 0) {
1372 "pr: cannot get time of day, %s\n",
1377 timeptr
= localtime(&tv_sec
);
1379 if (fstat(fileno(inf
), &statbuf
) < 0) {
1383 "pr: cannot stat %s, %s\n",
1384 argv
[eoptind
], strerror(errno
));
1387 timeptr
= localtime(&(statbuf
.st_mtime
));
1396 * set up time field used in header
1398 if (strftime(buf
, HDBUF
, timefrmt
, timeptr
) <= 0) {
1402 (void)fputs("pr: time conversion failed\n", err
);
1409 * addnum(): adds the line number to the column
1410 * Truncates from the front or pads with spaces as required.
1411 * Numbers are right justified.
1413 * buf buffer to store the number
1414 * wdth width of buffer to fill
1417 * NOTE: numbers occupy part of the column. The posix
1418 * spec does not specify if -i processing should or should not
1419 * occur on number padding. The spec does say it occupies
1420 * part of the column. The usage of addnum currently treats
1421 * numbers as part of the column so spaces may be replaced.
1424 addnum(char *buf
, int wdth
, int line
)
1426 char *pt
= buf
+ wdth
;
1429 *--pt
= digs
[line
% 10];
1431 } while (line
&& (pt
> buf
));
1434 * pad with space as required
1441 * prhead(): prints the top of page header
1443 * buf buffer with time field (and offset)
1444 * cnt number of chars in buf
1445 * fname fname field for header
1446 * pagcnt page number
1449 prhead(char *buf
, const char *fname
, int pagcnt
)
1454 if ((putchar('\n') == EOF
) || (putchar('\n') == EOF
)) {
1459 * posix is not clear if the header is subject to line length
1460 * restrictions. The specification for header line format
1461 * in the spec clearly does not limit length. No pr currently
1462 * restricts header length. However if we need to truncate in
1463 * a reasonable way, adjust the length of the printf by
1464 * changing HDFMT to allow a length max as an arguement printf.
1465 * buf (which contains the offset spaces and time field could
1468 * note only the offset (if any) is processed for tab expansion
1470 if (offst
&& otln(buf
, offst
, &ips
, &ops
, -1))
1472 (void)printf(HDFMT
,buf
+offst
, fname
, pagcnt
);
1477 * prtail(): pad page with empty lines (if required) and print page trailer
1480 * cnt number of lines of padding needed
1481 * incomp was a '\n' missing from last line output
1484 prtail(int cnt
, int incomp
)
1488 * only pad with no headers when incomplete last line
1491 ((dspace
&& (putchar('\n') == EOF
)) ||
1492 (putchar('\n') == EOF
))) {
1497 * but honor the formfeed request
1500 if (putchar('\f') == EOF
) {
1508 * if double space output two \n
1514 * if an odd number of lines per page, add an extra \n
1523 if ((incomp
&& (putchar('\n') == EOF
)) ||
1524 (putchar('\f') == EOF
)) {
1531 while (--cnt
>= 0) {
1532 if (putchar('\n') == EOF
) {
1541 * terminate(): when a SIGINT is recvd
1544 terminate(int which_sig __unused
)
1552 * flsh_errs(): output saved up diagnostic messages after all normal
1553 * processing has completed
1560 (void)fflush(stdout
);
1565 while (fgets(buf
, BUFSIZ
, err
) != NULL
)
1566 (void)fputs(buf
, stderr
);
1572 (void)fputs("pr: memory allocation failed\n", err
);
1578 (void)fprintf(err
, "pr: write failure, %s\n", strerror(errno
));
1585 "usage: pr [+page] [-col] [-adFfmprt] [-e[ch][gap]] [-h header]\n",
1588 " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",err
);
1590 " [-L locale] [-s[ch]] [-w width] [-] [file ...]\n", err
);
1594 * setup: Validate command args, initialize and perform sanity
1598 setup(int argc
, char *argv
[])
1608 if (isatty(fileno(stdout
))) {
1610 * defer diagnostics until processing is done
1612 if ((err
= tmpfile()) == NULL
) {
1614 (void)fputs("Cannot defer diagnostic messages\n",stderr
);
1619 while ((c
= egetopt(argc
, argv
, "#adFfmrte?h:i?L:l:n?o:ps?w:")) != -1) {
1622 if ((pgnm
= atoi(eoptarg
)) < 1) {
1623 (void)fputs("pr: +page number must be 1 or more\n",
1629 if ((clcnt
= atoi(eoptarg
)) < 1) {
1630 (void)fputs("pr: -columns must be 1 or more\n",err
);
1644 if ((eoptarg
!= NULL
) && !isdigit((unsigned char)*eoptarg
))
1645 inchar
= *eoptarg
++;
1648 if ((eoptarg
!= NULL
) && isdigit((unsigned char)*eoptarg
)) {
1649 if ((ingap
= atoi(eoptarg
)) < 0) {
1651 "pr: -e gap must be 0 or more\n", err
);
1656 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1658 "pr: invalid value for -e %s\n", eoptarg
);
1674 if ((eoptarg
!= NULL
) && !isdigit((unsigned char)*eoptarg
))
1678 if ((eoptarg
!= NULL
) && isdigit((unsigned char)*eoptarg
)) {
1679 if ((ogap
= atoi(eoptarg
)) < 0) {
1681 "pr: -i gap must be 0 or more\n", err
);
1686 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1688 "pr: invalid value for -i %s\n", eoptarg
);
1697 if (!isdigit((unsigned char)*eoptarg
) || ((lines
=atoi(eoptarg
)) < 1)) {
1699 "pr: number of lines must be 1 or more\n",err
);
1707 if ((eoptarg
!= NULL
) && !isdigit((unsigned char)*eoptarg
))
1708 nmchar
= *eoptarg
++;
1711 if ((eoptarg
!= NULL
) && isdigit((unsigned char)*eoptarg
)) {
1712 if ((nmwd
= atoi(eoptarg
)) < 1) {
1714 "pr: -n width must be 1 or more\n",err
);
1717 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1719 "pr: invalid value for -n %s\n", eoptarg
);
1725 if (!isdigit((unsigned char)*eoptarg
) || ((offst
= atoi(eoptarg
))< 1)){
1726 (void)fputs("pr: -o offset must be 1 or more\n",
1739 if (eoptarg
== NULL
)
1743 if (*eoptarg
!= '\0') {
1745 "pr: invalid value for -s %s\n",
1756 if (!isdigit((unsigned char)*eoptarg
) || ((pgwd
= atoi(eoptarg
)) < 1)){
1758 "pr: -w width must be 1 or more \n",err
);
1769 * default and sanity checks
1773 if ((clcnt
= argc
- eoptind
) <= 1) {
1782 (void)fputs("pr: -a flag requires multiple columns\n",
1787 (void)fputs("pr: -m cannot be used with -a\n", err
);
1797 if (cflag
|| merge
) {
1810 "pr: -m cannot be used with multiple columns\n", err
);
1814 colwd
= (pgwd
+ 1 - (clcnt
* (nmwd
+ 2)))/clcnt
;
1815 pgwd
= ((colwd
+ nmwd
+ 2) * clcnt
) - 1;
1817 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
1818 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
1822 "pr: page width is too small for %d columns\n",clcnt
);
1830 * make sure long enough for headers. if not disable
1832 if (lines
<= HEADLEN
+ TAILLEN
)
1835 lines
-= HEADLEN
+ TAILLEN
;
1838 * adjust for double space on odd length pages
1850 (void) setlocale(LC_TIME
, (Lflag
!= NULL
) ? Lflag
: "");
1852 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
1853 timefrmt
= strdup(d_first
? TIMEFMTD
: TIMEFMTM
);