2 * SPDX-License-Identifier: BSD-4-Clause
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/types.h>
74 #define HDFMT "%s %s Page %d\n\n\n"
77 #define TIMEFMTD "%e %b %H:%M %Y"
78 #define TIMEFMTM "%b %e %H:%M %Y"
84 * structure for vertical columns. Used to balance cols on last page
87 char *pt
; /* ptr to col */
88 int cnt
; /* char count */
93 * pr: a printing and pagination filter. If multiple input files
94 * are specified, each is read, formatted, and written to standard
95 * output. By default, input is separated into 66-line pages, each
96 * with a header that includes the page number, date, time and the
99 * Complies with posix P1003.2/D11
103 * parameter variables
105 static int pgnm
; /* starting page number */
106 static int clcnt
; /* number of columns */
107 static int colwd
; /* column data width - multiple columns */
108 static int across
; /* mult col flag; write across page */
109 static int dspace
; /* double space flag */
110 static char inchar
; /* expand input char */
111 static int ingap
; /* expand input gap */
112 static int pausefst
; /* Pause before first page */
113 static int pauseall
; /* Pause before each page */
114 static int formfeed
; /* use formfeed as trailer */
115 static char *header
; /* header name instead of file name */
116 static char ochar
; /* contract output char */
117 static int ogap
; /* contract output gap */
118 static int lines
; /* number of lines per page */
119 static int merge
; /* merge multiple files in output */
120 static char nmchar
; /* line numbering append char */
121 static int nmwd
; /* width of line number field */
122 static int offst
; /* number of page offset spaces */
123 static int nodiag
; /* do not report file open errors */
124 static char schar
; /* text column separation character */
125 static int sflag
; /* -s option for multiple columns */
126 static int nohead
; /* do not write head and trailer */
127 static int pgwd
; /* page width with multiple col output */
128 static char *timefrmt
; /* time conversion string */
133 static FILE *err
; /* error message file pointer */
134 static int addone
; /* page length is odd with double space */
135 static int errcnt
; /* error count on file processing */
136 static char digs
[] = "0123456789"; /* page number translation map */
138 static char fnamedefault
[] = FNAME
;
140 /* forward decls for internal helper funcs */
141 static void mfail(void);
142 static void pfail(void);
143 static void usage(void);
144 static void flsh_errs(void);
145 static void terminate(int which_sig
);
146 static int prtail(int cnt
, int incomp
);
147 static int setup(int argc
, char *argv
[]);
148 static int onecol(int argc
, char *argv
[]);
149 static int vertcol(int argc
, char *argv
[]);
150 static int horzcol(int argc
, char *argv
[]);
151 static int mulfile(int argc
, char *argv
[]);
152 static void addnum(char *buf
, int wdth
, int line
);
153 static int inskip(FILE *inf
, int pgcnt
, int lncnt
);
154 static int prhead(char *buf
, const char *fname
, int pagcnt
);
155 static int inln(FILE *inf
, char *buf
, int lim
, int *cps
, int trnc
, int *mor
);
156 static int otln(char *buf
, int cnt
, int *svips
, int *svops
, int mor
);
157 static FILE *nxtfile(int argc
, char **argv
, const char **fname
, char *buf
, int dt
);
159 /* forward decls for egetopt */
163 extern char *eoptarg
;
164 static int egetopt(int nargc
, char * const *nargv
, const char *ostr
);
167 main(int argc
, char *argv
[])
171 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
172 (void)signal(SIGINT
, terminate
);
173 ret_val
= setup(argc
, argv
);
176 * select the output format based on options
179 ret_val
= mulfile(argc
, argv
);
181 ret_val
= onecol(argc
, argv
);
183 ret_val
= horzcol(argc
, argv
);
185 ret_val
= vertcol(argc
, argv
);
190 if (errcnt
|| ret_val
)
196 * Check if we should pause and write an alert character and wait for a
197 * carriage return on /dev/tty.
200 ttypause(int pagecnt
)
205 if ((pauseall
|| (pausefst
&& pagecnt
== 1)) &&
206 isatty(STDOUT_FILENO
)) {
207 if ((ttyfp
= fopen("/dev/tty", "r")) != NULL
) {
208 (void)putc('\a', stderr
);
209 while ((pch
= getc(ttyfp
)) != '\n' && pch
!= EOF
)
217 * onecol: print files with only one column of output.
218 * Line length is unlimited.
220 static int onecol(int argc
, char *argv
[])
248 * allocate line buffer
250 if ((obuf
= malloc((unsigned)(LBUF
+ off
)*sizeof(char))) == NULL
) {
255 * allocate header buffer
257 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
263 ohbuf
= hbuf
+ offst
;
267 nbuf
[--num
] = nmchar
;
269 (void)memset(obuf
, (int)' ', offst
);
270 (void)memset(hbuf
, (int)' ', offst
);
276 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
279 * skip to specified page
281 if (inskip(inf
, pgnm
, lines
))
303 while (linecnt
< lines
) {
307 if ((cnt
= inln(inf
,lbuf
,LBUF
,&cps
,0,&mor
)) < 0)
309 if (!linecnt
&& !nohead
&&
310 prhead(hbuf
, fname
, pagecnt
))
318 addnum(nbuf
, num
, ++lncnt
);
319 if (otln(obuf
,cnt
+off
, &ips
, &ops
, mor
))
321 } else if (otln(lbuf
, cnt
, &ips
, &ops
, mor
))
325 * if line bigger than buffer, get more
333 * whole line rcvd. reset tab proc. state
342 * fill to end of page
344 if (linecnt
&& prtail(lines
-linecnt
-lrgln
, lrgln
))
348 * On EOF go to next file
369 * vertcol: print files with more than one column of output down a page
371 static int vertcol(int argc
, char *argv
[])
374 char **lstdat
= NULL
;
386 int mxlen
= pgwd
+ offst
+ 1;
387 int mclcnt
= clcnt
- 1;
388 struct vcol
*vc
= NULL
;
405 * allocate page buffer
407 if ((buf
= malloc((unsigned)lines
*mxlen
*sizeof(char))) == NULL
) {
413 * allocate page header
415 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
419 ohbuf
= hbuf
+ offst
;
421 (void)memset(hbuf
, (int)' ', offst
);
424 * col pointers when no headers
428 (struct vcol
*)malloc((unsigned)mvc
*sizeof(struct vcol
))) == NULL
) {
434 * pointer into page where last data per line is located
436 if ((lstdat
= (char **)malloc((unsigned)lines
*sizeof(char *))) == NULL
){
442 * fast index lookups to locate start of lines
444 if ((indy
= (int *)malloc((unsigned)lines
*sizeof(int))) == NULL
) {
448 if ((lindy
= (int *)malloc((unsigned)lines
*sizeof(int))) == NULL
) {
459 * initialize buffer lookup indexes and offset area
461 for (j
= 0; j
< lines
; ++j
) {
462 lindy
[j
] = j
* mxlen
;
463 indy
[j
] = lindy
[j
] + offst
;
465 ptbf
= buf
+ lindy
[j
];
466 (void)memset(ptbf
, (int)' ', offst
);
469 ptbf
= buf
+ indy
[j
];
476 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
479 * skip to requested page
481 if (inskip(inf
, pgnm
, lines
))
498 for (i
= 0; i
< clcnt
; ++i
) {
501 * if last column, do not pad
512 * is this first column
515 ptbf
= buf
+ indy
[j
];
525 addnum(ptbf
, nmwd
, ++lncnt
);
533 cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,&mor
);
540 * pad all but last column on page
544 * pad to end of column
548 else if ((pln
= col
-cnt
) > 0) {
555 * remember last char in line
566 * when -t (no header) is specified the spec requires
567 * the min number of lines. The last page may not have
568 * balanced length columns. To fix this we must reorder
569 * the columns. This is a very slow technique so it is
570 * only used under limited conditions. Without -t, the
571 * balancing of text columns is unspecified. To NOT
572 * balance the last page, add the global variable
573 * nohead to the if statement below e.g.
575 * if ((cnt < 0) && nohead && cvc ......
580 * check to see if last page needs to be reordered
582 if ((cnt
< 0) && cvc
&& ((mvc
-cvc
) >= clcnt
)){
590 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
592 for (i
= 0; i
< pln
; ++i
) {
596 otln(buf
,offst
,&ips
,&ops
,1))
600 for (j
= 0; j
< clcnt
; ++j
) {
602 * determine column length
615 cnt
= vc
[tvc
].cnt
+ 1;
620 if (otln(vc
[tvc
].pt
, cnt
, &ips
,
630 if (otln(buf
, 0, &ips
, &ops
, 0))
636 if (prtail((lines
- pln
), 0))
639 * done with output, go to next file
645 * determine how many lines to output
655 if (pln
&& !nohead
&& prhead(hbuf
, fname
, pagecnt
))
661 for (i
= 0; i
< pln
; ++i
) {
662 ptbf
= buf
+ lindy
[i
];
663 if ((j
= lstdat
[i
] - ptbf
) <= offst
)
665 if (otln(ptbf
, j
, &ips
, &ops
, 0))
672 if (pln
&& prtail((lines
- pln
), 0))
676 * if EOF go to next file
699 * horzcol: print files with more than one column of output across a page
701 static int horzcol(int argc
, char *argv
[])
722 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
) {
730 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
735 ohbuf
= hbuf
+ offst
;
737 (void)memset(buf
, (int)' ', offst
);
738 (void)memset(hbuf
, (int)' ', offst
);
744 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
746 if (inskip(inf
, pgnm
, lines
))
762 for (i
= 0; i
< lines
; ++i
) {
772 * add number to column
774 addnum(ptbf
, nmwd
, ++lncnt
);
781 if ((cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,
788 * if last line skip padding
794 * pad to end of column
798 else if ((pln
= col
- cnt
) > 0) {
799 (void)memset(ptbf
,(int)' ',pln
);
805 * determine line length
807 if ((j
= lstdat
- buf
) <= offst
)
810 prhead(hbuf
, fname
, pagecnt
))
815 if (otln(buf
, j
, &ips
, &ops
, 0))
822 if (i
&& prtail(lines
-i
, 0))
826 * if EOF go to next file
847 * mulfile: print files with more than one column of output and
848 * more than one file concurrently
850 static int mulfile(int argc
, char *argv
[])
875 * array of FILE *, one for each operand
877 if ((fbuf
= (FILE **)malloc((unsigned)clcnt
*sizeof(FILE *))) == NULL
) {
885 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
) {
889 ohbuf
= hbuf
+ offst
;
892 * do not know how many columns yet. The number of operands provide an
893 * upper bound on the number of columns. We use the number of files
894 * we can open successfully to set the number of columns. The operation
895 * of the merge operation (-m) in relation to unsuccessful file opens
896 * is unspecified by posix.
900 if ((fbuf
[j
] = nxtfile(argc
, argv
, &fname
, ohbuf
, 1)) == NULL
)
902 if (pgnm
&& (inskip(fbuf
[j
], pgnm
, lines
)))
914 * calculate page boundaries based on open file count
918 colwd
= (pgwd
- clcnt
- nmwd
)/clcnt
;
919 pgwd
= ((colwd
+ 1) * clcnt
) - nmwd
- 2;
921 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
922 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
926 "pr: page width too small for %d columns\n", clcnt
);
935 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
) {
940 (void)memset(buf
, (int)' ', offst
);
941 (void)memset(hbuf
, (int)' ', offst
);
950 * continue to loop while any file still has data
958 for (i
= 0; i
< lines
; ++i
) {
963 * add line number to line
965 addnum(ptbf
, nmwd
, ++lncnt
);
975 for (j
= 0; j
< clcnt
; ++j
) {
976 if (fbuf
[j
] == NULL
) {
981 } else if ((cnt
= inln(fbuf
[j
], ptbf
, colwd
,
982 &cps
, 1, &mor
)) < 0) {
986 if (fbuf
[j
] != stdin
)
987 (void)fclose(fbuf
[j
]);
1001 * if last ACTIVE column, done with line
1007 * pad to end of column
1011 } else if ((pln
= col
- cnt
) > 0) {
1012 (void)memset(ptbf
, (int)' ', pln
);
1018 * calculate data in line
1020 if ((j
= lstdat
- buf
) <= offst
)
1023 if (!i
&& !nohead
&& prhead(hbuf
, fname
, pagecnt
))
1029 if (otln(buf
, j
, &ips
, &ops
, 0))
1033 * if no more active files, done
1042 * pad to end of page
1044 if (i
&& prtail(lines
-i
, 0))
1059 * inln(): input a line of data (unlimited length lines supported)
1060 * Input is optionally expanded to spaces
1064 * lim: buffer length
1065 * cps: column position 1st char in buffer (large line support)
1066 * trnc: throw away data more than lim up to \n
1067 * mor: set if more data in line (not truncated)
1069 static int inln(FILE *inf
, char *buf
, int lim
, int *cps
, int trnc
, int *mor
)
1075 int chk
= (int)inchar
;
1081 * expanding input option
1083 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1085 * is this the input "tab" char
1089 * expand to number of spaces
1091 col
= (ptbuf
- buf
) + *cps
;
1092 col
= gap
- (col
% gap
);
1095 * if more than this line, push back
1097 if ((col
> lim
) && (ungetc(ch
, inf
) == EOF
))
1103 while ((--col
>= 0) && (--lim
>= 0))
1115 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1131 * entire line processed
1139 * line was larger than limit
1143 * throw away rest of line
1145 while ((ch
= getc(inf
)) != EOF
) {
1153 * save column offset if not truncated
1163 * otln(): output a line of data. (Supports unlimited length lines)
1164 * output is optionally contracted to tabs
1166 * buf: output buffer with data
1167 * cnt: number of chars of valid data in buf
1168 * svips: buffer input column position (for large lines)
1169 * svops: buffer output column position (for large lines)
1170 * mor: output line not complete in this buf; more data to come.
1171 * 1 is more, 0 is complete, -1 is no \n's
1173 static int otln(char *buf
, int cnt
, int *svips
, int *svops
, int mor
)
1175 int ops
; /* last col output */
1176 int ips
; /* last col in buf examined */
1183 * contracting on output
1188 while (buf
< endbuf
) {
1190 * count number of spaces and ochar in buffer
1199 * simulate ochar processing
1201 if (*buf
== ochar
) {
1202 ips
+= gap
- (ips
% gap
);
1208 * got a non space char; contract out spaces
1210 while (ips
- ops
> 1) {
1212 * use as many ochar as will fit
1214 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1216 if (putchar(ochar
) == EOF
) {
1225 * finish off with spaces
1227 if (putchar(' ') == EOF
) {
1235 * output non space char
1237 if (putchar(*buf
++) == EOF
) {
1247 * if incomplete line, save position counts
1255 while (ips
- ops
> 1) {
1257 * use as many ochar as will fit
1259 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1261 if (putchar(ochar
) == EOF
) {
1269 * finish off with spaces
1271 if (putchar(' ') == EOF
) {
1281 * output is not contracted
1283 if (cnt
&& (fwrite(buf
, sizeof(char), cnt
, stdout
) <= 0)) {
1292 * process line end and double space as required
1294 if ((putchar('\n') == EOF
) || (dspace
&& (putchar('\n') == EOF
))) {
1302 * inskip(): skip over pgcnt pages with lncnt lines per page
1303 * file is closed at EOF (if not stdin).
1305 * inf FILE * to read from
1306 * pgcnt number of pages to skip
1307 * lncnt number of lines per page
1309 static int inskip(FILE *inf
, int pgcnt
, int lncnt
)
1314 while(--pgcnt
> 0) {
1316 while ((c
= getc(inf
)) != EOF
) {
1317 if ((c
== '\n') && (--cnt
== 0))
1330 * nxtfile: returns a FILE * to next file in arg list and sets the
1331 * time field for this file (or current date).
1333 * buf array to store proper date for the header.
1334 * dt if set skips the date processing (used with -m)
1336 static FILE *nxtfile(int argc
, char **argv
, const char **fname
, char *buf
, int dt
)
1340 struct tm
*timeptr
= NULL
;
1341 struct stat statbuf
;
1342 static int twice
= -1;
1345 if (eoptind
>= argc
) {
1347 * no file listed; default, use standard input
1356 *fname
= fnamedefault
;
1359 if ((tv_sec
= time(NULL
)) == -1) {
1361 (void)fprintf(err
, "pr: cannot get time of day, %s\n",
1366 timeptr
= localtime(&tv_sec
);
1368 for (; eoptind
< argc
; ++eoptind
) {
1369 if (strcmp(argv
[eoptind
], "-") == 0) {
1371 * process a "-" for filename
1378 *fname
= fnamedefault
;
1380 if (nohead
|| (dt
&& twice
))
1382 if ((tv_sec
= time(NULL
)) == -1) {
1385 "pr: cannot get time of day, %s\n",
1389 timeptr
= localtime(&tv_sec
);
1392 * normal file processing
1394 if ((inf
= fopen(argv
[eoptind
], "r")) == NULL
) {
1398 (void)fprintf(err
, "pr: cannot open %s, %s\n",
1399 argv
[eoptind
], strerror(errno
));
1405 *fname
= fnamedefault
;
1407 *fname
= argv
[eoptind
];
1409 if (nohead
|| (dt
&& twice
))
1413 if ((tv_sec
= time(NULL
)) == -1) {
1416 "pr: cannot get time of day, %s\n",
1421 timeptr
= localtime(&tv_sec
);
1423 if (fstat(fileno(inf
), &statbuf
) < 0) {
1427 "pr: cannot stat %s, %s\n",
1428 argv
[eoptind
], strerror(errno
));
1431 timeptr
= localtime(&(statbuf
.st_mtime
));
1440 * set up time field used in header
1442 if (strftime(buf
, HDBUF
, timefrmt
, timeptr
) <= 0) {
1446 (void)fputs("pr: time conversion failed\n", err
);
1453 * addnum(): adds the line number to the column
1454 * Truncates from the front or pads with spaces as required.
1455 * Numbers are right justified.
1457 * buf buffer to store the number
1458 * wdth width of buffer to fill
1461 * NOTE: numbers occupy part of the column. The posix
1462 * spec does not specify if -i processing should or should not
1463 * occur on number padding. The spec does say it occupies
1464 * part of the column. The usage of addnum currently treats
1465 * numbers as part of the column so spaces may be replaced.
1467 static void addnum(char *buf
, int wdth
, int line
)
1469 char *pt
= buf
+ wdth
;
1472 *--pt
= digs
[line
% 10];
1474 } while (line
&& (pt
> buf
));
1477 * pad with space as required
1484 * prhead(): prints the top of page header
1486 * buf buffer with time field (and offset)
1487 * cnt number of chars in buf
1488 * fname fname field for header
1489 * pagcnt page number
1491 static int prhead(char *buf
, const char *fname
, int pagcnt
)
1496 if ((putchar('\n') == EOF
) || (putchar('\n') == EOF
)) {
1501 * posix is not clear if the header is subject to line length
1502 * restrictions. The specification for header line format
1503 * in the spec clearly does not limit length. No pr currently
1504 * restricts header length. However if we need to truncate in
1505 * a reasonable way, adjust the length of the printf by
1506 * changing HDFMT to allow a length max as an argument to printf.
1507 * buf (which contains the offset spaces and time field could
1510 * note only the offset (if any) is processed for tab expansion
1512 if (offst
&& otln(buf
, offst
, &ips
, &ops
, -1))
1514 (void)printf(HDFMT
,buf
+offst
, fname
, pagcnt
);
1519 * prtail(): pad page with empty lines (if required) and print page trailer
1522 * cnt number of lines of padding needed
1523 * incomp was a '\n' missing from last line output
1525 static int prtail(int cnt
, int incomp
)
1529 * only pad with no headers when incomplete last line
1532 ((dspace
&& (putchar('\n') == EOF
)) ||
1533 (putchar('\n') == EOF
))) {
1538 * but honor the formfeed request
1541 if (putchar('\f') == EOF
) {
1549 * if double space output two \n
1555 * if an odd number of lines per page, add an extra \n
1564 if ((incomp
&& (putchar('\n') == EOF
)) ||
1565 (putchar('\f') == EOF
)) {
1572 while (--cnt
>= 0) {
1573 if (putchar('\n') == EOF
) {
1582 * terminate(): when a SIGINT is recvd
1584 static void terminate(int which_sig
)
1593 * flsh_errs(): output saved up diagnostic messages after all normal
1594 * processing has completed
1596 static void flsh_errs(void)
1600 (void)fflush(stdout
);
1605 while (fgets(buf
, BUFSIZ
, err
) != NULL
)
1606 (void)fputs(buf
, stderr
);
1609 static void mfail(void)
1611 (void)fputs("pr: memory allocation failed\n", err
);
1614 static void pfail(void)
1616 (void)fprintf(err
, "pr: write failure, %s\n", strerror(errno
));
1619 static void usage(void)
1622 "usage: pr [+page] [-col] [-adFfmprt] [-e[ch][gap]] [-h header]\n",
1625 " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",err
);
1627 " [-L locale] [-s[ch]] [-w width] [-] [file ...]\n", err
);
1631 * setup: Validate command args, initialize and perform sanity
1634 static int setup(int argc
, char *argv
[])
1644 if (isatty(fileno(stdout
))) {
1646 * defer diagnostics until processing is done
1648 if ((err
= tmpfile()) == NULL
) {
1650 (void)fputs("Cannot defer diagnostic messages\n",stderr
);
1655 while ((c
= egetopt(argc
, argv
, "#adFfmrte?h:i?L:l:n?o:ps?w:")) != -1) {
1658 if ((pgnm
= atoi(eoptarg
)) < 1) {
1659 (void)fputs("pr: +page number must be 1 or more\n",
1665 if ((clcnt
= atoi(eoptarg
)) < 1) {
1666 (void)fputs("pr: -columns must be 1 or more\n",err
);
1680 if ((eoptarg
!= NULL
) && !isdigit((unsigned char)*eoptarg
))
1681 inchar
= *eoptarg
++;
1684 if ((eoptarg
!= NULL
) && isdigit((unsigned char)*eoptarg
)) {
1685 if ((ingap
= atoi(eoptarg
)) < 0) {
1687 "pr: -e gap must be 0 or more\n", err
);
1692 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1694 "pr: invalid value for -e %s\n", eoptarg
);
1710 if ((eoptarg
!= NULL
) && !isdigit((unsigned char)*eoptarg
))
1714 if ((eoptarg
!= NULL
) && isdigit((unsigned char)*eoptarg
)) {
1715 if ((ogap
= atoi(eoptarg
)) < 0) {
1717 "pr: -i gap must be 0 or more\n", err
);
1722 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1724 "pr: invalid value for -i %s\n", eoptarg
);
1733 if (!isdigit((unsigned char)*eoptarg
) || ((lines
=atoi(eoptarg
)) < 1)) {
1735 "pr: number of lines must be 1 or more\n",err
);
1743 if ((eoptarg
!= NULL
) && !isdigit((unsigned char)*eoptarg
))
1744 nmchar
= *eoptarg
++;
1747 if ((eoptarg
!= NULL
) && isdigit((unsigned char)*eoptarg
)) {
1748 if ((nmwd
= atoi(eoptarg
)) < 1) {
1750 "pr: -n width must be 1 or more\n",err
);
1753 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1755 "pr: invalid value for -n %s\n", eoptarg
);
1761 if (!isdigit((unsigned char)*eoptarg
) || ((offst
= atoi(eoptarg
))< 1)){
1762 (void)fputs("pr: -o offset must be 1 or more\n",
1775 if (eoptarg
== NULL
)
1779 if (*eoptarg
!= '\0') {
1781 "pr: invalid value for -s %s\n",
1792 if ((eoptarg
== NULL
) ||
1793 !isdigit((unsigned char)*eoptarg
) ||
1794 ((pgwd
= atoi(eoptarg
)) < 1)){
1796 "pr: -w width must be 1 or more \n",err
);
1807 * default and sanity checks
1811 if ((clcnt
= argc
- eoptind
) <= 1) {
1820 (void)fputs("pr: -a flag requires multiple columns\n",
1825 (void)fputs("pr: -m cannot be used with -a\n", err
);
1835 if (cflag
|| merge
) {
1848 "pr: -m cannot be used with multiple columns\n", err
);
1852 colwd
= (pgwd
+ 1 - (clcnt
* (nmwd
+ 2)))/clcnt
;
1853 pgwd
= ((colwd
+ nmwd
+ 2) * clcnt
) - 1;
1855 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
1856 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
1860 "pr: page width is too small for %d columns\n",clcnt
);
1868 * make sure long enough for headers. if not disable
1870 if (lines
<= HEADLEN
+ TAILLEN
)
1873 lines
-= HEADLEN
+ TAILLEN
;
1876 * adjust for double space on odd length pages
1888 (void) setlocale(LC_TIME
, (Lflag
!= NULL
) ? Lflag
: "");
1890 #if 0 /* FIXME : figure out how to do this portably */
1891 d_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
1892 timefrmt
= strdup(d_first
? TIMEFMTD
: TIMEFMTM
);
1894 timefrmt
= strdup(TIMEFMTM
);
1899 /* embedded contents of egetopt.c START */
1901 int eopterr
= 1; /* if error message should be printed */
1902 int eoptind
= 1; /* index into parent argv vector */
1903 int eoptopt
; /* character checked for validity */
1904 char *eoptarg
; /* argument associated with option */
1906 #define BADCH (int)'?'
1908 static char emsg
[] = "";
1910 static int egetopt(int nargc
, char * const *nargv
, const char *ostr
)
1912 static char *place
= emsg
; /* option letter processing */
1913 char *oli
; /* option letter list index */
1914 static int delim
; /* which option delimiter */
1916 static char savec
= '\0';
1918 if (savec
!= '\0') {
1925 * update scanning pointer
1927 if ((eoptind
>= nargc
) ||
1928 ((*(place
= nargv
[eoptind
]) != '-') && (*place
!= '+'))) {
1933 delim
= (int)*place
;
1934 if (place
[1] && *++place
== '-' && !place
[1]) {
1945 * check option letter
1947 if ((eoptopt
= (int)*place
++) == (int)':' || (eoptopt
== (int)'?') ||
1948 !(oli
= strchr(ostr
, eoptopt
))) {
1950 * if the user didn't specify '-' as an option,
1951 * assume it means -1 when by itself.
1953 if ((eoptopt
== (int)'-') && !*place
)
1955 if (strchr(ostr
, '#') && (isdigit(eoptopt
) ||
1956 (((eoptopt
== (int)'-') || (eoptopt
== (int)'+')) &&
1957 isdigit(*place
)))) {
1959 * # option: +/- with a number is ok
1961 for (p
= place
; *p
!= '\0'; ++p
) {
1981 if (!(p
= strrchr(*nargv
, '/')))
1985 (void)fprintf(stderr
, "%s: illegal option -- %c\n",
1990 if (delim
== (int)'+') {
1992 * '+' is only allowed with numbers
1997 if (!(p
= strrchr(*nargv
, '/')))
2001 (void)fprintf(stderr
,
2002 "%s: illegal '+' delimiter with option -- %c\n",
2008 if ((*oli
!= ':') && (*oli
!= '?')) {
2010 * don't need argument
2023 } else if (*oli
== '?') {
2025 * no arg, but NOT required
2028 } else if (nargc
<= ++eoptind
) {
2030 * no arg, but IS required
2034 if (!(p
= strrchr(*nargv
, '/')))
2038 (void)fprintf(stderr
,
2039 "%s: option requires an argument -- %c\n", p
,
2045 * arg has white space
2047 eoptarg
= nargv
[eoptind
];
2054 /* embedded contents of egetopt.c END */