xref: /freebsd/usr.bin/pr/pr.c (revision d68e2acd952631472ec8c60ec409be20af5e207f)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1991 Keith Muller.
39b50d902SRodney W. Grimes  * Copyright (c) 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  *
69b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
79b50d902SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
36b5076d91SAndrey A. Chernov  *
37b5076d91SAndrey A. Chernov  * $FreeBSD$
389b50d902SRodney W. Grimes  */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
419b50d902SRodney W. Grimes static char copyright[] =
429b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\
439b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
449b50d902SRodney W. Grimes #endif /* not lint */
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #ifndef lint
479b50d902SRodney W. Grimes static char sccsid[] = "@(#)pr.c	8.2 (Berkeley) 4/16/94";
489b50d902SRodney W. Grimes #endif /* not lint */
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <sys/types.h>
519b50d902SRodney W. Grimes #include <sys/time.h>
529b50d902SRodney W. Grimes #include <sys/stat.h>
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes #include <ctype.h>
559b50d902SRodney W. Grimes #include <errno.h>
56b5076d91SAndrey A. Chernov #include <locale.h>
579b50d902SRodney W. Grimes #include <signal.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
619b50d902SRodney W. Grimes #include <unistd.h>
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes #include "pr.h"
649b50d902SRodney W. Grimes #include "extern.h"
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes /*
679b50d902SRodney W. Grimes  * pr:	a printing and pagination filter. If multiple input files
689b50d902SRodney W. Grimes  *	are specified, each is read, formatted, and written to standard
699b50d902SRodney W. Grimes  *	output. By default, input is seperated into 66-line pages, each
709b50d902SRodney W. Grimes  *	with a header that includes the page number, date, time and the
719b50d902SRodney W. Grimes  *	files pathname.
729b50d902SRodney W. Grimes  *
739b50d902SRodney W. Grimes  *	Complies with posix P1003.2/D11
749b50d902SRodney W. Grimes  */
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes /*
779b50d902SRodney W. Grimes  * parameter variables
789b50d902SRodney W. Grimes  */
799b50d902SRodney W. Grimes int	pgnm;			/* starting page number */
809b50d902SRodney W. Grimes int	clcnt;			/* number of columns */
819b50d902SRodney W. Grimes int	colwd;			/* column data width - multiple columns */
829b50d902SRodney W. Grimes int	across;			/* mult col flag; write across page */
839b50d902SRodney W. Grimes int	dspace;			/* double space flag */
849b50d902SRodney W. Grimes char	inchar;			/* expand input char */
859b50d902SRodney W. Grimes int	ingap;			/* expand input gap */
869b50d902SRodney W. Grimes int	formfeed;		/* use formfeed as trailer */
879b50d902SRodney W. Grimes char	*header;		/* header name instead of file name */
889b50d902SRodney W. Grimes char	ochar;			/* contract output char */
899b50d902SRodney W. Grimes int	ogap;			/* contract output gap */
909b50d902SRodney W. Grimes int	lines;			/* number of lines per page */
919b50d902SRodney W. Grimes int	merge;			/* merge multiple files in output */
929b50d902SRodney W. Grimes char	nmchar;			/* line numbering append char */
939b50d902SRodney W. Grimes int	nmwd;			/* width of line number field */
949b50d902SRodney W. Grimes int	offst;			/* number of page offset spaces */
959b50d902SRodney W. Grimes int	nodiag;			/* do not report file open errors */
969b50d902SRodney W. Grimes char	schar;			/* text column separation character */
979b50d902SRodney W. Grimes int	sflag;			/* -s option for multiple columns */
989b50d902SRodney W. Grimes int	nohead;			/* do not write head and trailer */
999b50d902SRodney W. Grimes int	pgwd;			/* page width with multiple col output */
1009b50d902SRodney W. Grimes char	*timefrmt;		/* time conversion string */
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes /*
1039b50d902SRodney W. Grimes  * misc globals
1049b50d902SRodney W. Grimes  */
1059b50d902SRodney W. Grimes FILE	*err;			/* error message file pointer */
1069b50d902SRodney W. Grimes int	addone;			/* page length is odd with double space */
1079b50d902SRodney W. Grimes int	errcnt;			/* error count on file processing */
1089b50d902SRodney W. Grimes char	digs[] = "0123456789";	/* page number translation map */
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes int
1119b50d902SRodney W. Grimes main(argc, argv)
1129b50d902SRodney W. Grimes         int argc;
1139b50d902SRodney W. Grimes         char *argv[];
1149b50d902SRodney W. Grimes {
1159b50d902SRodney W. Grimes 	int ret_val;
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1189b50d902SRodney W. Grimes 		(void)signal(SIGINT, terminate);
1199b50d902SRodney W. Grimes 	ret_val = setup(argc, argv);
1209b50d902SRodney W. Grimes 	if (!ret_val) {
1219b50d902SRodney W. Grimes 		/*
1229b50d902SRodney W. Grimes 		 * select the output format based on options
1239b50d902SRodney W. Grimes 		 */
1249b50d902SRodney W. Grimes 		if (merge)
1259b50d902SRodney W. Grimes 			ret_val = mulfile(argc, argv);
1269b50d902SRodney W. Grimes 		else if (clcnt == 1)
1279b50d902SRodney W. Grimes 			ret_val = onecol(argc, argv);
1289b50d902SRodney W. Grimes 		else if (across)
1299b50d902SRodney W. Grimes 			ret_val = horzcol(argc, argv);
1309b50d902SRodney W. Grimes 		else
1319b50d902SRodney W. Grimes 			ret_val = vertcol(argc, argv);
1329b50d902SRodney W. Grimes 	} else
1339b50d902SRodney W. Grimes 		usage();
1349b50d902SRodney W. Grimes 	flsh_errs();
1359b50d902SRodney W. Grimes 	if (errcnt || ret_val)
1369b50d902SRodney W. Grimes 		exit(1);
1379b50d902SRodney W. Grimes 	return(0);
1389b50d902SRodney W. Grimes }
1399b50d902SRodney W. Grimes 
1409b50d902SRodney W. Grimes /*
1419b50d902SRodney W. Grimes  * onecol:	print files with only one column of output.
1429b50d902SRodney W. Grimes  *		Line length is unlimited.
1439b50d902SRodney W. Grimes  */
1449b50d902SRodney W. Grimes int
1459b50d902SRodney W. Grimes onecol(argc, argv)
1469b50d902SRodney W. Grimes         int argc;
1479b50d902SRodney W. Grimes         char *argv[];
1489b50d902SRodney W. Grimes {
1499b50d902SRodney W. Grimes 	register int cnt = -1;
1509b50d902SRodney W. Grimes 	register int off;
1519b50d902SRodney W. Grimes 	register int lrgln;
1529b50d902SRodney W. Grimes 	register int linecnt;
1539b50d902SRodney W. Grimes 	register int num;
1549b50d902SRodney W. Grimes 	int lncnt;
1559b50d902SRodney W. Grimes 	int pagecnt;
1569b50d902SRodney W. Grimes 	int ips;
1579b50d902SRodney W. Grimes 	int ops;
1589b50d902SRodney W. Grimes 	int cps;
1599b50d902SRodney W. Grimes 	char *obuf;
1609b50d902SRodney W. Grimes 	char *lbuf;
1619b50d902SRodney W. Grimes 	char *nbuf;
1629b50d902SRodney W. Grimes 	char *hbuf;
1639b50d902SRodney W. Grimes 	char *ohbuf;
1649b50d902SRodney W. Grimes 	FILE *inf;
1659b50d902SRodney W. Grimes 	char *fname;
1669b50d902SRodney W. Grimes 	int mor;
1679b50d902SRodney W. Grimes 
1689b50d902SRodney W. Grimes 	if (nmwd)
1699b50d902SRodney W. Grimes 		num = nmwd + 1;
1709b50d902SRodney W. Grimes 	else
1719b50d902SRodney W. Grimes 		num = 0;
1729b50d902SRodney W. Grimes 	off = num + offst;
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes 	/*
1759b50d902SRodney W. Grimes 	 * allocate line buffer
1769b50d902SRodney W. Grimes 	 */
1779b50d902SRodney W. Grimes 	if ((obuf = malloc((unsigned)(LBUF + off)*sizeof(char))) == NULL) {
1789b50d902SRodney W. Grimes 		mfail();
1799b50d902SRodney W. Grimes 		return(1);
1809b50d902SRodney W. Grimes 	}
1819b50d902SRodney W. Grimes 	/*
1829b50d902SRodney W. Grimes 	 * allocate header buffer
1839b50d902SRodney W. Grimes 	 */
1849b50d902SRodney W. Grimes 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
1859b50d902SRodney W. Grimes 		mfail();
1869b50d902SRodney W. Grimes 		return(1);
1879b50d902SRodney W. Grimes 	}
1889b50d902SRodney W. Grimes 
1899b50d902SRodney W. Grimes 	ohbuf = hbuf + offst;
1909b50d902SRodney W. Grimes 	nbuf = obuf + offst;
1919b50d902SRodney W. Grimes 	lbuf = nbuf + num;
1929b50d902SRodney W. Grimes 	if (num)
1939b50d902SRodney W. Grimes 		nbuf[--num] = nmchar;
1949b50d902SRodney W. Grimes 	if (offst) {
1959b50d902SRodney W. Grimes 		(void)memset(obuf, (int)' ', offst);
1969b50d902SRodney W. Grimes 		(void)memset(hbuf, (int)' ', offst);
1979b50d902SRodney W. Grimes 	}
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 	/*
2009b50d902SRodney W. Grimes 	 * loop by file
2019b50d902SRodney W. Grimes 	 */
2029b50d902SRodney W. Grimes 	while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
2039b50d902SRodney W. Grimes 		if (pgnm) {
2049b50d902SRodney W. Grimes 			/*
2059b50d902SRodney W. Grimes 			 * skip to specified page
2069b50d902SRodney W. Grimes 			 */
2079b50d902SRodney W. Grimes 			if (inskip(inf, pgnm, lines))
2089b50d902SRodney W. Grimes 				continue;
2099b50d902SRodney W. Grimes 			pagecnt = pgnm;
2109b50d902SRodney W. Grimes 		} else
2119b50d902SRodney W. Grimes 			pagecnt = 1;
2129b50d902SRodney W. Grimes 		lncnt = 0;
2139b50d902SRodney W. Grimes 
2149b50d902SRodney W. Grimes 		/*
2159b50d902SRodney W. Grimes 		 * loop by page
2169b50d902SRodney W. Grimes 		 */
2179b50d902SRodney W. Grimes 		for(;;) {
2189b50d902SRodney W. Grimes 			linecnt = 0;
2199b50d902SRodney W. Grimes 			lrgln = 0;
2209b50d902SRodney W. Grimes 			ops = 0;
2219b50d902SRodney W. Grimes 			ips = 0;
2229b50d902SRodney W. Grimes 			cps = 0;
2239b50d902SRodney W. Grimes 
2249b50d902SRodney W. Grimes 			/*
2259b50d902SRodney W. Grimes 			 * loop by line
2269b50d902SRodney W. Grimes 			 */
2279b50d902SRodney W. Grimes 			while (linecnt < lines) {
2289b50d902SRodney W. Grimes 				/*
2299b50d902SRodney W. Grimes 				 * input next line
2309b50d902SRodney W. Grimes 				 */
2319b50d902SRodney W. Grimes 				if ((cnt = inln(inf,lbuf,LBUF,&cps,0,&mor)) < 0)
2329b50d902SRodney W. Grimes 					break;
2339b50d902SRodney W. Grimes 				if (!linecnt && !nohead &&
2349b50d902SRodney W. Grimes 					prhead(hbuf, fname, pagecnt))
2359b50d902SRodney W. Grimes 					return(1);
2369b50d902SRodney W. Grimes 
2379b50d902SRodney W. Grimes 				/*
2389b50d902SRodney W. Grimes 				 * start of new line.
2399b50d902SRodney W. Grimes 				 */
2409b50d902SRodney W. Grimes 				if (!lrgln) {
2419b50d902SRodney W. Grimes 					if (num)
2429b50d902SRodney W. Grimes 						addnum(nbuf, num, ++lncnt);
2439b50d902SRodney W. Grimes 					if (otln(obuf,cnt+off, &ips, &ops, mor))
2449b50d902SRodney W. Grimes 						return(1);
2459b50d902SRodney W. Grimes 				} else if (otln(lbuf, cnt, &ips, &ops, mor))
2469b50d902SRodney W. Grimes 					return(1);
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 				/*
2499b50d902SRodney W. Grimes 				 * if line bigger than buffer, get more
2509b50d902SRodney W. Grimes 				 */
2519b50d902SRodney W. Grimes 				if (mor) {
2529b50d902SRodney W. Grimes 					lrgln = 1;
2539b50d902SRodney W. Grimes 					continue;
2549b50d902SRodney W. Grimes 				}
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes 				/*
2579b50d902SRodney W. Grimes 				 * whole line rcvd. reset tab proc. state
2589b50d902SRodney W. Grimes 				 */
2599b50d902SRodney W. Grimes 				++linecnt;
2609b50d902SRodney W. Grimes 				lrgln = 0;
2619b50d902SRodney W. Grimes 				ops = 0;
2629b50d902SRodney W. Grimes 				ips = 0;
2639b50d902SRodney W. Grimes 			}
2649b50d902SRodney W. Grimes 
2659b50d902SRodney W. Grimes 			/*
2669b50d902SRodney W. Grimes 			 * fill to end of page
2679b50d902SRodney W. Grimes 			 */
2689b50d902SRodney W. Grimes 			if (linecnt && prtail(lines-linecnt-lrgln, lrgln))
2699b50d902SRodney W. Grimes 				return(1);
2709b50d902SRodney W. Grimes 
2719b50d902SRodney W. Grimes 			/*
2729b50d902SRodney W. Grimes 			 * On EOF go to next file
2739b50d902SRodney W. Grimes 			 */
2749b50d902SRodney W. Grimes 			if (cnt < 0)
2759b50d902SRodney W. Grimes 				break;
2769b50d902SRodney W. Grimes 			++pagecnt;
2779b50d902SRodney W. Grimes 		}
2789b50d902SRodney W. Grimes 		if (inf != stdin)
2799b50d902SRodney W. Grimes 			(void)fclose(inf);
2809b50d902SRodney W. Grimes 	}
2819b50d902SRodney W. Grimes 	if (eoptind < argc)
2829b50d902SRodney W. Grimes 		return(1);
2839b50d902SRodney W. Grimes 	return(0);
2849b50d902SRodney W. Grimes }
2859b50d902SRodney W. Grimes 
2869b50d902SRodney W. Grimes /*
2879b50d902SRodney W. Grimes  * vertcol:	print files with more than one column of output down a page
2889b50d902SRodney W. Grimes  */
2899b50d902SRodney W. Grimes int
2909b50d902SRodney W. Grimes vertcol(argc, argv)
2919b50d902SRodney W. Grimes         int argc;
2929b50d902SRodney W. Grimes         char *argv[];
2939b50d902SRodney W. Grimes {
2949b50d902SRodney W. Grimes 	register char *ptbf;
2959b50d902SRodney W. Grimes 	register char **lstdat;
2969b50d902SRodney W. Grimes 	register int i;
2979b50d902SRodney W. Grimes 	register int j;
2989b50d902SRodney W. Grimes 	register int cnt = -1;
2999b50d902SRodney W. Grimes 	register int pln;
3009b50d902SRodney W. Grimes 	register int *indy;
3019b50d902SRodney W. Grimes 	int cvc;
3029b50d902SRodney W. Grimes 	int *lindy;
3039b50d902SRodney W. Grimes 	int lncnt;
3049b50d902SRodney W. Grimes 	int stp;
3059b50d902SRodney W. Grimes 	int pagecnt;
3069b50d902SRodney W. Grimes 	int col = colwd + 1;
3079b50d902SRodney W. Grimes 	int mxlen = pgwd + offst + 1;
3089b50d902SRodney W. Grimes 	int mclcnt = clcnt - 1;
3099b50d902SRodney W. Grimes 	struct vcol *vc;
3109b50d902SRodney W. Grimes 	int mvc;
3119b50d902SRodney W. Grimes 	int tvc;
3129b50d902SRodney W. Grimes 	int cw = nmwd + 1;
3139b50d902SRodney W. Grimes 	int fullcol;
3149b50d902SRodney W. Grimes 	char *buf;
3159b50d902SRodney W. Grimes 	char *hbuf;
3169b50d902SRodney W. Grimes 	char *ohbuf;
3179b50d902SRodney W. Grimes 	char *fname;
3189b50d902SRodney W. Grimes 	FILE *inf;
3199b50d902SRodney W. Grimes 	int ips = 0;
3209b50d902SRodney W. Grimes 	int cps = 0;
3219b50d902SRodney W. Grimes 	int ops = 0;
3229b50d902SRodney W. Grimes 	int mor = 0;
3239b50d902SRodney W. Grimes 
3249b50d902SRodney W. Grimes 	/*
3259b50d902SRodney W. Grimes 	 * allocate page buffer
3269b50d902SRodney W. Grimes 	 */
3279b50d902SRodney W. Grimes 	if ((buf = malloc((unsigned)lines*mxlen*sizeof(char))) == NULL) {
3289b50d902SRodney W. Grimes 		mfail();
3299b50d902SRodney W. Grimes 		return(1);
3309b50d902SRodney W. Grimes 	}
3319b50d902SRodney W. Grimes 
3329b50d902SRodney W. Grimes 	/*
3339b50d902SRodney W. Grimes 	 * allocate page header
3349b50d902SRodney W. Grimes 	 */
3359b50d902SRodney W. Grimes 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
3369b50d902SRodney W. Grimes 		mfail();
3379b50d902SRodney W. Grimes 		return(1);
3389b50d902SRodney W. Grimes 	}
3399b50d902SRodney W. Grimes 	ohbuf = hbuf + offst;
3409b50d902SRodney W. Grimes 	if (offst)
3419b50d902SRodney W. Grimes 		(void)memset(hbuf, (int)' ', offst);
3429b50d902SRodney W. Grimes 
3439b50d902SRodney W. Grimes 	/*
3449b50d902SRodney W. Grimes 	 * col pointers when no headers
3459b50d902SRodney W. Grimes 	 */
3469b50d902SRodney W. Grimes 	mvc = lines * clcnt;
3479b50d902SRodney W. Grimes 	if ((vc =
3489b50d902SRodney W. Grimes 	    (struct vcol *)malloc((unsigned)mvc*sizeof(struct vcol))) == NULL) {
3499b50d902SRodney W. Grimes 		mfail();
3509b50d902SRodney W. Grimes 		return(1);
3519b50d902SRodney W. Grimes 	}
3529b50d902SRodney W. Grimes 
3539b50d902SRodney W. Grimes 	/*
3549b50d902SRodney W. Grimes 	 * pointer into page where last data per line is located
3559b50d902SRodney W. Grimes 	 */
3569b50d902SRodney W. Grimes 	if ((lstdat = (char **)malloc((unsigned)lines*sizeof(char *))) == NULL){
3579b50d902SRodney W. Grimes 		mfail();
3589b50d902SRodney W. Grimes 		return(1);
3599b50d902SRodney W. Grimes 	}
3609b50d902SRodney W. Grimes 
3619b50d902SRodney W. Grimes 	/*
3629b50d902SRodney W. Grimes 	 * fast index lookups to locate start of lines
3639b50d902SRodney W. Grimes 	 */
3649b50d902SRodney W. Grimes 	if ((indy = (int *)malloc((unsigned)lines*sizeof(int))) == NULL) {
3659b50d902SRodney W. Grimes 		mfail();
3669b50d902SRodney W. Grimes 		return(1);
3679b50d902SRodney W. Grimes 	}
3689b50d902SRodney W. Grimes 	if ((lindy = (int *)malloc((unsigned)lines*sizeof(int))) == NULL) {
3699b50d902SRodney W. Grimes 		mfail();
3709b50d902SRodney W. Grimes 		return(1);
3719b50d902SRodney W. Grimes 	}
3729b50d902SRodney W. Grimes 
3739b50d902SRodney W. Grimes 	if (nmwd)
3749b50d902SRodney W. Grimes 		fullcol = col + cw;
3759b50d902SRodney W. Grimes 	else
3769b50d902SRodney W. Grimes 		fullcol = col;
3779b50d902SRodney W. Grimes 
3789b50d902SRodney W. Grimes 	/*
3799b50d902SRodney W. Grimes 	 * initialize buffer lookup indexes and offset area
3809b50d902SRodney W. Grimes 	 */
3819b50d902SRodney W. Grimes 	for (j = 0; j < lines; ++j) {
3829b50d902SRodney W. Grimes 		lindy[j] = j * mxlen;
3839b50d902SRodney W. Grimes 		indy[j] = lindy[j] + offst;
3849b50d902SRodney W. Grimes 		if (offst) {
3859b50d902SRodney W. Grimes 			ptbf = buf + lindy[j];
3869b50d902SRodney W. Grimes 			(void)memset(ptbf, (int)' ', offst);
3879b50d902SRodney W. Grimes 			ptbf += offst;
3889b50d902SRodney W. Grimes 		} else
3899b50d902SRodney W. Grimes 			ptbf = buf + indy[j];
3909b50d902SRodney W. Grimes 		lstdat[j] = ptbf;
3919b50d902SRodney W. Grimes 	}
3929b50d902SRodney W. Grimes 
3939b50d902SRodney W. Grimes 	/*
3949b50d902SRodney W. Grimes 	 * loop by file
3959b50d902SRodney W. Grimes 	 */
3969b50d902SRodney W. Grimes 	while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
3979b50d902SRodney W. Grimes 		if (pgnm) {
3989b50d902SRodney W. Grimes 			/*
3999b50d902SRodney W. Grimes 			 * skip to requested page
4009b50d902SRodney W. Grimes 			 */
4019b50d902SRodney W. Grimes 			if (inskip(inf, pgnm, lines))
4029b50d902SRodney W. Grimes 				continue;
4039b50d902SRodney W. Grimes 			pagecnt = pgnm;
4049b50d902SRodney W. Grimes 		} else
4059b50d902SRodney W. Grimes 			pagecnt = 1;
4069b50d902SRodney W. Grimes 		lncnt = 0;
4079b50d902SRodney W. Grimes 
4089b50d902SRodney W. Grimes 		/*
4099b50d902SRodney W. Grimes 		 * loop by page
4109b50d902SRodney W. Grimes 		 */
4119b50d902SRodney W. Grimes 		for(;;) {
4129b50d902SRodney W. Grimes 			/*
4139b50d902SRodney W. Grimes 			 * loop by column
4149b50d902SRodney W. Grimes 			 */
4159b50d902SRodney W. Grimes 			cvc = 0;
4169b50d902SRodney W. Grimes 			for (i = 0; i < clcnt; ++i) {
4179b50d902SRodney W. Grimes 				j = 0;
4189b50d902SRodney W. Grimes 				/*
4199b50d902SRodney W. Grimes 				 * if last column, do not pad
4209b50d902SRodney W. Grimes 				 */
4219b50d902SRodney W. Grimes 				if (i == mclcnt)
4229b50d902SRodney W. Grimes 					stp = 1;
4239b50d902SRodney W. Grimes 				else
4249b50d902SRodney W. Grimes 					stp = 0;
4259b50d902SRodney W. Grimes 				/*
4269b50d902SRodney W. Grimes 				 * loop by line
4279b50d902SRodney W. Grimes 				 */
4289b50d902SRodney W. Grimes 				for(;;) {
4299b50d902SRodney W. Grimes 					/*
4309b50d902SRodney W. Grimes 					 * is this first column
4319b50d902SRodney W. Grimes 					 */
4329b50d902SRodney W. Grimes 					if (!i) {
4339b50d902SRodney W. Grimes 						ptbf = buf + indy[j];
4349b50d902SRodney W. Grimes 						lstdat[j] = ptbf;
4359b50d902SRodney W. Grimes 					} else
4369b50d902SRodney W. Grimes 						ptbf = lstdat[j];
4379b50d902SRodney W. Grimes 					vc[cvc].pt = ptbf;
4389b50d902SRodney W. Grimes 
4399b50d902SRodney W. Grimes 					/*
4409b50d902SRodney W. Grimes 					 * add number
4419b50d902SRodney W. Grimes 					 */
4429b50d902SRodney W. Grimes 					if (nmwd) {
4439b50d902SRodney W. Grimes 						addnum(ptbf, nmwd, ++lncnt);
4449b50d902SRodney W. Grimes 						ptbf += nmwd;
4459b50d902SRodney W. Grimes 						*ptbf++ = nmchar;
4469b50d902SRodney W. Grimes 					}
4479b50d902SRodney W. Grimes 
4489b50d902SRodney W. Grimes 					/*
4499b50d902SRodney W. Grimes 					 * input next line
4509b50d902SRodney W. Grimes 					 */
4519b50d902SRodney W. Grimes 					cnt = inln(inf,ptbf,colwd,&cps,1,&mor);
4529b50d902SRodney W. Grimes 					vc[cvc++].cnt = cnt;
4539b50d902SRodney W. Grimes 					if (cnt < 0)
4549b50d902SRodney W. Grimes 						break;
4559b50d902SRodney W. Grimes 					ptbf += cnt;
4569b50d902SRodney W. Grimes 
4579b50d902SRodney W. Grimes 					/*
4589b50d902SRodney W. Grimes 					 * pad all but last column on page
4599b50d902SRodney W. Grimes 					 */
4609b50d902SRodney W. Grimes 					if (!stp) {
4619b50d902SRodney W. Grimes 						/*
4629b50d902SRodney W. Grimes 						 * pad to end of column
4639b50d902SRodney W. Grimes 						 */
4649b50d902SRodney W. Grimes 						if (sflag)
4659b50d902SRodney W. Grimes 							*ptbf++ = schar;
4669b50d902SRodney W. Grimes 						else if ((pln = col-cnt) > 0) {
4679b50d902SRodney W. Grimes 							(void)memset(ptbf,
4689b50d902SRodney W. Grimes 								(int)' ',pln);
4699b50d902SRodney W. Grimes 							ptbf += pln;
4709b50d902SRodney W. Grimes 						}
4719b50d902SRodney W. Grimes 					}
4729b50d902SRodney W. Grimes 					/*
4739b50d902SRodney W. Grimes 					 * remember last char in line
4749b50d902SRodney W. Grimes 					 */
4759b50d902SRodney W. Grimes 					lstdat[j] = ptbf;
4769b50d902SRodney W. Grimes 					if (++j >= lines)
4779b50d902SRodney W. Grimes 						break;
4789b50d902SRodney W. Grimes 				}
4799b50d902SRodney W. Grimes 				if (cnt < 0)
4809b50d902SRodney W. Grimes 					break;
4819b50d902SRodney W. Grimes 			}
4829b50d902SRodney W. Grimes 
4839b50d902SRodney W. Grimes 			/*
4849b50d902SRodney W. Grimes 			 * when -t (no header) is specified the spec requires
4859b50d902SRodney W. Grimes 			 * the min number of lines. The last page may not have
4869b50d902SRodney W. Grimes 			 * balanced length columns. To fix this we must reorder
4879b50d902SRodney W. Grimes 			 * the columns. This is a very slow technique so it is
4889b50d902SRodney W. Grimes 			 * only used under limited conditions. Without -t, the
4899b50d902SRodney W. Grimes 			 * balancing of text columns is unspecified. To NOT
4909b50d902SRodney W. Grimes 			 * balance the last page, add the global variable
4919b50d902SRodney W. Grimes 			 * nohead to the if statement below e.g.
4929b50d902SRodney W. Grimes 			 *
4939b50d902SRodney W. Grimes 			 * if ((cnt < 0) && nohead && cvc ......
4949b50d902SRodney W. Grimes 			 */
4959b50d902SRodney W. Grimes 			--cvc;
4969b50d902SRodney W. Grimes 
4979b50d902SRodney W. Grimes 			/*
4989b50d902SRodney W. Grimes 			 * check to see if last page needs to be reordered
4999b50d902SRodney W. Grimes 			 */
5009b50d902SRodney W. Grimes 			if ((cnt < 0) && cvc && ((mvc-cvc) >= clcnt)){
5019b50d902SRodney W. Grimes 				pln = cvc/clcnt;
5029b50d902SRodney W. Grimes 				if (cvc % clcnt)
5039b50d902SRodney W. Grimes 					++pln;
5049b50d902SRodney W. Grimes 
5059b50d902SRodney W. Grimes 				/*
5069b50d902SRodney W. Grimes 				 * print header
5079b50d902SRodney W. Grimes 				 */
5089b50d902SRodney W. Grimes 				if (!nohead && prhead(hbuf, fname, pagecnt))
5099b50d902SRodney W. Grimes 					return(1);
5109b50d902SRodney W. Grimes 				for (i = 0; i < pln; ++i) {
5119b50d902SRodney W. Grimes 					ips = 0;
5129b50d902SRodney W. Grimes 					ops = 0;
5139b50d902SRodney W. Grimes 					if (offst&& otln(buf,offst,&ips,&ops,1))
5149b50d902SRodney W. Grimes 						return(1);
5159b50d902SRodney W. Grimes 					tvc = i;
5169b50d902SRodney W. Grimes 
5179b50d902SRodney W. Grimes 					for (j = 0; j < clcnt; ++j) {
5189b50d902SRodney W. Grimes 						/*
5199b50d902SRodney W. Grimes 						 * determine column length
5209b50d902SRodney W. Grimes 						 */
5219b50d902SRodney W. Grimes 						if (j == mclcnt) {
5229b50d902SRodney W. Grimes 							/*
5239b50d902SRodney W. Grimes 							 * last column
5249b50d902SRodney W. Grimes 							 */
5259b50d902SRodney W. Grimes 							cnt = vc[tvc].cnt;
5269b50d902SRodney W. Grimes 							if (nmwd)
5279b50d902SRodney W. Grimes 								cnt += cw;
5289b50d902SRodney W. Grimes 						} else if (sflag) {
5299b50d902SRodney W. Grimes 							/*
5309b50d902SRodney W. Grimes 							 * single ch between
5319b50d902SRodney W. Grimes 							 */
5329b50d902SRodney W. Grimes 							cnt = vc[tvc].cnt + 1;
5339b50d902SRodney W. Grimes 							if (nmwd)
5349b50d902SRodney W. Grimes 								cnt += cw;
5359b50d902SRodney W. Grimes 						} else
5369b50d902SRodney W. Grimes 							cnt = fullcol;
5379b50d902SRodney W. Grimes 						if (otln(vc[tvc].pt, cnt, &ips,
5389b50d902SRodney W. Grimes 								&ops, 1))
5399b50d902SRodney W. Grimes 							return(1);
5409b50d902SRodney W. Grimes 						tvc += pln;
5419b50d902SRodney W. Grimes 						if (tvc >= cvc)
5429b50d902SRodney W. Grimes 							break;
5439b50d902SRodney W. Grimes 					}
5449b50d902SRodney W. Grimes 					/*
5459b50d902SRodney W. Grimes 					 * terminate line
5469b50d902SRodney W. Grimes 					 */
5479b50d902SRodney W. Grimes 					if (otln(buf, 0, &ips, &ops, 0))
5489b50d902SRodney W. Grimes 						return(1);
5499b50d902SRodney W. Grimes 				}
5509b50d902SRodney W. Grimes 				/*
5519b50d902SRodney W. Grimes 				 * pad to end of page
5529b50d902SRodney W. Grimes 				 */
5539b50d902SRodney W. Grimes 				if (prtail((lines - pln), 0))
5549b50d902SRodney W. Grimes 					return(1);
5559b50d902SRodney W. Grimes 				/*
5569b50d902SRodney W. Grimes 				 * done with output, go to next file
5579b50d902SRodney W. Grimes 				 */
5589b50d902SRodney W. Grimes 				break;
5599b50d902SRodney W. Grimes 			}
5609b50d902SRodney W. Grimes 
5619b50d902SRodney W. Grimes 			/*
5629b50d902SRodney W. Grimes 			 * determine how many lines to output
5639b50d902SRodney W. Grimes 			 */
5649b50d902SRodney W. Grimes 			if (i > 0)
5659b50d902SRodney W. Grimes 				pln = lines;
5669b50d902SRodney W. Grimes 			else
5679b50d902SRodney W. Grimes 				pln = j;
5689b50d902SRodney W. Grimes 
5699b50d902SRodney W. Grimes 			/*
5709b50d902SRodney W. Grimes 			 * print header
5719b50d902SRodney W. Grimes 			 */
5729b50d902SRodney W. Grimes 			if (pln && !nohead && prhead(hbuf, fname, pagecnt))
5739b50d902SRodney W. Grimes 				return(1);
5749b50d902SRodney W. Grimes 
5759b50d902SRodney W. Grimes 			/*
5769b50d902SRodney W. Grimes 			 * output each line
5779b50d902SRodney W. Grimes 			 */
5789b50d902SRodney W. Grimes 			for (i = 0; i < pln; ++i) {
5799b50d902SRodney W. Grimes 				ptbf = buf + lindy[i];
5809b50d902SRodney W. Grimes 				if ((j = lstdat[i] - ptbf) <= offst)
5819b50d902SRodney W. Grimes 					break;
5829b50d902SRodney W. Grimes 				if (otln(ptbf, j, &ips, &ops, 0))
5839b50d902SRodney W. Grimes 					return(1);
5849b50d902SRodney W. Grimes 			}
5859b50d902SRodney W. Grimes 
5869b50d902SRodney W. Grimes 			/*
5879b50d902SRodney W. Grimes 			 * pad to end of page
5889b50d902SRodney W. Grimes 			 */
5899b50d902SRodney W. Grimes 			if (pln && prtail((lines - pln), 0))
5909b50d902SRodney W. Grimes 				return(1);
5919b50d902SRodney W. Grimes 
5929b50d902SRodney W. Grimes 			/*
5939b50d902SRodney W. Grimes 			 * if EOF go to next file
5949b50d902SRodney W. Grimes 			 */
5959b50d902SRodney W. Grimes 			if (cnt < 0)
5969b50d902SRodney W. Grimes 				break;
5979b50d902SRodney W. Grimes 			++pagecnt;
5989b50d902SRodney W. Grimes 		}
5999b50d902SRodney W. Grimes 		if (inf != stdin)
6009b50d902SRodney W. Grimes 			(void)fclose(inf);
6019b50d902SRodney W. Grimes 	}
6029b50d902SRodney W. Grimes 	if (eoptind < argc)
6039b50d902SRodney W. Grimes 		return(1);
6049b50d902SRodney W. Grimes 	return(0);
6059b50d902SRodney W. Grimes }
6069b50d902SRodney W. Grimes 
6079b50d902SRodney W. Grimes /*
6089b50d902SRodney W. Grimes  * horzcol:	print files with more than one column of output across a page
6099b50d902SRodney W. Grimes  */
6109b50d902SRodney W. Grimes int
6119b50d902SRodney W. Grimes horzcol(argc, argv)
6129b50d902SRodney W. Grimes         int argc;
6139b50d902SRodney W. Grimes         char *argv[];
6149b50d902SRodney W. Grimes {
6159b50d902SRodney W. Grimes 	register char *ptbf;
6169b50d902SRodney W. Grimes 	register int pln;
6179b50d902SRodney W. Grimes 	register int cnt = -1;
6189b50d902SRodney W. Grimes 	register char *lstdat;
6199b50d902SRodney W. Grimes 	register int col = colwd + 1;
6209b50d902SRodney W. Grimes 	register int j;
6219b50d902SRodney W. Grimes 	register int i;
6229b50d902SRodney W. Grimes 	int lncnt;
6239b50d902SRodney W. Grimes 	int pagecnt;
6249b50d902SRodney W. Grimes 	char *buf;
6259b50d902SRodney W. Grimes 	char *hbuf;
6269b50d902SRodney W. Grimes 	char *ohbuf;
6279b50d902SRodney W. Grimes 	char *fname;
6289b50d902SRodney W. Grimes 	FILE *inf;
6299b50d902SRodney W. Grimes 	int ips = 0;
6309b50d902SRodney W. Grimes 	int cps = 0;
6319b50d902SRodney W. Grimes 	int ops = 0;
6329b50d902SRodney W. Grimes 	int mor = 0;
6339b50d902SRodney W. Grimes 
6349b50d902SRodney W. Grimes 	if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) {
6359b50d902SRodney W. Grimes 		mfail();
6369b50d902SRodney W. Grimes 		return(1);
6379b50d902SRodney W. Grimes 	}
6389b50d902SRodney W. Grimes 
6399b50d902SRodney W. Grimes 	/*
6409b50d902SRodney W. Grimes 	 * page header
6419b50d902SRodney W. Grimes 	 */
6429b50d902SRodney W. Grimes 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
6439b50d902SRodney W. Grimes 		mfail();
6449b50d902SRodney W. Grimes 		return(1);
6459b50d902SRodney W. Grimes 	}
6469b50d902SRodney W. Grimes 	ohbuf = hbuf + offst;
6479b50d902SRodney W. Grimes 	if (offst) {
6489b50d902SRodney W. Grimes 		(void)memset(buf, (int)' ', offst);
6499b50d902SRodney W. Grimes 		(void)memset(hbuf, (int)' ', offst);
6509b50d902SRodney W. Grimes 	}
6519b50d902SRodney W. Grimes 
6529b50d902SRodney W. Grimes 	/*
6539b50d902SRodney W. Grimes 	 * loop by file
6549b50d902SRodney W. Grimes 	 */
6559b50d902SRodney W. Grimes 	while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
6569b50d902SRodney W. Grimes 		if (pgnm) {
6579b50d902SRodney W. Grimes 			if (inskip(inf, pgnm, lines))
6589b50d902SRodney W. Grimes 				continue;
6599b50d902SRodney W. Grimes 			pagecnt = pgnm;
6609b50d902SRodney W. Grimes 		} else
6619b50d902SRodney W. Grimes 			pagecnt = 1;
6629b50d902SRodney W. Grimes 		lncnt = 0;
6639b50d902SRodney W. Grimes 
6649b50d902SRodney W. Grimes 		/*
6659b50d902SRodney W. Grimes 		 * loop by page
6669b50d902SRodney W. Grimes 		 */
6679b50d902SRodney W. Grimes 		for(;;) {
6689b50d902SRodney W. Grimes 			/*
6699b50d902SRodney W. Grimes 			 * loop by line
6709b50d902SRodney W. Grimes 			 */
6719b50d902SRodney W. Grimes 			for (i = 0; i < lines; ++i) {
6729b50d902SRodney W. Grimes 				ptbf = buf + offst;
6739b50d902SRodney W. Grimes 				lstdat = ptbf;
6749b50d902SRodney W. Grimes 				j = 0;
6759b50d902SRodney W. Grimes 				/*
6769b50d902SRodney W. Grimes 				 * loop by col
6779b50d902SRodney W. Grimes 				 */
6789b50d902SRodney W. Grimes 				for(;;) {
6799b50d902SRodney W. Grimes 					if (nmwd) {
6809b50d902SRodney W. Grimes 						/*
6819b50d902SRodney W. Grimes 						 * add number to column
6829b50d902SRodney W. Grimes 						 */
6839b50d902SRodney W. Grimes 						addnum(ptbf, nmwd, ++lncnt);
6849b50d902SRodney W. Grimes 						ptbf += nmwd;
6859b50d902SRodney W. Grimes 						*ptbf++ = nmchar;
6869b50d902SRodney W. Grimes 					}
6879b50d902SRodney W. Grimes 					/*
6889b50d902SRodney W. Grimes 					 * input line
6899b50d902SRodney W. Grimes 					 */
6909b50d902SRodney W. Grimes 					if ((cnt = inln(inf,ptbf,colwd,&cps,1,
6919b50d902SRodney W. Grimes 							&mor)) < 0)
6929b50d902SRodney W. Grimes 						break;
6939b50d902SRodney W. Grimes 					ptbf += cnt;
6949b50d902SRodney W. Grimes 					lstdat = ptbf;
6959b50d902SRodney W. Grimes 
6969b50d902SRodney W. Grimes 					/*
6979b50d902SRodney W. Grimes 					 * if last line skip padding
6989b50d902SRodney W. Grimes 					 */
6999b50d902SRodney W. Grimes 					if (++j >= clcnt)
7009b50d902SRodney W. Grimes 						break;
7019b50d902SRodney W. Grimes 
7029b50d902SRodney W. Grimes 					/*
7039b50d902SRodney W. Grimes 					 * pad to end of column
7049b50d902SRodney W. Grimes 					 */
7059b50d902SRodney W. Grimes 					if (sflag)
7069b50d902SRodney W. Grimes 						*ptbf++ = schar;
7079b50d902SRodney W. Grimes 					else if ((pln = col - cnt) > 0) {
7089b50d902SRodney W. Grimes 						(void)memset(ptbf,(int)' ',pln);
7099b50d902SRodney W. Grimes 						ptbf += pln;
7109b50d902SRodney W. Grimes 					}
7119b50d902SRodney W. Grimes 				}
7129b50d902SRodney W. Grimes 
7139b50d902SRodney W. Grimes 				/*
7149b50d902SRodney W. Grimes 				 * determine line length
7159b50d902SRodney W. Grimes 				 */
7169b50d902SRodney W. Grimes 				if ((j = lstdat - buf) <= offst)
7179b50d902SRodney W. Grimes 					break;
7189b50d902SRodney W. Grimes 				if (!i && !nohead &&
7199b50d902SRodney W. Grimes 					prhead(hbuf, fname, pagecnt))
7209b50d902SRodney W. Grimes 					return(1);
7219b50d902SRodney W. Grimes 				/*
7229b50d902SRodney W. Grimes 				 * output line
7239b50d902SRodney W. Grimes 				 */
7249b50d902SRodney W. Grimes 				if (otln(buf, j, &ips, &ops, 0))
7259b50d902SRodney W. Grimes 					return(1);
7269b50d902SRodney W. Grimes 			}
7279b50d902SRodney W. Grimes 
7289b50d902SRodney W. Grimes 			/*
7299b50d902SRodney W. Grimes 			 * pad to end of page
7309b50d902SRodney W. Grimes 			 */
7319b50d902SRodney W. Grimes 			if (i && prtail(lines-i, 0))
7329b50d902SRodney W. Grimes 				return(1);
7339b50d902SRodney W. Grimes 
7349b50d902SRodney W. Grimes 			/*
7359b50d902SRodney W. Grimes 			 * if EOF go to next file
7369b50d902SRodney W. Grimes 			 */
7379b50d902SRodney W. Grimes 			if (cnt < 0)
7389b50d902SRodney W. Grimes 				break;
7399b50d902SRodney W. Grimes 			++pagecnt;
7409b50d902SRodney W. Grimes 		}
7419b50d902SRodney W. Grimes 		if (inf != stdin)
7429b50d902SRodney W. Grimes 			(void)fclose(inf);
7439b50d902SRodney W. Grimes 	}
7449b50d902SRodney W. Grimes 	if (eoptind < argc)
7459b50d902SRodney W. Grimes 		return(1);
7469b50d902SRodney W. Grimes 	return(0);
7479b50d902SRodney W. Grimes }
7489b50d902SRodney W. Grimes 
7499b50d902SRodney W. Grimes /*
7509b50d902SRodney W. Grimes  * mulfile:	print files with more than one column of output and
7519b50d902SRodney W. Grimes  *		more than one file concurrently
7529b50d902SRodney W. Grimes  */
7539b50d902SRodney W. Grimes int
7549b50d902SRodney W. Grimes mulfile(argc, argv)
7559b50d902SRodney W. Grimes         int argc;
7569b50d902SRodney W. Grimes         char *argv[];
7579b50d902SRodney W. Grimes {
7589b50d902SRodney W. Grimes 	register char *ptbf;
7599b50d902SRodney W. Grimes 	register int j;
7609b50d902SRodney W. Grimes 	register int pln;
7619b50d902SRodney W. Grimes 	register int cnt;
7629b50d902SRodney W. Grimes 	register char *lstdat;
7639b50d902SRodney W. Grimes 	register int i;
7649b50d902SRodney W. Grimes 	FILE **fbuf;
7659b50d902SRodney W. Grimes 	int actf;
7669b50d902SRodney W. Grimes 	int lncnt;
7679b50d902SRodney W. Grimes 	int col;
7689b50d902SRodney W. Grimes 	int pagecnt;
7699b50d902SRodney W. Grimes 	int fproc;
7709b50d902SRodney W. Grimes 	char *buf;
7719b50d902SRodney W. Grimes 	char *hbuf;
7729b50d902SRodney W. Grimes 	char *ohbuf;
7739b50d902SRodney W. Grimes 	char *fname;
7749b50d902SRodney W. Grimes 	int ips = 0;
7759b50d902SRodney W. Grimes 	int cps = 0;
7769b50d902SRodney W. Grimes 	int ops = 0;
7779b50d902SRodney W. Grimes 	int mor = 0;
7789b50d902SRodney W. Grimes 
7799b50d902SRodney W. Grimes 	/*
7809b50d902SRodney W. Grimes 	 * array of FILE *, one for each operand
7819b50d902SRodney W. Grimes 	 */
7829b50d902SRodney W. Grimes 	if ((fbuf = (FILE **)malloc((unsigned)clcnt*sizeof(FILE *))) == NULL) {
7839b50d902SRodney W. Grimes 		mfail();
7849b50d902SRodney W. Grimes 		return(1);
7859b50d902SRodney W. Grimes 	}
7869b50d902SRodney W. Grimes 
7879b50d902SRodney W. Grimes 	/*
7889b50d902SRodney W. Grimes 	 * page header
7899b50d902SRodney W. Grimes 	 */
7909b50d902SRodney W. Grimes 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
7919b50d902SRodney W. Grimes 		mfail();
7929b50d902SRodney W. Grimes 		return(1);
7939b50d902SRodney W. Grimes 	}
7949b50d902SRodney W. Grimes 	ohbuf = hbuf + offst;
7959b50d902SRodney W. Grimes 
7969b50d902SRodney W. Grimes 	/*
7979b50d902SRodney W. Grimes 	 * do not know how many columns yet. The number of operands provide an
7989b50d902SRodney W. Grimes 	 * upper bound on the number of columns. We use the number of files
7999b50d902SRodney W. Grimes 	 * we can open successfully to set the number of columns. The operation
8009b50d902SRodney W. Grimes 	 * of the merge operation (-m) in relation to unsuccesful file opens
8019b50d902SRodney W. Grimes 	 * is unspecified by posix.
8029b50d902SRodney W. Grimes 	 */
8039b50d902SRodney W. Grimes 	j = 0;
8049b50d902SRodney W. Grimes 	while (j < clcnt) {
8059b50d902SRodney W. Grimes 		if ((fbuf[j] = nxtfile(argc, argv, &fname, ohbuf, 1)) == NULL)
8069b50d902SRodney W. Grimes 			break;
8079b50d902SRodney W. Grimes 		if (pgnm && (inskip(fbuf[j], pgnm, lines)))
8089b50d902SRodney W. Grimes 			fbuf[j] = NULL;
8099b50d902SRodney W. Grimes 		++j;
8109b50d902SRodney W. Grimes 	}
8119b50d902SRodney W. Grimes 
8129b50d902SRodney W. Grimes 	/*
8139b50d902SRodney W. Grimes 	 * if no files, exit
8149b50d902SRodney W. Grimes 	 */
8159b50d902SRodney W. Grimes 	if (!j)
8169b50d902SRodney W. Grimes 		return(1);
8179b50d902SRodney W. Grimes 
8189b50d902SRodney W. Grimes 	/*
8199b50d902SRodney W. Grimes 	 * calculate page boundries based on open file count
8209b50d902SRodney W. Grimes 	 */
8219b50d902SRodney W. Grimes 	clcnt = j;
8229b50d902SRodney W. Grimes 	if (nmwd) {
8239b50d902SRodney W. Grimes 		colwd = (pgwd - clcnt - nmwd)/clcnt;
8249b50d902SRodney W. Grimes 		pgwd = ((colwd + 1) * clcnt) - nmwd - 2;
8259b50d902SRodney W. Grimes 	} else {
8269b50d902SRodney W. Grimes 		colwd = (pgwd + 1 - clcnt)/clcnt;
8279b50d902SRodney W. Grimes 		pgwd = ((colwd + 1) * clcnt) - 1;
8289b50d902SRodney W. Grimes 	}
8299b50d902SRodney W. Grimes 	if (colwd < 1) {
8309b50d902SRodney W. Grimes 		(void)fprintf(err,
8319b50d902SRodney W. Grimes 		  "pr: page width too small for %d columns\n", clcnt);
8329b50d902SRodney W. Grimes 		return(1);
8339b50d902SRodney W. Grimes 	}
8349b50d902SRodney W. Grimes 	actf = clcnt;
8359b50d902SRodney W. Grimes 	col = colwd + 1;
8369b50d902SRodney W. Grimes 
8379b50d902SRodney W. Grimes 	/*
8389b50d902SRodney W. Grimes 	 * line buffer
8399b50d902SRodney W. Grimes 	 */
8409b50d902SRodney W. Grimes 	if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) {
8419b50d902SRodney W. Grimes 		mfail();
8429b50d902SRodney W. Grimes 		return(1);
8439b50d902SRodney W. Grimes 	}
8449b50d902SRodney W. Grimes 	if (offst) {
8459b50d902SRodney W. Grimes 		(void)memset(buf, (int)' ', offst);
8469b50d902SRodney W. Grimes 		(void)memset(hbuf, (int)' ', offst);
8479b50d902SRodney W. Grimes 	}
8489b50d902SRodney W. Grimes 	if (pgnm)
8499b50d902SRodney W. Grimes 		pagecnt = pgnm;
8509b50d902SRodney W. Grimes 	else
8519b50d902SRodney W. Grimes 		pagecnt = 1;
8529b50d902SRodney W. Grimes 	lncnt = 0;
8539b50d902SRodney W. Grimes 
8549b50d902SRodney W. Grimes 	/*
8559b50d902SRodney W. Grimes 	 * continue to loop while any file still has data
8569b50d902SRodney W. Grimes 	 */
8579b50d902SRodney W. Grimes 	while (actf > 0) {
8589b50d902SRodney W. Grimes 		/*
8599b50d902SRodney W. Grimes 		 * loop by line
8609b50d902SRodney W. Grimes 		 */
8619b50d902SRodney W. Grimes 		for (i = 0; i < lines; ++i) {
8629b50d902SRodney W. Grimes 			ptbf = buf + offst;
8639b50d902SRodney W. Grimes 			lstdat = ptbf;
8649b50d902SRodney W. Grimes 			if (nmwd) {
8659b50d902SRodney W. Grimes 				/*
8669b50d902SRodney W. Grimes 				 * add line number to line
8679b50d902SRodney W. Grimes 				 */
8689b50d902SRodney W. Grimes 				addnum(ptbf, nmwd, ++lncnt);
8699b50d902SRodney W. Grimes 				ptbf += nmwd;
8709b50d902SRodney W. Grimes 				*ptbf++ = nmchar;
8719b50d902SRodney W. Grimes 			}
8729b50d902SRodney W. Grimes 			j = 0;
8739b50d902SRodney W. Grimes 			fproc = 0;
8749b50d902SRodney W. Grimes 
8759b50d902SRodney W. Grimes 			/*
8769b50d902SRodney W. Grimes 			 * loop by column
8779b50d902SRodney W. Grimes 			 */
8789b50d902SRodney W. Grimes 			for (j = 0; j < clcnt; ++j) {
8799b50d902SRodney W. Grimes 				if (fbuf[j] == NULL) {
8809b50d902SRodney W. Grimes 					/*
8819b50d902SRodney W. Grimes 					 * empty column; EOF
8829b50d902SRodney W. Grimes 					 */
8839b50d902SRodney W. Grimes 					cnt = 0;
8849b50d902SRodney W. Grimes 				} else if ((cnt = inln(fbuf[j], ptbf, colwd,
8859b50d902SRodney W. Grimes 							&cps, 1, &mor)) < 0) {
8869b50d902SRodney W. Grimes 					/*
8879b50d902SRodney W. Grimes 					 * EOF hit; no data
8889b50d902SRodney W. Grimes 					 */
8899b50d902SRodney W. Grimes 					if (fbuf[j] != stdin)
8909b50d902SRodney W. Grimes 						(void)fclose(fbuf[j]);
8919b50d902SRodney W. Grimes 					fbuf[j] = NULL;
8929b50d902SRodney W. Grimes 					--actf;
8939b50d902SRodney W. Grimes 					cnt = 0;
8949b50d902SRodney W. Grimes 				} else {
8959b50d902SRodney W. Grimes 					/*
8969b50d902SRodney W. Grimes 					 * process file data
8979b50d902SRodney W. Grimes 					 */
8989b50d902SRodney W. Grimes 					ptbf += cnt;
8999b50d902SRodney W. Grimes 					lstdat = ptbf;
9009b50d902SRodney W. Grimes 					fproc++;
9019b50d902SRodney W. Grimes 				}
9029b50d902SRodney W. Grimes 
9039b50d902SRodney W. Grimes 				/*
9049b50d902SRodney W. Grimes 				 * if last ACTIVE column, done with line
9059b50d902SRodney W. Grimes 				 */
9069b50d902SRodney W. Grimes 				if (fproc >= actf)
9079b50d902SRodney W. Grimes 					break;
9089b50d902SRodney W. Grimes 
9099b50d902SRodney W. Grimes 				/*
9109b50d902SRodney W. Grimes 				 * pad to end of column
9119b50d902SRodney W. Grimes 				 */
9129b50d902SRodney W. Grimes 				if (sflag) {
9139b50d902SRodney W. Grimes 					*ptbf++ = schar;
9149b50d902SRodney W. Grimes 				} else if ((pln = col - cnt) > 0) {
9159b50d902SRodney W. Grimes 					(void)memset(ptbf, (int)' ', pln);
9169b50d902SRodney W. Grimes 					ptbf += pln;
9179b50d902SRodney W. Grimes 				}
9189b50d902SRodney W. Grimes 			}
9199b50d902SRodney W. Grimes 
9209b50d902SRodney W. Grimes 			/*
9219b50d902SRodney W. Grimes 			 * calculate data in line
9229b50d902SRodney W. Grimes 			 */
9239b50d902SRodney W. Grimes 			if ((j = lstdat - buf) <= offst)
9249b50d902SRodney W. Grimes 				break;
9259b50d902SRodney W. Grimes 
9269b50d902SRodney W. Grimes 			if (!i && !nohead && prhead(hbuf, fname, pagecnt))
9279b50d902SRodney W. Grimes 				return(1);
9289b50d902SRodney W. Grimes 
9299b50d902SRodney W. Grimes 			/*
9309b50d902SRodney W. Grimes 			 * output line
9319b50d902SRodney W. Grimes 			 */
9329b50d902SRodney W. Grimes 			if (otln(buf, j, &ips, &ops, 0))
9339b50d902SRodney W. Grimes 				return(1);
9349b50d902SRodney W. Grimes 
9359b50d902SRodney W. Grimes 			/*
9369b50d902SRodney W. Grimes 			 * if no more active files, done
9379b50d902SRodney W. Grimes 			 */
9389b50d902SRodney W. Grimes 			if (actf <= 0) {
9399b50d902SRodney W. Grimes 				++i;
9409b50d902SRodney W. Grimes 				break;
9419b50d902SRodney W. Grimes 			}
9429b50d902SRodney W. Grimes 		}
9439b50d902SRodney W. Grimes 
9449b50d902SRodney W. Grimes 		/*
9459b50d902SRodney W. Grimes 		 * pad to end of page
9469b50d902SRodney W. Grimes 		 */
9479b50d902SRodney W. Grimes 		if (i && prtail(lines-i, 0))
9489b50d902SRodney W. Grimes 			return(1);
9499b50d902SRodney W. Grimes 		++pagecnt;
9509b50d902SRodney W. Grimes 	}
9519b50d902SRodney W. Grimes 	if (eoptind < argc)
9529b50d902SRodney W. Grimes 		return(1);
9539b50d902SRodney W. Grimes 	return(0);
9549b50d902SRodney W. Grimes }
9559b50d902SRodney W. Grimes 
9569b50d902SRodney W. Grimes /*
9579b50d902SRodney W. Grimes  * inln():	input a line of data (unlimited length lines supported)
9589b50d902SRodney W. Grimes  *		Input is optionally expanded to spaces
9599b50d902SRodney W. Grimes  *
9609b50d902SRodney W. Grimes  *	inf:	file
9619b50d902SRodney W. Grimes  *	buf:	buffer
9629b50d902SRodney W. Grimes  *	lim:	buffer length
9639b50d902SRodney W. Grimes  *	cps:	column positon 1st char in buffer (large line support)
9649b50d902SRodney W. Grimes  *	trnc:	throw away data more than lim up to \n
9659b50d902SRodney W. Grimes  *	mor:	set if more data in line (not truncated)
9669b50d902SRodney W. Grimes  */
9679b50d902SRodney W. Grimes int
9689b50d902SRodney W. Grimes inln(inf, buf, lim, cps, trnc, mor)
9699b50d902SRodney W. Grimes 	FILE *inf;
9709b50d902SRodney W. Grimes 	char *buf;
9719b50d902SRodney W. Grimes 	register int lim;
9729b50d902SRodney W. Grimes 	int *cps;
9739b50d902SRodney W. Grimes 	int trnc;
9749b50d902SRodney W. Grimes 	int *mor;
9759b50d902SRodney W. Grimes {
9769b50d902SRodney W. Grimes 	register int col;
9779b50d902SRodney W. Grimes 	register int gap = ingap;
9789b50d902SRodney W. Grimes 	register int ch = EOF;
9799b50d902SRodney W. Grimes 	register char *ptbuf;
9809b50d902SRodney W. Grimes 	register int chk = (int)inchar;
9819b50d902SRodney W. Grimes 
9829b50d902SRodney W. Grimes 	ptbuf = buf;
9839b50d902SRodney W. Grimes 
9849b50d902SRodney W. Grimes 	if (gap) {
9859b50d902SRodney W. Grimes 		/*
9869b50d902SRodney W. Grimes 		 * expanding input option
9879b50d902SRodney W. Grimes 		 */
9889b50d902SRodney W. Grimes 		while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
9899b50d902SRodney W. Grimes 			/*
9909b50d902SRodney W. Grimes 			 * is this the input "tab" char
9919b50d902SRodney W. Grimes 			 */
9929b50d902SRodney W. Grimes 			if (ch == chk) {
9939b50d902SRodney W. Grimes 				/*
9949b50d902SRodney W. Grimes 				 * expand to number of spaces
9959b50d902SRodney W. Grimes 				 */
9969b50d902SRodney W. Grimes 				col = (ptbuf - buf) + *cps;
9979b50d902SRodney W. Grimes 				col = gap - (col % gap);
9989b50d902SRodney W. Grimes 
9999b50d902SRodney W. Grimes 				/*
10009b50d902SRodney W. Grimes 				 * if more than this line, push back
10019b50d902SRodney W. Grimes 				 */
10029b50d902SRodney W. Grimes 				if ((col > lim) && (ungetc(ch, inf) == EOF))
10039b50d902SRodney W. Grimes 					return(1);
10049b50d902SRodney W. Grimes 
10059b50d902SRodney W. Grimes 				/*
10069b50d902SRodney W. Grimes 				 * expand to spaces
10079b50d902SRodney W. Grimes 				 */
10089b50d902SRodney W. Grimes 				while ((--col >= 0) && (--lim >= 0))
10099b50d902SRodney W. Grimes 					*ptbuf++ = ' ';
10109b50d902SRodney W. Grimes 				continue;
10119b50d902SRodney W. Grimes 			}
10129b50d902SRodney W. Grimes 			if (ch == '\n')
10139b50d902SRodney W. Grimes 				break;
10149b50d902SRodney W. Grimes 			*ptbuf++ = ch;
10159b50d902SRodney W. Grimes 		}
10169b50d902SRodney W. Grimes 	} else {
10179b50d902SRodney W. Grimes 		/*
10189b50d902SRodney W. Grimes 		 * no expansion
10199b50d902SRodney W. Grimes 		 */
10209b50d902SRodney W. Grimes 		while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
10219b50d902SRodney W. Grimes 			if (ch == '\n')
10229b50d902SRodney W. Grimes 				break;
10239b50d902SRodney W. Grimes 			*ptbuf++ = ch;
10249b50d902SRodney W. Grimes 		}
10259b50d902SRodney W. Grimes 	}
10269b50d902SRodney W. Grimes 	col = ptbuf - buf;
10279b50d902SRodney W. Grimes 	if (ch == EOF) {
10289b50d902SRodney W. Grimes 		*mor = 0;
10299b50d902SRodney W. Grimes 		*cps = 0;
10309b50d902SRodney W. Grimes 		if (!col)
10319b50d902SRodney W. Grimes 			return(-1);
10329b50d902SRodney W. Grimes 		return(col);
10339b50d902SRodney W. Grimes 	}
10349b50d902SRodney W. Grimes 	if (ch == '\n') {
10359b50d902SRodney W. Grimes 		/*
10369b50d902SRodney W. Grimes 		 * entire line processed
10379b50d902SRodney W. Grimes 		 */
10389b50d902SRodney W. Grimes 		*mor = 0;
10399b50d902SRodney W. Grimes 		*cps = 0;
10409b50d902SRodney W. Grimes 		return(col);
10419b50d902SRodney W. Grimes 	}
10429b50d902SRodney W. Grimes 
10439b50d902SRodney W. Grimes 	/*
10449b50d902SRodney W. Grimes 	 * line was larger than limit
10459b50d902SRodney W. Grimes 	 */
10469b50d902SRodney W. Grimes 	if (trnc) {
10479b50d902SRodney W. Grimes 		/*
10489b50d902SRodney W. Grimes 		 * throw away rest of line
10499b50d902SRodney W. Grimes 		 */
10509b50d902SRodney W. Grimes 		while ((ch = getc(inf)) != EOF) {
10519b50d902SRodney W. Grimes 			if (ch == '\n')
10529b50d902SRodney W. Grimes 				break;
10539b50d902SRodney W. Grimes 		}
10549b50d902SRodney W. Grimes 		*cps = 0;
10559b50d902SRodney W. Grimes 		*mor = 0;
10569b50d902SRodney W. Grimes 	} else {
10579b50d902SRodney W. Grimes 		/*
10589b50d902SRodney W. Grimes 		 * save column offset if not truncated
10599b50d902SRodney W. Grimes 		 */
10609b50d902SRodney W. Grimes 		*cps += col;
10619b50d902SRodney W. Grimes 		*mor = 1;
10629b50d902SRodney W. Grimes 	}
10639b50d902SRodney W. Grimes 
10649b50d902SRodney W. Grimes 	return(col);
10659b50d902SRodney W. Grimes }
10669b50d902SRodney W. Grimes 
10679b50d902SRodney W. Grimes /*
10689b50d902SRodney W. Grimes  * otln():	output a line of data. (Supports unlimited length lines)
10699b50d902SRodney W. Grimes  *		output is optionally contracted to tabs
10709b50d902SRodney W. Grimes  *
10719b50d902SRodney W. Grimes  *	buf:	output buffer with data
10729b50d902SRodney W. Grimes  *	cnt:	number of chars of valid data in buf
10739b50d902SRodney W. Grimes  *	svips:	buffer input column position (for large lines)
10749b50d902SRodney W. Grimes  *	svops:	buffer output column position (for large lines)
10759b50d902SRodney W. Grimes  *	mor:	output line not complete in this buf; more data to come.
10769b50d902SRodney W. Grimes  *		1 is more, 0 is complete, -1 is no \n's
10779b50d902SRodney W. Grimes  */
10789b50d902SRodney W. Grimes int
10799b50d902SRodney W. Grimes otln(buf, cnt, svips, svops, mor)
10809b50d902SRodney W. Grimes 	register char *buf;
10819b50d902SRodney W. Grimes 	int cnt;
10829b50d902SRodney W. Grimes 	int *svops;
10839b50d902SRodney W. Grimes 	int *svips;
10849b50d902SRodney W. Grimes 	int mor;
10859b50d902SRodney W. Grimes {
10869b50d902SRodney W. Grimes 	register int ops;		/* last col output */
10879b50d902SRodney W. Grimes 	register int ips;		/* last col in buf examined */
10889b50d902SRodney W. Grimes 	register int gap = ogap;
10899b50d902SRodney W. Grimes 	register int tbps;
10909b50d902SRodney W. Grimes 	register char *endbuf;
10919b50d902SRodney W. Grimes 
10929b50d902SRodney W. Grimes 	if (ogap) {
10939b50d902SRodney W. Grimes 		/*
10949b50d902SRodney W. Grimes 		 * contracting on output
10959b50d902SRodney W. Grimes 		 */
10969b50d902SRodney W. Grimes 		endbuf = buf + cnt;
10979b50d902SRodney W. Grimes 		ops = *svops;
10989b50d902SRodney W. Grimes 		ips = *svips;
10999b50d902SRodney W. Grimes 		while (buf < endbuf) {
11009b50d902SRodney W. Grimes 			/*
11019b50d902SRodney W. Grimes 			 * count number of spaces and ochar in buffer
11029b50d902SRodney W. Grimes 			 */
11039b50d902SRodney W. Grimes 			if (*buf == ' ') {
11049b50d902SRodney W. Grimes 				++ips;
11059b50d902SRodney W. Grimes 				++buf;
11069b50d902SRodney W. Grimes 				continue;
11079b50d902SRodney W. Grimes 			}
11089b50d902SRodney W. Grimes 
11099b50d902SRodney W. Grimes 			/*
11109b50d902SRodney W. Grimes 			 * simulate ochar processing
11119b50d902SRodney W. Grimes 			 */
11129b50d902SRodney W. Grimes 			if (*buf == ochar) {
11139b50d902SRodney W. Grimes 				ips += gap - (ips % gap);
11149b50d902SRodney W. Grimes 				++buf;
11159b50d902SRodney W. Grimes 				continue;
11169b50d902SRodney W. Grimes 			}
11179b50d902SRodney W. Grimes 
11189b50d902SRodney W. Grimes 			/*
11199b50d902SRodney W. Grimes 			 * got a non space char; contract out spaces
11209b50d902SRodney W. Grimes 			 */
11219b50d902SRodney W. Grimes 			while (ops < ips) {
11229b50d902SRodney W. Grimes 				/*
11239b50d902SRodney W. Grimes 				 * use as many ochar as will fit
11249b50d902SRodney W. Grimes 				 */
11259b50d902SRodney W. Grimes 				if ((tbps = ops + gap - (ops % gap)) > ips)
11269b50d902SRodney W. Grimes 					break;
11279b50d902SRodney W. Grimes 				if (putchar(ochar) == EOF) {
11289b50d902SRodney W. Grimes 					pfail();
11299b50d902SRodney W. Grimes 					return(1);
11309b50d902SRodney W. Grimes 				}
11319b50d902SRodney W. Grimes 				ops = tbps;
11329b50d902SRodney W. Grimes 			}
11339b50d902SRodney W. Grimes 
11349b50d902SRodney W. Grimes 			while (ops < ips) {
11359b50d902SRodney W. Grimes 				/*
11369b50d902SRodney W. Grimes 				 * finish off with spaces
11379b50d902SRodney W. Grimes 				 */
11389b50d902SRodney W. Grimes 				if (putchar(' ') == EOF) {
11399b50d902SRodney W. Grimes 					pfail();
11409b50d902SRodney W. Grimes 					return(1);
11419b50d902SRodney W. Grimes 				}
11429b50d902SRodney W. Grimes 				++ops;
11439b50d902SRodney W. Grimes 			}
11449b50d902SRodney W. Grimes 
11459b50d902SRodney W. Grimes 			/*
11469b50d902SRodney W. Grimes 			 * output non space char
11479b50d902SRodney W. Grimes 			 */
11489b50d902SRodney W. Grimes 			if (putchar(*buf++) == EOF) {
11499b50d902SRodney W. Grimes 				pfail();
11509b50d902SRodney W. Grimes 				return(1);
11519b50d902SRodney W. Grimes 			}
11529b50d902SRodney W. Grimes 			++ips;
11539b50d902SRodney W. Grimes 			++ops;
11549b50d902SRodney W. Grimes 		}
11559b50d902SRodney W. Grimes 
11569b50d902SRodney W. Grimes 		if (mor > 0) {
11579b50d902SRodney W. Grimes 			/*
11589b50d902SRodney W. Grimes 			 * if incomplete line, save position counts
11599b50d902SRodney W. Grimes 			 */
11609b50d902SRodney W. Grimes 			*svops = ops;
11619b50d902SRodney W. Grimes 			*svips = ips;
11629b50d902SRodney W. Grimes 			return(0);
11639b50d902SRodney W. Grimes 		}
11649b50d902SRodney W. Grimes 
11659b50d902SRodney W. Grimes 		if (mor < 0) {
11669b50d902SRodney W. Grimes 			while (ops < ips) {
11679b50d902SRodney W. Grimes 				/*
11689b50d902SRodney W. Grimes 				 * use as many ochar as will fit
11699b50d902SRodney W. Grimes 				 */
11709b50d902SRodney W. Grimes 				if ((tbps = ops + gap - (ops % gap)) > ips)
11719b50d902SRodney W. Grimes 					break;
11729b50d902SRodney W. Grimes 				if (putchar(ochar) == EOF) {
11739b50d902SRodney W. Grimes 					pfail();
11749b50d902SRodney W. Grimes 					return(1);
11759b50d902SRodney W. Grimes 				}
11769b50d902SRodney W. Grimes 				ops = tbps;
11779b50d902SRodney W. Grimes 			}
11789b50d902SRodney W. Grimes 			while (ops < ips) {
11799b50d902SRodney W. Grimes 				/*
11809b50d902SRodney W. Grimes 				 * finish off with spaces
11819b50d902SRodney W. Grimes 				 */
11829b50d902SRodney W. Grimes 				if (putchar(' ') == EOF) {
11839b50d902SRodney W. Grimes 					pfail();
11849b50d902SRodney W. Grimes 					return(1);
11859b50d902SRodney W. Grimes 				}
11869b50d902SRodney W. Grimes 				++ops;
11879b50d902SRodney W. Grimes 			}
11889b50d902SRodney W. Grimes 			return(0);
11899b50d902SRodney W. Grimes 		}
11909b50d902SRodney W. Grimes 	} else {
11919b50d902SRodney W. Grimes 		/*
11929b50d902SRodney W. Grimes 		 * output is not contracted
11939b50d902SRodney W. Grimes 		 */
11949b50d902SRodney W. Grimes 		if (cnt && (fwrite(buf, sizeof(char), cnt, stdout) <= 0)) {
11959b50d902SRodney W. Grimes 			pfail();
11969b50d902SRodney W. Grimes 			return(1);
11979b50d902SRodney W. Grimes 		}
11989b50d902SRodney W. Grimes 		if (mor != 0)
11999b50d902SRodney W. Grimes 			return(0);
12009b50d902SRodney W. Grimes 	}
12019b50d902SRodney W. Grimes 
12029b50d902SRodney W. Grimes 	/*
12039b50d902SRodney W. Grimes 	 * process line end and double space as required
12049b50d902SRodney W. Grimes 	 */
12059b50d902SRodney W. Grimes 	if ((putchar('\n') == EOF) || (dspace && (putchar('\n') == EOF))) {
12069b50d902SRodney W. Grimes 		pfail();
12079b50d902SRodney W. Grimes 		return(1);
12089b50d902SRodney W. Grimes 	}
12099b50d902SRodney W. Grimes 	return(0);
12109b50d902SRodney W. Grimes }
12119b50d902SRodney W. Grimes 
12129b50d902SRodney W. Grimes /*
12139b50d902SRodney W. Grimes  * inskip():	skip over pgcnt pages with lncnt lines per page
12149b50d902SRodney W. Grimes  *		file is closed at EOF (if not stdin).
12159b50d902SRodney W. Grimes  *
12169b50d902SRodney W. Grimes  *	inf	FILE * to read from
12179b50d902SRodney W. Grimes  *	pgcnt	number of pages to skip
12189b50d902SRodney W. Grimes  *	lncnt	number of lines per page
12199b50d902SRodney W. Grimes  */
12209b50d902SRodney W. Grimes int
12219b50d902SRodney W. Grimes inskip(inf, pgcnt, lncnt)
12229b50d902SRodney W. Grimes 	FILE *inf;
12239b50d902SRodney W. Grimes 	register int pgcnt;
12249b50d902SRodney W. Grimes 	register int lncnt;
12259b50d902SRodney W. Grimes {
12269b50d902SRodney W. Grimes 	register int c;
12279b50d902SRodney W. Grimes 	register int cnt;
12289b50d902SRodney W. Grimes 
12299b50d902SRodney W. Grimes 	while(--pgcnt > 0) {
12309b50d902SRodney W. Grimes 		cnt = lncnt;
12319b50d902SRodney W. Grimes 		while ((c = getc(inf)) != EOF) {
12329b50d902SRodney W. Grimes 			if ((c == '\n') && (--cnt == 0))
12339b50d902SRodney W. Grimes 				break;
12349b50d902SRodney W. Grimes 		}
12359b50d902SRodney W. Grimes 		if (c == EOF) {
12369b50d902SRodney W. Grimes 			if (inf != stdin)
12379b50d902SRodney W. Grimes 				(void)fclose(inf);
12389b50d902SRodney W. Grimes 			return(1);
12399b50d902SRodney W. Grimes 		}
12409b50d902SRodney W. Grimes 	}
12419b50d902SRodney W. Grimes 	return(0);
12429b50d902SRodney W. Grimes }
12439b50d902SRodney W. Grimes 
12449b50d902SRodney W. Grimes /*
12459b50d902SRodney W. Grimes  * nxtfile:	returns a FILE * to next file in arg list and sets the
12469b50d902SRodney W. Grimes  *		time field for this file (or current date).
12479b50d902SRodney W. Grimes  *
12489b50d902SRodney W. Grimes  *	buf	array to store proper date for the header.
12499b50d902SRodney W. Grimes  *	dt	if set skips the date processing (used with -m)
12509b50d902SRodney W. Grimes  */
12519b50d902SRodney W. Grimes FILE *
12529b50d902SRodney W. Grimes nxtfile(argc, argv, fname, buf, dt)
12539b50d902SRodney W. Grimes 	int argc;
12549b50d902SRodney W. Grimes 	char **argv;
12559b50d902SRodney W. Grimes 	char **fname;
12569b50d902SRodney W. Grimes 	char *buf;
12579b50d902SRodney W. Grimes 	int dt;
12589b50d902SRodney W. Grimes {
12599b50d902SRodney W. Grimes 	FILE *inf = NULL;
12609b50d902SRodney W. Grimes 	struct timeval tv;
1261375557fcSBruce Evans 	time_t tv_sec;
12629b50d902SRodney W. Grimes 	struct timezone tz;
12639b50d902SRodney W. Grimes 	struct tm *timeptr = NULL;
12649b50d902SRodney W. Grimes 	struct stat statbuf;
12659b50d902SRodney W. Grimes 	static int twice = -1;
12669b50d902SRodney W. Grimes 
12679b50d902SRodney W. Grimes 	++twice;
12689b50d902SRodney W. Grimes 	if (eoptind >= argc) {
12699b50d902SRodney W. Grimes 		/*
12709b50d902SRodney W. Grimes 		 * no file listed; default, use standard input
12719b50d902SRodney W. Grimes 		 */
12729b50d902SRodney W. Grimes 		if (twice)
12739b50d902SRodney W. Grimes 			return(NULL);
12749b50d902SRodney W. Grimes 		clearerr(stdin);
12759b50d902SRodney W. Grimes 		inf = stdin;
12769b50d902SRodney W. Grimes 		if (header != NULL)
12779b50d902SRodney W. Grimes 			*fname = header;
12789b50d902SRodney W. Grimes 		else
12799b50d902SRodney W. Grimes 			*fname = FNAME;
12809b50d902SRodney W. Grimes 		if (nohead)
12819b50d902SRodney W. Grimes 			return(inf);
12829b50d902SRodney W. Grimes 		if (gettimeofday(&tv, &tz) < 0) {
12839b50d902SRodney W. Grimes 			++errcnt;
12849b50d902SRodney W. Grimes 			(void)fprintf(err, "pr: cannot get time of day, %s\n",
12859b50d902SRodney W. Grimes 				strerror(errno));
12869b50d902SRodney W. Grimes 			eoptind = argc - 1;
12879b50d902SRodney W. Grimes 			return(NULL);
12889b50d902SRodney W. Grimes 		}
1289375557fcSBruce Evans 		tv_sec = tv.tv_sec;
1290375557fcSBruce Evans 		timeptr = localtime(&tv_sec);
12919b50d902SRodney W. Grimes 	}
12929b50d902SRodney W. Grimes 	for (; eoptind < argc; ++eoptind) {
12939b50d902SRodney W. Grimes 		if (strcmp(argv[eoptind], "-") == 0) {
12949b50d902SRodney W. Grimes 			/*
12959b50d902SRodney W. Grimes 			 * process a "-" for filename
12969b50d902SRodney W. Grimes 			 */
12979b50d902SRodney W. Grimes 			clearerr(stdin);
12989b50d902SRodney W. Grimes 			inf = stdin;
12999b50d902SRodney W. Grimes 			if (header != NULL)
13009b50d902SRodney W. Grimes 				*fname = header;
13019b50d902SRodney W. Grimes 			else
13029b50d902SRodney W. Grimes 				*fname = FNAME;
13039b50d902SRodney W. Grimes 			++eoptind;
13049b50d902SRodney W. Grimes 			if (nohead || (dt && twice))
13059b50d902SRodney W. Grimes 				return(inf);
13069b50d902SRodney W. Grimes 			if (gettimeofday(&tv, &tz) < 0) {
13079b50d902SRodney W. Grimes 				++errcnt;
13089b50d902SRodney W. Grimes 				(void)fprintf(err,
13099b50d902SRodney W. Grimes 					"pr: cannot get time of day, %s\n",
13109b50d902SRodney W. Grimes 					strerror(errno));
13119b50d902SRodney W. Grimes 				return(NULL);
13129b50d902SRodney W. Grimes 			}
1313375557fcSBruce Evans 			tv_sec = tv.tv_sec;
1314375557fcSBruce Evans 			timeptr = localtime(&tv_sec);
13159b50d902SRodney W. Grimes 		} else {
13169b50d902SRodney W. Grimes 			/*
13179b50d902SRodney W. Grimes 			 * normal file processing
13189b50d902SRodney W. Grimes 			 */
13199b50d902SRodney W. Grimes 			if ((inf = fopen(argv[eoptind], "r")) == NULL) {
13209b50d902SRodney W. Grimes 				++errcnt;
13219b50d902SRodney W. Grimes 				if (nodiag)
13229b50d902SRodney W. Grimes 					continue;
13239b50d902SRodney W. Grimes 				(void)fprintf(err, "pr: Cannot open %s, %s\n",
13249b50d902SRodney W. Grimes 					argv[eoptind], strerror(errno));
13259b50d902SRodney W. Grimes 				continue;
13269b50d902SRodney W. Grimes 			}
13279b50d902SRodney W. Grimes 			if (header != NULL)
13289b50d902SRodney W. Grimes 				*fname = header;
13299b50d902SRodney W. Grimes 			else if (dt)
13309b50d902SRodney W. Grimes 				*fname = FNAME;
13319b50d902SRodney W. Grimes 			else
13329b50d902SRodney W. Grimes 				*fname = argv[eoptind];
13339b50d902SRodney W. Grimes 			++eoptind;
13349b50d902SRodney W. Grimes 			if (nohead || (dt && twice))
13359b50d902SRodney W. Grimes 				return(inf);
13369b50d902SRodney W. Grimes 
13379b50d902SRodney W. Grimes 			if (dt) {
13389b50d902SRodney W. Grimes 				if (gettimeofday(&tv, &tz) < 0) {
13399b50d902SRodney W. Grimes 					++errcnt;
13409b50d902SRodney W. Grimes 					(void)fprintf(err,
13419b50d902SRodney W. Grimes 					     "pr: cannot get time of day, %s\n",
13429b50d902SRodney W. Grimes 					     strerror(errno));
13439b50d902SRodney W. Grimes 					return(NULL);
13449b50d902SRodney W. Grimes 				}
1345375557fcSBruce Evans 				tv_sec = tv.tv_sec;
1346375557fcSBruce Evans 				timeptr = localtime(&tv_sec);
13479b50d902SRodney W. Grimes 			} else {
13489b50d902SRodney W. Grimes 				if (fstat(fileno(inf), &statbuf) < 0) {
13499b50d902SRodney W. Grimes 					++errcnt;
13509b50d902SRodney W. Grimes 					(void)fclose(inf);
13519b50d902SRodney W. Grimes 					(void)fprintf(err,
13529b50d902SRodney W. Grimes 						"pr: Cannot stat %s, %s\n",
13539b50d902SRodney W. Grimes 						argv[eoptind], strerror(errno));
13549b50d902SRodney W. Grimes 					return(NULL);
13559b50d902SRodney W. Grimes 				}
13569b50d902SRodney W. Grimes 				timeptr = localtime(&(statbuf.st_mtime));
13579b50d902SRodney W. Grimes 			}
13589b50d902SRodney W. Grimes 		}
13599b50d902SRodney W. Grimes 		break;
13609b50d902SRodney W. Grimes 	}
13619b50d902SRodney W. Grimes 	if (inf == NULL)
13629b50d902SRodney W. Grimes 		return(NULL);
13639b50d902SRodney W. Grimes 
13649b50d902SRodney W. Grimes 	/*
13659b50d902SRodney W. Grimes 	 * set up time field used in header
13669b50d902SRodney W. Grimes 	 */
13679b50d902SRodney W. Grimes 	if (strftime(buf, HDBUF, timefrmt, timeptr) <= 0) {
13689b50d902SRodney W. Grimes 		++errcnt;
13699b50d902SRodney W. Grimes 		if (inf != stdin)
13709b50d902SRodney W. Grimes 			(void)fclose(inf);
13719b50d902SRodney W. Grimes 		(void)fputs("pr: time conversion failed\n", err);
13729b50d902SRodney W. Grimes 		return(NULL);
13739b50d902SRodney W. Grimes 	}
13749b50d902SRodney W. Grimes 	return(inf);
13759b50d902SRodney W. Grimes }
13769b50d902SRodney W. Grimes 
13779b50d902SRodney W. Grimes /*
13789b50d902SRodney W. Grimes  * addnum():	adds the line number to the column
13799b50d902SRodney W. Grimes  *		Truncates from the front or pads with spaces as required.
13809b50d902SRodney W. Grimes  *		Numbers are right justified.
13819b50d902SRodney W. Grimes  *
13829b50d902SRodney W. Grimes  *	buf	buffer to store the number
13839b50d902SRodney W. Grimes  *	wdth	width of buffer to fill
13849b50d902SRodney W. Grimes  *	line	line number
13859b50d902SRodney W. Grimes  *
13869b50d902SRodney W. Grimes  *		NOTE: numbers occupy part of the column. The posix
13879b50d902SRodney W. Grimes  *		spec does not specify if -i processing should or should not
13889b50d902SRodney W. Grimes  *		occur on number padding. The spec does say it occupies
13899b50d902SRodney W. Grimes  *		part of the column. The usage of addnum	currently treats
13909b50d902SRodney W. Grimes  *		numbers as part of the column so spaces may be replaced.
13919b50d902SRodney W. Grimes  */
13929b50d902SRodney W. Grimes void
13939b50d902SRodney W. Grimes addnum(buf, wdth, line)
13949b50d902SRodney W. Grimes 	register char *buf;
13959b50d902SRodney W. Grimes 	register int wdth;
13969b50d902SRodney W. Grimes 	register int line;
13979b50d902SRodney W. Grimes {
13989b50d902SRodney W. Grimes 	register char *pt = buf + wdth;
13999b50d902SRodney W. Grimes 
14009b50d902SRodney W. Grimes 	do {
14019b50d902SRodney W. Grimes 		*--pt = digs[line % 10];
14029b50d902SRodney W. Grimes 		line /= 10;
14039b50d902SRodney W. Grimes 	} while (line && (pt > buf));
14049b50d902SRodney W. Grimes 
14059b50d902SRodney W. Grimes 	/*
14069b50d902SRodney W. Grimes 	 * pad with space as required
14079b50d902SRodney W. Grimes 	 */
14089b50d902SRodney W. Grimes 	while (pt > buf)
14099b50d902SRodney W. Grimes 		*--pt = ' ';
14109b50d902SRodney W. Grimes }
14119b50d902SRodney W. Grimes 
14129b50d902SRodney W. Grimes /*
14139b50d902SRodney W. Grimes  * prhead():	prints the top of page header
14149b50d902SRodney W. Grimes  *
14159b50d902SRodney W. Grimes  *	buf	buffer with time field (and offset)
14169b50d902SRodney W. Grimes  *	cnt	number of chars in buf
14179b50d902SRodney W. Grimes  *	fname	fname field for header
14189b50d902SRodney W. Grimes  *	pagcnt	page number
14199b50d902SRodney W. Grimes  */
14209b50d902SRodney W. Grimes int
14219b50d902SRodney W. Grimes prhead(buf, fname, pagcnt)
14229b50d902SRodney W. Grimes 	char *buf;
14239b50d902SRodney W. Grimes 	char *fname;
14249b50d902SRodney W. Grimes 	int pagcnt;
14259b50d902SRodney W. Grimes {
14269b50d902SRodney W. Grimes 	int ips = 0;
14279b50d902SRodney W. Grimes 	int ops = 0;
14289b50d902SRodney W. Grimes 
14299b50d902SRodney W. Grimes 	if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) {
14309b50d902SRodney W. Grimes 		pfail();
14319b50d902SRodney W. Grimes 		return(1);
14329b50d902SRodney W. Grimes 	}
14339b50d902SRodney W. Grimes 	/*
14349b50d902SRodney W. Grimes 	 * posix is not clear if the header is subject to line length
14359b50d902SRodney W. Grimes 	 * restrictions. The specification for header line format
14369b50d902SRodney W. Grimes 	 * in the spec clearly does not limit length. No pr currently
14379b50d902SRodney W. Grimes 	 * restricts header length. However if we need to truncate in
14389b50d902SRodney W. Grimes 	 * an reasonable way, adjust the length of the printf by
14399b50d902SRodney W. Grimes 	 * changing HDFMT to allow a length max as an arguement printf.
14409b50d902SRodney W. Grimes 	 * buf (which contains the offset spaces and time field could
14419b50d902SRodney W. Grimes 	 * also be trimmed
14429b50d902SRodney W. Grimes 	 *
14439b50d902SRodney W. Grimes 	 * note only the offset (if any) is processed for tab expansion
14449b50d902SRodney W. Grimes 	 */
14459b50d902SRodney W. Grimes 	if (offst && otln(buf, offst, &ips, &ops, -1))
14469b50d902SRodney W. Grimes 		return(1);
14479b50d902SRodney W. Grimes 	(void)printf(HDFMT,buf+offst, fname, pagcnt);
14489b50d902SRodney W. Grimes 	return(0);
14499b50d902SRodney W. Grimes }
14509b50d902SRodney W. Grimes 
14519b50d902SRodney W. Grimes /*
14529b50d902SRodney W. Grimes  * prtail():	pad page with empty lines (if required) and print page trailer
14539b50d902SRodney W. Grimes  *		if requested
14549b50d902SRodney W. Grimes  *
14559b50d902SRodney W. Grimes  *	cnt	number of lines of padding needed
14569b50d902SRodney W. Grimes  *	incomp	was a '\n' missing from last line output
14579b50d902SRodney W. Grimes  */
14589b50d902SRodney W. Grimes int
14599b50d902SRodney W. Grimes prtail(cnt, incomp)
14609b50d902SRodney W. Grimes 	register int cnt;
14619b50d902SRodney W. Grimes 	int incomp;
14629b50d902SRodney W. Grimes {
14639b50d902SRodney W. Grimes 	if (nohead) {
14649b50d902SRodney W. Grimes 		/*
14659b50d902SRodney W. Grimes 		 * only pad with no headers when incomplete last line
14669b50d902SRodney W. Grimes 		 */
1467ebcf24fbSMarc G. Fournier 		if (incomp &&
1468ebcf24fbSMarc G. Fournier 		    ((dspace && (putchar('\n') == EOF)) ||
1469ebcf24fbSMarc G. Fournier 		     (putchar('\n') == EOF))) {
14709b50d902SRodney W. Grimes 			pfail();
14719b50d902SRodney W. Grimes 			return(1);
14729b50d902SRodney W. Grimes 		}
1473ebcf24fbSMarc G. Fournier 		/*
1474ebcf24fbSMarc G. Fournier 		 * but honor the formfeed request
1475ebcf24fbSMarc G. Fournier 		 */
1476ebcf24fbSMarc G. Fournier 		if (formfeed) {
1477ebcf24fbSMarc G. Fournier 			if (putchar('\f') == EOF) {
1478ebcf24fbSMarc G. Fournier 				pfail();
1479ebcf24fbSMarc G. Fournier 				return(1);
1480ebcf24fbSMarc G. Fournier 			}
1481ebcf24fbSMarc G. Fournier 		}
14829b50d902SRodney W. Grimes 		return(0);
14839b50d902SRodney W. Grimes 	}
14849b50d902SRodney W. Grimes 	/*
14859b50d902SRodney W. Grimes 	 * if double space output two \n
14869b50d902SRodney W. Grimes 	 */
14879b50d902SRodney W. Grimes 	if (dspace)
14889b50d902SRodney W. Grimes 		cnt *= 2;
14899b50d902SRodney W. Grimes 
14909b50d902SRodney W. Grimes 	/*
14919b50d902SRodney W. Grimes 	 * if an odd number of lines per page, add an extra \n
14929b50d902SRodney W. Grimes 	 */
14939b50d902SRodney W. Grimes 	if (addone)
14949b50d902SRodney W. Grimes 		++cnt;
14959b50d902SRodney W. Grimes 
14969b50d902SRodney W. Grimes 	/*
14979b50d902SRodney W. Grimes 	 * pad page
14989b50d902SRodney W. Grimes 	 */
14999b50d902SRodney W. Grimes 	if (formfeed) {
15009b50d902SRodney W. Grimes 		if ((incomp && (putchar('\n') == EOF)) ||
15019b50d902SRodney W. Grimes 		    (putchar('\f') == EOF)) {
15029b50d902SRodney W. Grimes 			pfail();
15039b50d902SRodney W. Grimes 			return(1);
15049b50d902SRodney W. Grimes 		}
15059b50d902SRodney W. Grimes 		return(0);
15069b50d902SRodney W. Grimes 	}
15079b50d902SRodney W. Grimes 	cnt += TAILLEN;
15089b50d902SRodney W. Grimes 	while (--cnt >= 0) {
15099b50d902SRodney W. Grimes 		if (putchar('\n') == EOF) {
15109b50d902SRodney W. Grimes 			pfail();
15119b50d902SRodney W. Grimes 			return(1);
15129b50d902SRodney W. Grimes 		}
15139b50d902SRodney W. Grimes 	}
15149b50d902SRodney W. Grimes 	return(0);
15159b50d902SRodney W. Grimes }
15169b50d902SRodney W. Grimes 
15179b50d902SRodney W. Grimes /*
15189b50d902SRodney W. Grimes  * terminate():	when a SIGINT is recvd
15199b50d902SRodney W. Grimes  */
15209b50d902SRodney W. Grimes void
15219b50d902SRodney W. Grimes terminate(which_sig)
15229b50d902SRodney W. Grimes 	int which_sig;
15239b50d902SRodney W. Grimes {
15249b50d902SRodney W. Grimes 	flsh_errs();
15259b50d902SRodney W. Grimes 	exit(1);
15269b50d902SRodney W. Grimes }
15279b50d902SRodney W. Grimes 
15289b50d902SRodney W. Grimes 
15299b50d902SRodney W. Grimes /*
15309b50d902SRodney W. Grimes  * flsh_errs():	output saved up diagnostic messages after all normal
15319b50d902SRodney W. Grimes  *		processing has completed
15329b50d902SRodney W. Grimes  */
15339b50d902SRodney W. Grimes void
15349b50d902SRodney W. Grimes flsh_errs()
15359b50d902SRodney W. Grimes {
15369b50d902SRodney W. Grimes 	char buf[BUFSIZ];
15379b50d902SRodney W. Grimes 
15389b50d902SRodney W. Grimes 	(void)fflush(stdout);
15399b50d902SRodney W. Grimes 	(void)fflush(err);
15409b50d902SRodney W. Grimes 	if (err == stderr)
15419b50d902SRodney W. Grimes 		return;
15429b50d902SRodney W. Grimes 	rewind(err);
15439b50d902SRodney W. Grimes 	while (fgets(buf, BUFSIZ, err) != NULL)
15449b50d902SRodney W. Grimes 		(void)fputs(buf, stderr);
15459b50d902SRodney W. Grimes }
15469b50d902SRodney W. Grimes 
15479b50d902SRodney W. Grimes void
15489b50d902SRodney W. Grimes mfail()
15499b50d902SRodney W. Grimes {
15509b50d902SRodney W. Grimes 	(void)fputs("pr: memory allocation failed\n", err);
15519b50d902SRodney W. Grimes }
15529b50d902SRodney W. Grimes 
15539b50d902SRodney W. Grimes void
15549b50d902SRodney W. Grimes pfail()
15559b50d902SRodney W. Grimes {
15569b50d902SRodney W. Grimes 	(void)fprintf(err, "pr: write failure, %s\n", strerror(errno));
15579b50d902SRodney W. Grimes }
15589b50d902SRodney W. Grimes 
15599b50d902SRodney W. Grimes void
15609b50d902SRodney W. Grimes usage()
15619b50d902SRodney W. Grimes {
15629b50d902SRodney W. Grimes 	(void)fputs(
15634bf9895eSAndrey A. Chernov 	 "usage: pr [+page] [-col] [-adFmrt] [-e[ch][gap]] [-h header]\n",err);
15649b50d902SRodney W. Grimes 	(void)fputs(
15659b50d902SRodney W. Grimes 	 "          [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",err);
15669b50d902SRodney W. Grimes 	(void)fputs(
15674bf9895eSAndrey A. Chernov 	 "          [-L locale] [-s[ch]] [-w width] [-] [file ...]\n", err);
15689b50d902SRodney W. Grimes }
15699b50d902SRodney W. Grimes 
15709b50d902SRodney W. Grimes /*
15719b50d902SRodney W. Grimes  * setup:	Validate command args, initialize and perform sanity
15729b50d902SRodney W. Grimes  *		checks on options
15739b50d902SRodney W. Grimes  */
15749b50d902SRodney W. Grimes int
15759b50d902SRodney W. Grimes setup(argc, argv)
15769b50d902SRodney W. Grimes 	register int argc;
15779b50d902SRodney W. Grimes 	register char **argv;
15789b50d902SRodney W. Grimes {
15799b50d902SRodney W. Grimes 	register int c;
15809b50d902SRodney W. Grimes 	int eflag = 0;
15819b50d902SRodney W. Grimes 	int iflag = 0;
15829b50d902SRodney W. Grimes 	int wflag = 0;
15839b50d902SRodney W. Grimes 	int cflag = 0;
15844bf9895eSAndrey A. Chernov 	char *Lflag = NULL;
15859b50d902SRodney W. Grimes 
15869b50d902SRodney W. Grimes 	if (isatty(fileno(stdout))) {
15879b50d902SRodney W. Grimes 		/*
15889b50d902SRodney W. Grimes 		 * defer diagnostics until processing is done
15899b50d902SRodney W. Grimes 		 */
15909b50d902SRodney W. Grimes 		if ((err = tmpfile()) == NULL) {
15919b50d902SRodney W. Grimes 		       (void)fputs("Cannot defer diagnostic messages\n",stderr);
15929b50d902SRodney W. Grimes 		       return(1);
15939b50d902SRodney W. Grimes 		}
15949b50d902SRodney W. Grimes 	} else
15959b50d902SRodney W. Grimes 		err = stderr;
15964bf9895eSAndrey A. Chernov 	while ((c = egetopt(argc, argv, "#adFmrte?h:i?L:l:n?o:s?w:")) != -1) {
15979b50d902SRodney W. Grimes 		switch (c) {
15989b50d902SRodney W. Grimes 		case '+':
15999b50d902SRodney W. Grimes 			if ((pgnm = atoi(eoptarg)) < 1) {
16009b50d902SRodney W. Grimes 			    (void)fputs("pr: +page number must be 1 or more\n",
16019b50d902SRodney W. Grimes 				err);
16029b50d902SRodney W. Grimes 			    return(1);
16039b50d902SRodney W. Grimes 			}
16049b50d902SRodney W. Grimes 			break;
16059b50d902SRodney W. Grimes 		case '-':
16069b50d902SRodney W. Grimes 			if ((clcnt = atoi(eoptarg)) < 1) {
16079b50d902SRodney W. Grimes 			    (void)fputs("pr: -columns must be 1 or more\n",err);
16089b50d902SRodney W. Grimes 			    return(1);
16099b50d902SRodney W. Grimes 			}
16109b50d902SRodney W. Grimes 			if (clcnt > 1)
16119b50d902SRodney W. Grimes 				++cflag;
16129b50d902SRodney W. Grimes 			break;
16139b50d902SRodney W. Grimes 		case 'a':
16149b50d902SRodney W. Grimes 			++across;
16159b50d902SRodney W. Grimes 			break;
16169b50d902SRodney W. Grimes 		case 'd':
16179b50d902SRodney W. Grimes 			++dspace;
16189b50d902SRodney W. Grimes 			break;
16199b50d902SRodney W. Grimes 		case 'e':
16209b50d902SRodney W. Grimes 			++eflag;
1621b5076d91SAndrey A. Chernov 			if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
16229b50d902SRodney W. Grimes 				inchar = *eoptarg++;
16239b50d902SRodney W. Grimes 			else
16249b50d902SRodney W. Grimes 				inchar = INCHAR;
1625b5076d91SAndrey A. Chernov 			if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
16269b50d902SRodney W. Grimes 				if ((ingap = atoi(eoptarg)) < 0) {
16279b50d902SRodney W. Grimes 					(void)fputs(
16289b50d902SRodney W. Grimes 					"pr: -e gap must be 0 or more\n", err);
16299b50d902SRodney W. Grimes 					return(1);
16309b50d902SRodney W. Grimes 				}
16319b50d902SRodney W. Grimes 				if (ingap == 0)
16329b50d902SRodney W. Grimes 					ingap = INGAP;
16339b50d902SRodney W. Grimes 			} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
16349b50d902SRodney W. Grimes 				(void)fprintf(err,
16359b50d902SRodney W. Grimes 				      "pr: invalid value for -e %s\n", eoptarg);
16369b50d902SRodney W. Grimes 				return(1);
16379b50d902SRodney W. Grimes 			} else
16389b50d902SRodney W. Grimes 				ingap = INGAP;
16399b50d902SRodney W. Grimes 			break;
16409b50d902SRodney W. Grimes 		case 'F':
16419b50d902SRodney W. Grimes 			++formfeed;
16429b50d902SRodney W. Grimes 			break;
16439b50d902SRodney W. Grimes 		case 'h':
16449b50d902SRodney W. Grimes 			header = eoptarg;
16459b50d902SRodney W. Grimes 			break;
16469b50d902SRodney W. Grimes 		case 'i':
16479b50d902SRodney W. Grimes 			++iflag;
1648b5076d91SAndrey A. Chernov 			if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
16499b50d902SRodney W. Grimes 				ochar = *eoptarg++;
16509b50d902SRodney W. Grimes 			else
16519b50d902SRodney W. Grimes 				ochar = OCHAR;
1652b5076d91SAndrey A. Chernov 			if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
16539b50d902SRodney W. Grimes 				if ((ogap = atoi(eoptarg)) < 0) {
16549b50d902SRodney W. Grimes 					(void)fputs(
16559b50d902SRodney W. Grimes 					"pr: -i gap must be 0 or more\n", err);
16569b50d902SRodney W. Grimes 					return(1);
16579b50d902SRodney W. Grimes 				}
16589b50d902SRodney W. Grimes 				if (ogap == 0)
16599b50d902SRodney W. Grimes 					ogap = OGAP;
16609b50d902SRodney W. Grimes 			} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
16619b50d902SRodney W. Grimes 				(void)fprintf(err,
16629b50d902SRodney W. Grimes 				      "pr: invalid value for -i %s\n", eoptarg);
16639b50d902SRodney W. Grimes 				return(1);
16649b50d902SRodney W. Grimes 			} else
16659b50d902SRodney W. Grimes 				ogap = OGAP;
16669b50d902SRodney W. Grimes 			break;
1667b5076d91SAndrey A. Chernov 		case 'L':
16684bf9895eSAndrey A. Chernov 			Lflag = eoptarg;
1669b5076d91SAndrey A. Chernov 			break;
16709b50d902SRodney W. Grimes 		case 'l':
1671b5076d91SAndrey A. Chernov 			if (!isdigit((unsigned char)*eoptarg) || ((lines=atoi(eoptarg)) < 1)) {
16729b50d902SRodney W. Grimes 				(void)fputs(
16739b50d902SRodney W. Grimes 				 "pr: Number of lines must be 1 or more\n",err);
16749b50d902SRodney W. Grimes 				return(1);
16759b50d902SRodney W. Grimes 			}
16769b50d902SRodney W. Grimes 			break;
16779b50d902SRodney W. Grimes 		case 'm':
16789b50d902SRodney W. Grimes 			++merge;
16799b50d902SRodney W. Grimes 			break;
16809b50d902SRodney W. Grimes 		case 'n':
1681b5076d91SAndrey A. Chernov 			if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
16829b50d902SRodney W. Grimes 				nmchar = *eoptarg++;
16839b50d902SRodney W. Grimes 			else
16849b50d902SRodney W. Grimes 				nmchar = NMCHAR;
1685b5076d91SAndrey A. Chernov 			if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
16869b50d902SRodney W. Grimes 				if ((nmwd = atoi(eoptarg)) < 1) {
16879b50d902SRodney W. Grimes 					(void)fputs(
16889b50d902SRodney W. Grimes 					"pr: -n width must be 1 or more\n",err);
16899b50d902SRodney W. Grimes 					return(1);
16909b50d902SRodney W. Grimes 				}
16919b50d902SRodney W. Grimes 			} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
16929b50d902SRodney W. Grimes 				(void)fprintf(err,
16939b50d902SRodney W. Grimes 				      "pr: invalid value for -n %s\n", eoptarg);
16949b50d902SRodney W. Grimes 				return(1);
16959b50d902SRodney W. Grimes 			} else
16969b50d902SRodney W. Grimes 				nmwd = NMWD;
16979b50d902SRodney W. Grimes 			break;
16989b50d902SRodney W. Grimes 		case 'o':
1699b5076d91SAndrey A. Chernov 			if (!isdigit((unsigned char)*eoptarg) || ((offst = atoi(eoptarg))< 1)){
17009b50d902SRodney W. Grimes 				(void)fputs("pr: -o offset must be 1 or more\n",
17019b50d902SRodney W. Grimes 					err);
17029b50d902SRodney W. Grimes 				return(1);
17039b50d902SRodney W. Grimes 			}
17049b50d902SRodney W. Grimes 			break;
17059b50d902SRodney W. Grimes 		case 'r':
17069b50d902SRodney W. Grimes 			++nodiag;
17079b50d902SRodney W. Grimes 			break;
17089b50d902SRodney W. Grimes 		case 's':
17099b50d902SRodney W. Grimes 			++sflag;
17109b50d902SRodney W. Grimes 			if (eoptarg == NULL)
17119b50d902SRodney W. Grimes 				schar = SCHAR;
1712d68e2acdSKris Kennaway 			else {
17139b50d902SRodney W. Grimes 				schar = *eoptarg++;
17149b50d902SRodney W. Grimes 				if (*eoptarg != '\0') {
17159b50d902SRodney W. Grimes 					(void)fprintf(err,
1716d68e2acdSKris Kennaway 					    "pr: invalid value for -s %s\n",
1717d68e2acdSKris Kennaway 					    eoptarg);
17189b50d902SRodney W. Grimes 					return(1);
17199b50d902SRodney W. Grimes 				}
1720d68e2acdSKris Kennaway 			}
17219b50d902SRodney W. Grimes 			break;
17229b50d902SRodney W. Grimes 		case 't':
17239b50d902SRodney W. Grimes 			++nohead;
17249b50d902SRodney W. Grimes 			break;
17259b50d902SRodney W. Grimes 		case 'w':
17269b50d902SRodney W. Grimes 			++wflag;
1727b5076d91SAndrey A. Chernov 			if (!isdigit((unsigned char)*eoptarg) || ((pgwd = atoi(eoptarg)) < 1)){
17289b50d902SRodney W. Grimes 				(void)fputs(
17299b50d902SRodney W. Grimes 				   "pr: -w width must be 1 or more \n",err);
17309b50d902SRodney W. Grimes 				return(1);
17319b50d902SRodney W. Grimes 			}
17329b50d902SRodney W. Grimes 			break;
17339b50d902SRodney W. Grimes 		case '?':
17349b50d902SRodney W. Grimes 		default:
17359b50d902SRodney W. Grimes 			return(1);
17369b50d902SRodney W. Grimes 		}
17379b50d902SRodney W. Grimes 	}
17389b50d902SRodney W. Grimes 
17399b50d902SRodney W. Grimes 	/*
17409b50d902SRodney W. Grimes 	 * default and sanity checks
17419b50d902SRodney W. Grimes 	 */
17429b50d902SRodney W. Grimes 	if (!clcnt) {
17439b50d902SRodney W. Grimes 		if (merge) {
17449b50d902SRodney W. Grimes 			if ((clcnt = argc - eoptind) <= 1) {
17459b50d902SRodney W. Grimes 				clcnt = CLCNT;
17469b50d902SRodney W. Grimes 				merge = 0;
17479b50d902SRodney W. Grimes 			}
17489b50d902SRodney W. Grimes 		} else
17499b50d902SRodney W. Grimes 			clcnt = CLCNT;
17509b50d902SRodney W. Grimes 	}
17519b50d902SRodney W. Grimes 	if (across) {
17529b50d902SRodney W. Grimes 		if (clcnt == 1) {
17539b50d902SRodney W. Grimes 			(void)fputs("pr: -a flag requires multiple columns\n",
17549b50d902SRodney W. Grimes 				err);
17559b50d902SRodney W. Grimes 			return(1);
17569b50d902SRodney W. Grimes 		}
17579b50d902SRodney W. Grimes 		if (merge) {
17589b50d902SRodney W. Grimes 			(void)fputs("pr: -m cannot be used with -a\n", err);
17599b50d902SRodney W. Grimes 			return(1);
17609b50d902SRodney W. Grimes 		}
17619b50d902SRodney W. Grimes 	}
17629b50d902SRodney W. Grimes 	if (!wflag) {
17639b50d902SRodney W. Grimes 		if (sflag)
17649b50d902SRodney W. Grimes 			pgwd = SPGWD;
17659b50d902SRodney W. Grimes 		else
17669b50d902SRodney W. Grimes 			pgwd = PGWD;
17679b50d902SRodney W. Grimes 	}
17689b50d902SRodney W. Grimes 	if (cflag || merge) {
17699b50d902SRodney W. Grimes 		if (!eflag) {
17709b50d902SRodney W. Grimes 			inchar = INCHAR;
17719b50d902SRodney W. Grimes 			ingap = INGAP;
17729b50d902SRodney W. Grimes 		}
17739b50d902SRodney W. Grimes 		if (!iflag) {
17749b50d902SRodney W. Grimes 			ochar = OCHAR;
17759b50d902SRodney W. Grimes 			ogap = OGAP;
17769b50d902SRodney W. Grimes 		}
17779b50d902SRodney W. Grimes 	}
17789b50d902SRodney W. Grimes 	if (cflag) {
17799b50d902SRodney W. Grimes 		if (merge) {
17809b50d902SRodney W. Grimes 			(void)fputs(
17819b50d902SRodney W. Grimes 			  "pr: -m cannot be used with multiple columns\n", err);
17829b50d902SRodney W. Grimes 			return(1);
17839b50d902SRodney W. Grimes 		}
17849b50d902SRodney W. Grimes 		if (nmwd) {
17859b50d902SRodney W. Grimes 			colwd = (pgwd + 1 - (clcnt * (nmwd + 2)))/clcnt;
17869b50d902SRodney W. Grimes 			pgwd = ((colwd + nmwd + 2) * clcnt) - 1;
17879b50d902SRodney W. Grimes 		} else {
17889b50d902SRodney W. Grimes 			colwd = (pgwd + 1 - clcnt)/clcnt;
17899b50d902SRodney W. Grimes 			pgwd = ((colwd + 1) * clcnt) - 1;
17909b50d902SRodney W. Grimes 		}
17919b50d902SRodney W. Grimes 		if (colwd < 1) {
17929b50d902SRodney W. Grimes 			(void)fprintf(err,
17939b50d902SRodney W. Grimes 			  "pr: page width is too small for %d columns\n",clcnt);
17949b50d902SRodney W. Grimes 			return(1);
17959b50d902SRodney W. Grimes 		}
17969b50d902SRodney W. Grimes 	}
17979b50d902SRodney W. Grimes 	if (!lines)
17989b50d902SRodney W. Grimes 		lines = LINES;
17999b50d902SRodney W. Grimes 
18009b50d902SRodney W. Grimes 	/*
18019b50d902SRodney W. Grimes 	 * make sure long enough for headers. if not disable
18029b50d902SRodney W. Grimes 	 */
18039b50d902SRodney W. Grimes 	if (lines <= HEADLEN + TAILLEN)
18049b50d902SRodney W. Grimes 		++nohead;
18059b50d902SRodney W. Grimes 	else if (!nohead)
18069b50d902SRodney W. Grimes 		lines -= HEADLEN + TAILLEN;
18079b50d902SRodney W. Grimes 
18089b50d902SRodney W. Grimes 	/*
18099b50d902SRodney W. Grimes 	 * adjust for double space on odd length pages
18109b50d902SRodney W. Grimes 	 */
18119b50d902SRodney W. Grimes 	if (dspace) {
18129b50d902SRodney W. Grimes 		if (lines == 1)
18139b50d902SRodney W. Grimes 			dspace = 0;
18149b50d902SRodney W. Grimes 		else {
18159b50d902SRodney W. Grimes 			if (lines & 1)
18169b50d902SRodney W. Grimes 				++addone;
18179b50d902SRodney W. Grimes 			lines /= 2;
18189b50d902SRodney W. Grimes 		}
18199b50d902SRodney W. Grimes 	}
18209b50d902SRodney W. Grimes 
18219b50d902SRodney W. Grimes 	timefrmt = TIMEFMT;
18224bf9895eSAndrey A. Chernov 	(void) setlocale(LC_TIME, (Lflag != NULL) ? Lflag : "");
1823b5076d91SAndrey A. Chernov 
18249b50d902SRodney W. Grimes 	return(0);
18259b50d902SRodney W. Grimes }
1826