xref: /titanic_50/usr/src/cmd/pr/pr.c (revision a77d64af7813dad3dad148a9974e0ec7b80d1f43)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
297c478bd9Sstevel@tonic-gate  *	All Rights Reserved
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
347c478bd9Sstevel@tonic-gate  * The Regents of the University of California
357c478bd9Sstevel@tonic-gate  * All Rights Reserved
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
387c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
397c478bd9Sstevel@tonic-gate  * contributors.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  *	PR command (print files in pages and columns, with headings)
467c478bd9Sstevel@tonic-gate  *	2+head+2+page[56]+5
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <signal.h>
517c478bd9Sstevel@tonic-gate #include <ctype.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/stat.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
557c478bd9Sstevel@tonic-gate #include <stdlib.h>
567c478bd9Sstevel@tonic-gate #include <locale.h>
577c478bd9Sstevel@tonic-gate #include <string.h>
587c478bd9Sstevel@tonic-gate #include <limits.h>
597c478bd9Sstevel@tonic-gate #include <wchar.h>
607c478bd9Sstevel@tonic-gate #include <errno.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	ESC		'\033'
637c478bd9Sstevel@tonic-gate #define	LENGTH		66
647c478bd9Sstevel@tonic-gate #define	LINEW		72
657c478bd9Sstevel@tonic-gate #define	NUMW		5
667c478bd9Sstevel@tonic-gate #define	MARGIN		10
677c478bd9Sstevel@tonic-gate #define	DEFTAB		8
687c478bd9Sstevel@tonic-gate #define	NFILES		10
697c478bd9Sstevel@tonic-gate #define	STDINNAME()	nulls
707c478bd9Sstevel@tonic-gate #define	PROMPT()	(void) putc('\7', stderr) /* BEL */
717c478bd9Sstevel@tonic-gate #define	NOFILE		nulls
727c478bd9Sstevel@tonic-gate #define	ETABS		(Inpos % Etabn)
737c478bd9Sstevel@tonic-gate #define	NSEPC		'\t'
747c478bd9Sstevel@tonic-gate #define	HEAD		gettext("%s  %s Page %d\n\n\n"), date, head, Page
757c478bd9Sstevel@tonic-gate #define	cerror(S)	(void) fprintf(stderr, "pr: %s", gettext(S))
767c478bd9Sstevel@tonic-gate #define	done()		if (Ttyout) (void) chmod(Ttyout, Mode)
777c478bd9Sstevel@tonic-gate #define	ALL_NUMS(s)	(strspn(s, "0123456789") == strlen(s))
787c478bd9Sstevel@tonic-gate #define	REMOVE_ARG(argc, argp)					\
797c478bd9Sstevel@tonic-gate 			{					\
807c478bd9Sstevel@tonic-gate 				char	**p = argp;		\
817c478bd9Sstevel@tonic-gate 				while (*p != NULL)		\
827c478bd9Sstevel@tonic-gate 				{				\
837c478bd9Sstevel@tonic-gate 					*p = *(p + 1);		\
847c478bd9Sstevel@tonic-gate 					p++;			\
857c478bd9Sstevel@tonic-gate 				}				\
867c478bd9Sstevel@tonic-gate 				argc--;				\
877c478bd9Sstevel@tonic-gate 			}
887c478bd9Sstevel@tonic-gate #define	SQUEEZE_ARG(argp, ind, n)					\
897c478bd9Sstevel@tonic-gate 			{					\
907c478bd9Sstevel@tonic-gate 				int	i;			\
917c478bd9Sstevel@tonic-gate 				for (i = ind; argp[i]; i++)	\
927c478bd9Sstevel@tonic-gate 					argp[i] = argp[i + n];	\
937c478bd9Sstevel@tonic-gate 			}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  *   ---date time format---
977c478bd9Sstevel@tonic-gate  *   b -- abbreviated month name
987c478bd9Sstevel@tonic-gate  *   e -- day of month
997c478bd9Sstevel@tonic-gate  *   H -- Hour (24 hour version)
1007c478bd9Sstevel@tonic-gate  *   M -- Minute
1017c478bd9Sstevel@tonic-gate  *   Y -- Year in the form ccyy
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate #define	FORMAT		"%b %e %H:%M %Y"
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate typedef	int	ANY;
1067c478bd9Sstevel@tonic-gate typedef	unsigned	int	UNS;
1077c478bd9Sstevel@tonic-gate typedef	struct	{ FILE *f_f; char *f_name; wchar_t f_nextc; } FILS;
1087c478bd9Sstevel@tonic-gate typedef	struct	{int fold; int skip; int eof; } foldinf;
1097c478bd9Sstevel@tonic-gate typedef	struct	{ wchar_t *c_ptr, *c_ptr0; long c_lno; int c_skip; } *COLP;
1107c478bd9Sstevel@tonic-gate typedef struct	err { struct err *e_nextp; char *e_mess; } ERR;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Global data.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate static	FILS	*Files;
1167c478bd9Sstevel@tonic-gate static	mode_t	Mode;
1177c478bd9Sstevel@tonic-gate static	int	Multi = 0;
1187c478bd9Sstevel@tonic-gate static	int	Nfiles = 0;
1197c478bd9Sstevel@tonic-gate static	int	Error = 0;
1207c478bd9Sstevel@tonic-gate static	char	nulls[] = "";
1217c478bd9Sstevel@tonic-gate static	char	*Ttyout;
1227c478bd9Sstevel@tonic-gate static	char	obuf[BUFSIZ];
1237c478bd9Sstevel@tonic-gate static	char	time_buf[50];	/* array to hold the time and date */
1247c478bd9Sstevel@tonic-gate static	long	Lnumb = 0;
1257c478bd9Sstevel@tonic-gate static	FILE	*Ttyin = stdin;
1267c478bd9Sstevel@tonic-gate static	int	Dblspace = 1;
1277c478bd9Sstevel@tonic-gate static	int	Fpage = 1;
1287c478bd9Sstevel@tonic-gate static	int	Formfeed = 0;
1297c478bd9Sstevel@tonic-gate static	int	Length = LENGTH;
1307c478bd9Sstevel@tonic-gate static	int	Linew = 0;
1317c478bd9Sstevel@tonic-gate static	int	Offset = 0;
1327c478bd9Sstevel@tonic-gate static	int	Ncols = 0;
1337c478bd9Sstevel@tonic-gate static	int	Pause = 0;
1347c478bd9Sstevel@tonic-gate static	wchar_t	Sepc = 0;
1357c478bd9Sstevel@tonic-gate static	int	Colw;
1367c478bd9Sstevel@tonic-gate static	int	Plength;
1377c478bd9Sstevel@tonic-gate static	int	Margin = MARGIN;
1387c478bd9Sstevel@tonic-gate static	int	Numw;
1397c478bd9Sstevel@tonic-gate static	int	Nsepc = NSEPC;
1407c478bd9Sstevel@tonic-gate static	int	Report = 1;
1417c478bd9Sstevel@tonic-gate static	int	Etabn = 0;
1427c478bd9Sstevel@tonic-gate static	wchar_t	Etabc = '\t';
1437c478bd9Sstevel@tonic-gate static	int	Itabn = 0;
1447c478bd9Sstevel@tonic-gate static	wchar_t	Itabc = '\t';
1457c478bd9Sstevel@tonic-gate static	int	fold = 0;
1467c478bd9Sstevel@tonic-gate static	int	foldcol = 0;
1477c478bd9Sstevel@tonic-gate static	int	alleof = 0;
1487c478bd9Sstevel@tonic-gate static	char	*Head = NULL;
1497c478bd9Sstevel@tonic-gate static	wchar_t *Buffer = NULL, *Bufend, *Bufptr;
1507c478bd9Sstevel@tonic-gate static	UNS	Buflen;
1517c478bd9Sstevel@tonic-gate static	COLP	Colpts;
1527c478bd9Sstevel@tonic-gate static	foldinf	*Fcol;
1537c478bd9Sstevel@tonic-gate static	int	Page;
1547c478bd9Sstevel@tonic-gate static	wchar_t	C = '\0';
1557c478bd9Sstevel@tonic-gate static	int	Nspace;
1567c478bd9Sstevel@tonic-gate static	int	Inpos;
1577c478bd9Sstevel@tonic-gate static	int	Outpos;
1587c478bd9Sstevel@tonic-gate static	int	Lcolpos;
1597c478bd9Sstevel@tonic-gate static	int	Pcolpos;
1607c478bd9Sstevel@tonic-gate static	int	Line;
1617c478bd9Sstevel@tonic-gate static	ERR	*Err = NULL;
1627c478bd9Sstevel@tonic-gate static	ERR	*Lasterr = (ERR *)&Err;
1637c478bd9Sstevel@tonic-gate static	int	mbcurmax = 1;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Function prototypes.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate static	void	onintr();
1697c478bd9Sstevel@tonic-gate static	ANY	*getspace();
1707c478bd9Sstevel@tonic-gate static	int	findopt(int, char **);
1717c478bd9Sstevel@tonic-gate static	void	fixtty();
1727c478bd9Sstevel@tonic-gate static	char 	*GETDATE();
1737c478bd9Sstevel@tonic-gate static	char	*ffiler(char *);
1747c478bd9Sstevel@tonic-gate static	int	print(char *);
1757c478bd9Sstevel@tonic-gate static	void	putpage();
1767c478bd9Sstevel@tonic-gate static	void	foldpage();
1777c478bd9Sstevel@tonic-gate static	void	nexbuf();
1787c478bd9Sstevel@tonic-gate static	void	foldbuf();
1797c478bd9Sstevel@tonic-gate static	void	balance(int);
1807c478bd9Sstevel@tonic-gate static	int	readbuf(wchar_t **, int, COLP);
1817c478bd9Sstevel@tonic-gate static	wint_t	get(int);
1827c478bd9Sstevel@tonic-gate static	int	put(wchar_t);
1837c478bd9Sstevel@tonic-gate static	void	putspace();
1847c478bd9Sstevel@tonic-gate static	void	unget(int);
1857c478bd9Sstevel@tonic-gate static	FILE	*mustopen(char *, FILS *);
1867c478bd9Sstevel@tonic-gate static	void	die(char *);
1877c478bd9Sstevel@tonic-gate static	void	errprint();
1887c478bd9Sstevel@tonic-gate static	void	usage(int);
1897c478bd9Sstevel@tonic-gate static wint_t	_fgetwc_pr(FILE *, int *);
1907c478bd9Sstevel@tonic-gate static size_t	freadw(wchar_t *, size_t, FILE *);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 
193*a77d64afScf46844 int
1947c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	FILS	fstr[NFILES];
1977c478bd9Sstevel@tonic-gate 	int	nfdone = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* Get locale variables for environment */
2017c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2047c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	mbcurmax = MB_CUR_MAX;
2097c478bd9Sstevel@tonic-gate 	Files = fstr;
2107c478bd9Sstevel@tonic-gate 	for (argc = findopt(argc, argv); argc > 0; --argc, ++argv) {
2117c478bd9Sstevel@tonic-gate 		if (Multi == 'm') {
2127c478bd9Sstevel@tonic-gate 			if (Nfiles >= NFILES - 1) die("too many files");
2137c478bd9Sstevel@tonic-gate 			if (mustopen(*argv, &Files[Nfiles++]) == NULL)
2147c478bd9Sstevel@tonic-gate 				++nfdone;	/* suppress printing */
2157c478bd9Sstevel@tonic-gate 		} else {
2167c478bd9Sstevel@tonic-gate 			if (print(*argv))
2177c478bd9Sstevel@tonic-gate 				(void) fclose(Files->f_f);
2187c478bd9Sstevel@tonic-gate 			++nfdone;
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	if (!nfdone)	/* no files named, use stdin */
2227c478bd9Sstevel@tonic-gate 		(void) print(NOFILE);	/* on GCOS, use current file, if any */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (Report) {
2257c478bd9Sstevel@tonic-gate 		errprint();	/* print accumulated error reports */
2267c478bd9Sstevel@tonic-gate 		exit(Error);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (Error);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * findopt() returns argc modified to be the number of explicitly supplied
2357c478bd9Sstevel@tonic-gate  * filenames, including '-', the explicit request to use stdin.
2367c478bd9Sstevel@tonic-gate  * argc == 0 implies that no filenames were supplied and stdin should be used.
2377c478bd9Sstevel@tonic-gate  * Options are striped from argv and only file names are returned.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static	int
2417c478bd9Sstevel@tonic-gate findopt(int argc, char **argv)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	int	eargc = 0;
2447c478bd9Sstevel@tonic-gate 	int	c;
2457c478bd9Sstevel@tonic-gate 	int	mflg = 0;
2467c478bd9Sstevel@tonic-gate 	int	aflg = 0;
2477c478bd9Sstevel@tonic-gate 	int	optnum;
2487c478bd9Sstevel@tonic-gate 	int	argv_ind;
2497c478bd9Sstevel@tonic-gate 	int	end_opt;
2507c478bd9Sstevel@tonic-gate 	int	i;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	fixtty();
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/* Handle page number option */
2557c478bd9Sstevel@tonic-gate 	for (optnum = 1, end_opt = 0; optnum < argc && !end_opt; optnum++) {
2567c478bd9Sstevel@tonic-gate 		switch (*argv[optnum]) {
2577c478bd9Sstevel@tonic-gate 		case '+':
2587c478bd9Sstevel@tonic-gate 			/* check for all digits */
2597c478bd9Sstevel@tonic-gate 			if (strlen(&argv[optnum][1]) !=
2607c478bd9Sstevel@tonic-gate 			    strspn(&argv[optnum][1], "0123456789")) {
2617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2627c478bd9Sstevel@tonic-gate 				    "pr: Badly formed number\n"));
2637c478bd9Sstevel@tonic-gate 				exit(1);
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 			if ((Fpage = (int)strtol(&argv[optnum][1],
2677c478bd9Sstevel@tonic-gate 			    (char **)NULL, 10)) < 0) {
2687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2697c478bd9Sstevel@tonic-gate 				    "pr: Badly formed number\n"));
2707c478bd9Sstevel@tonic-gate 				exit(1);
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 			REMOVE_ARG(argc, &argv[optnum]);
2737c478bd9Sstevel@tonic-gate 			optnum--;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		case '-':
2777c478bd9Sstevel@tonic-gate 			/* Check for end of options */
2787c478bd9Sstevel@tonic-gate 			if (argv[optnum][1] == '-') {
2797c478bd9Sstevel@tonic-gate 				end_opt++;
2807c478bd9Sstevel@tonic-gate 				break;
2817c478bd9Sstevel@tonic-gate 			}
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		default:
2857c478bd9Sstevel@tonic-gate 			end_opt++;
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Handle options with optional arguments.
2927c478bd9Sstevel@tonic-gate 	 * If optional arguments are present they may not be separated
2937c478bd9Sstevel@tonic-gate 	 * from the option letter.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	for (optnum = 1; optnum < argc; optnum++) {
2977c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] == '-' && argv[optnum][1] == '-')
2987c478bd9Sstevel@tonic-gate 			/* End of options */
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0')
3027c478bd9Sstevel@tonic-gate 			/* stdin file name */
3037c478bd9Sstevel@tonic-gate 			continue;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] != '-')
3067c478bd9Sstevel@tonic-gate 			/* not option */
3077c478bd9Sstevel@tonic-gate 			continue;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		for (argv_ind = 1; argv[optnum][argv_ind] != '\0'; argv_ind++) {
3107c478bd9Sstevel@tonic-gate 			switch (argv[optnum][argv_ind]) {
3117c478bd9Sstevel@tonic-gate 			case 'e':
3127c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3137c478bd9Sstevel@tonic-gate 				if ((c = argv[optnum][argv_ind]) != '\0' &&
3147c478bd9Sstevel@tonic-gate 				    !isdigit(c)) {
3157c478bd9Sstevel@tonic-gate 					int	r;
3167c478bd9Sstevel@tonic-gate 					wchar_t	wc;
3177c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
3187c478bd9Sstevel@tonic-gate 						mbcurmax);
3197c478bd9Sstevel@tonic-gate 					if (r == -1) {
3207c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
3217c478bd9Sstevel@tonic-gate "pr: Illegal character in -e option\n"));
3227c478bd9Sstevel@tonic-gate 						exit(1);
3237c478bd9Sstevel@tonic-gate 					}
3247c478bd9Sstevel@tonic-gate 					Etabc = wc;
3257c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
3267c478bd9Sstevel@tonic-gate 				}
3277c478bd9Sstevel@tonic-gate 				if (isdigit(argv[optnum][argv_ind])) {
3287c478bd9Sstevel@tonic-gate 					Etabn = (int)strtol(&argv[optnum]
3297c478bd9Sstevel@tonic-gate 					    [argv_ind], (char **)NULL, 10);
3307c478bd9Sstevel@tonic-gate 					while (isdigit(argv[optnum][argv_ind]))
3317c478bd9Sstevel@tonic-gate 					    SQUEEZE_ARG(argv[optnum],
3327c478bd9Sstevel@tonic-gate 							argv_ind, 1);
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 				if (Etabn <= 0)
3357c478bd9Sstevel@tonic-gate 					Etabn = DEFTAB;
3367c478bd9Sstevel@tonic-gate 				argv_ind--;
3377c478bd9Sstevel@tonic-gate 				break;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			case 'i':
3407c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3417c478bd9Sstevel@tonic-gate 				if ((c = argv[optnum][argv_ind]) != '\0' &&
3427c478bd9Sstevel@tonic-gate 				    !isdigit(c)) {
3437c478bd9Sstevel@tonic-gate 					int	r;
3447c478bd9Sstevel@tonic-gate 					wchar_t	wc;
3457c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
3467c478bd9Sstevel@tonic-gate 						mbcurmax);
3477c478bd9Sstevel@tonic-gate 					if (r == -1) {
3487c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
3497c478bd9Sstevel@tonic-gate "pr: Illegal character in -i option\n"));
3507c478bd9Sstevel@tonic-gate 						exit(1);
3517c478bd9Sstevel@tonic-gate 					}
3527c478bd9Sstevel@tonic-gate 					Itabc = wc;
3537c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
3547c478bd9Sstevel@tonic-gate 				}
3557c478bd9Sstevel@tonic-gate 				if (isdigit(argv[optnum][argv_ind])) {
3567c478bd9Sstevel@tonic-gate 					Itabn = (int)strtol(&argv[optnum]
3577c478bd9Sstevel@tonic-gate 					    [argv_ind], (char **)NULL, 10);
3587c478bd9Sstevel@tonic-gate 					while (isdigit(argv[optnum][argv_ind]))
3597c478bd9Sstevel@tonic-gate 					    SQUEEZE_ARG(argv[optnum],
3607c478bd9Sstevel@tonic-gate 							argv_ind, 1);
3617c478bd9Sstevel@tonic-gate 				}
3627c478bd9Sstevel@tonic-gate 				if (Itabn <= 0)
3637c478bd9Sstevel@tonic-gate 					Itabn = DEFTAB;
3647c478bd9Sstevel@tonic-gate 				argv_ind--;
3657c478bd9Sstevel@tonic-gate 				break;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			case 'n':
3697c478bd9Sstevel@tonic-gate 				++Lnumb;
3707c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3717c478bd9Sstevel@tonic-gate 				if ((c = argv[optnum][argv_ind]) != '\0' &&
3727c478bd9Sstevel@tonic-gate 				    !isdigit(c)) {
3737c478bd9Sstevel@tonic-gate 					int	r;
3747c478bd9Sstevel@tonic-gate 					wchar_t	wc;
3757c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
3767c478bd9Sstevel@tonic-gate 						mbcurmax);
3777c478bd9Sstevel@tonic-gate 					if (r == -1) {
3787c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
3797c478bd9Sstevel@tonic-gate "pr: Illegal character in -n option\n"));
3807c478bd9Sstevel@tonic-gate 						exit(1);
3817c478bd9Sstevel@tonic-gate 					}
3827c478bd9Sstevel@tonic-gate 					Nsepc = wc;
3837c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
3847c478bd9Sstevel@tonic-gate 				}
3857c478bd9Sstevel@tonic-gate 				if (isdigit(argv[optnum][argv_ind])) {
3867c478bd9Sstevel@tonic-gate 					Numw = (int)strtol(&argv[optnum]
3877c478bd9Sstevel@tonic-gate 					    [argv_ind], (char **)NULL, 10);
3887c478bd9Sstevel@tonic-gate 					while (isdigit(argv[optnum][argv_ind]))
3897c478bd9Sstevel@tonic-gate 					    SQUEEZE_ARG(argv[optnum],
3907c478bd9Sstevel@tonic-gate 							argv_ind, 1);
3917c478bd9Sstevel@tonic-gate 				}
3927c478bd9Sstevel@tonic-gate 				argv_ind--;
3937c478bd9Sstevel@tonic-gate 				if (!Numw)
3947c478bd9Sstevel@tonic-gate 					Numw = NUMW;
3957c478bd9Sstevel@tonic-gate 				break;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 			case 's':
3987c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3997c478bd9Sstevel@tonic-gate 				if ((Sepc = argv[optnum][argv_ind]) == '\0')
4007c478bd9Sstevel@tonic-gate 					Sepc = '\t';
4017c478bd9Sstevel@tonic-gate 				else {
4027c478bd9Sstevel@tonic-gate 					int	r;
4037c478bd9Sstevel@tonic-gate 					wchar_t	wc;
4047c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
4057c478bd9Sstevel@tonic-gate 						mbcurmax);
4067c478bd9Sstevel@tonic-gate 					if (r == -1) {
4077c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
4087c478bd9Sstevel@tonic-gate "pr: Illegal character in -s option\n"));
4097c478bd9Sstevel@tonic-gate 						exit(1);
4107c478bd9Sstevel@tonic-gate 					}
4117c478bd9Sstevel@tonic-gate 					Sepc = wc;
4127c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
4137c478bd9Sstevel@tonic-gate 				}
4147c478bd9Sstevel@tonic-gate 				argv_ind--;
4157c478bd9Sstevel@tonic-gate 				break;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 			default:
4187c478bd9Sstevel@tonic-gate 				break;
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0') {
4227c478bd9Sstevel@tonic-gate 			REMOVE_ARG(argc, &argv[optnum]);
4237c478bd9Sstevel@tonic-gate 			optnum--;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/* Now get the other options */
4287c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "0123456789adfFh:l:mo:prtw:"))
4297c478bd9Sstevel@tonic-gate 	    != EOF) {
4307c478bd9Sstevel@tonic-gate 		switch (c) {
4317c478bd9Sstevel@tonic-gate 		case '0':
4327c478bd9Sstevel@tonic-gate 		case '1':
4337c478bd9Sstevel@tonic-gate 		case '2':
4347c478bd9Sstevel@tonic-gate 		case '3':
4357c478bd9Sstevel@tonic-gate 		case '4':
4367c478bd9Sstevel@tonic-gate 		case '5':
4377c478bd9Sstevel@tonic-gate 		case '6':
4387c478bd9Sstevel@tonic-gate 		case '7':
4397c478bd9Sstevel@tonic-gate 		case '8':
4407c478bd9Sstevel@tonic-gate 		case '9':
4417c478bd9Sstevel@tonic-gate 			Ncols *= 10;
4427c478bd9Sstevel@tonic-gate 			Ncols += c - '0';
4437c478bd9Sstevel@tonic-gate 			break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		case 'a':
4467c478bd9Sstevel@tonic-gate 			aflg++;
4477c478bd9Sstevel@tonic-gate 			if (!Multi)
4487c478bd9Sstevel@tonic-gate 				Multi = c;
4497c478bd9Sstevel@tonic-gate 			break;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		case 'd':
4527c478bd9Sstevel@tonic-gate 			Dblspace = 2;
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		case 'f':
4567c478bd9Sstevel@tonic-gate 			++Formfeed;
4577c478bd9Sstevel@tonic-gate 			++Pause;
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		case 'h':
4617c478bd9Sstevel@tonic-gate 			Head = optarg;
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		case 'l':
4657c478bd9Sstevel@tonic-gate 			if (strlen(optarg) != strspn(optarg, "0123456789"))
4667c478bd9Sstevel@tonic-gate 				usage(1);
4677c478bd9Sstevel@tonic-gate 			Length = (int)strtol(optarg, (char **)NULL, 10);
4687c478bd9Sstevel@tonic-gate 			break;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		case 'm':
4717c478bd9Sstevel@tonic-gate 			mflg++;
4727c478bd9Sstevel@tonic-gate 			Multi = c;
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		case 'o':
4767c478bd9Sstevel@tonic-gate 			if (strlen(optarg) != strspn(optarg, "0123456789"))
4777c478bd9Sstevel@tonic-gate 				usage(1);
4787c478bd9Sstevel@tonic-gate 			Offset = (int)strtol(optarg, (char **)NULL, 10);
4797c478bd9Sstevel@tonic-gate 			break;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		case 'p':
4827c478bd9Sstevel@tonic-gate 			++Pause;
4837c478bd9Sstevel@tonic-gate 			break;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		case 'r':
4867c478bd9Sstevel@tonic-gate 			Report = 0;
4877c478bd9Sstevel@tonic-gate 			break;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		case 't':
4907c478bd9Sstevel@tonic-gate 			Margin = 0;
4917c478bd9Sstevel@tonic-gate 			break;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		case 'w':
4947c478bd9Sstevel@tonic-gate 			if (strlen(optarg) != strspn(optarg, "0123456789"))
4957c478bd9Sstevel@tonic-gate 				usage(1);
4967c478bd9Sstevel@tonic-gate 			Linew = (int)strtol(optarg, (char **)NULL, 10);
4977c478bd9Sstevel@tonic-gate 			break;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		case 'F':
5007c478bd9Sstevel@tonic-gate #ifdef	XPG4
5017c478bd9Sstevel@tonic-gate 			++Formfeed;
5027c478bd9Sstevel@tonic-gate #else
5037c478bd9Sstevel@tonic-gate 			fold++;
5047c478bd9Sstevel@tonic-gate #endif
5057c478bd9Sstevel@tonic-gate 			break;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		case '?':
5087c478bd9Sstevel@tonic-gate 			usage(2);
5097c478bd9Sstevel@tonic-gate 			break;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		default :
5127c478bd9Sstevel@tonic-gate 			usage(2);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/* Count the file names and strip options */
5177c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
5187c478bd9Sstevel@tonic-gate 		/* Check for explicit stdin */
5197c478bd9Sstevel@tonic-gate 		if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
5207c478bd9Sstevel@tonic-gate 			argv[eargc++][0] = '\0';
5217c478bd9Sstevel@tonic-gate 			REMOVE_ARG(argc, &argv[i]);
5227c478bd9Sstevel@tonic-gate 			if (i < optind)
5237c478bd9Sstevel@tonic-gate 				optind--;
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	for (i = eargc; optind < argc; i++, optind++) {
5277c478bd9Sstevel@tonic-gate 		argv[i] = argv[optind];
5287c478bd9Sstevel@tonic-gate 		eargc++;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/* Check options */
5327c478bd9Sstevel@tonic-gate 	if (Ncols == 0)
5337c478bd9Sstevel@tonic-gate 		Ncols = 1;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (mflg && (Ncols > 1)) {
5367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5377c478bd9Sstevel@tonic-gate 		    gettext("pr: only one of either -m or -column allowed\n"));
5387c478bd9Sstevel@tonic-gate 		usage(1);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (Ncols == 1 && fold)
5427c478bd9Sstevel@tonic-gate 		Multi = 'm';
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (Length <= 0)
5457c478bd9Sstevel@tonic-gate 		Length = LENGTH;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (Length <= Margin)
5487c478bd9Sstevel@tonic-gate 		Margin = 0;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	Plength = Length - Margin/2;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	if (Multi == 'm')
5537c478bd9Sstevel@tonic-gate 		Ncols = eargc;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	switch (Ncols) {
5567c478bd9Sstevel@tonic-gate 	case 0:
5577c478bd9Sstevel@tonic-gate 		Ncols = 1;
5587c478bd9Sstevel@tonic-gate 		break;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	case 1:
5617c478bd9Sstevel@tonic-gate 		break;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	default:
5647c478bd9Sstevel@tonic-gate 		if (Etabn == 0)	/* respect explicit tab specification */
5657c478bd9Sstevel@tonic-gate 			Etabn = DEFTAB;
5667c478bd9Sstevel@tonic-gate 		if (Itabn == 0)
5677c478bd9Sstevel@tonic-gate 			Itabn = DEFTAB;
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if ((Fcol = (foldinf *) malloc(sizeof (foldinf) * Ncols)) == NULL) {
5717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("pr: malloc failed\n"));
5727c478bd9Sstevel@tonic-gate 		exit(1);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ncols; i++)
5757c478bd9Sstevel@tonic-gate 		Fcol[i].fold = Fcol[i].skip = 0;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (Linew == 0)
5787c478bd9Sstevel@tonic-gate 		Linew = Ncols != 1 && Sepc == 0 ? LINEW : 512;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (Lnumb) {
5817c478bd9Sstevel@tonic-gate 		int numw;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		if (Nsepc == '\t') {
5847c478bd9Sstevel@tonic-gate 			if (Itabn == 0)
5857c478bd9Sstevel@tonic-gate 				numw = Numw + DEFTAB - (Numw % DEFTAB);
5867c478bd9Sstevel@tonic-gate 			else
5877c478bd9Sstevel@tonic-gate 				numw = Numw + Itabn - (Numw % Itabn);
5887c478bd9Sstevel@tonic-gate 		} else {
5897c478bd9Sstevel@tonic-gate 			numw = Numw + ((iswprint(Nsepc)) ? 1 : 0);
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		Linew -= (Multi == 'm') ? numw : numw * Ncols;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if ((Colw = (Linew - Ncols + 1)/Ncols) < 1)
5957c478bd9Sstevel@tonic-gate 		die("width too small");
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (Ncols != 1 && Multi == 0) {
5987c478bd9Sstevel@tonic-gate 		/* Buflen should take the number of wide characters */
5997c478bd9Sstevel@tonic-gate 		/* Not the size for Buffer */
6007c478bd9Sstevel@tonic-gate 		Buflen = ((UNS) (Plength / Dblspace + 1)) *
6017c478bd9Sstevel@tonic-gate 		    2 * (Linew + 1);
6027c478bd9Sstevel@tonic-gate 		/* Should allocate Buflen * sizeof (wchar_t) */
6037c478bd9Sstevel@tonic-gate 		Buffer = (wchar_t *)getspace(Buflen * sizeof (wchar_t));
6047c478bd9Sstevel@tonic-gate 		Bufptr = Bufend = &Buffer[Buflen];
6057c478bd9Sstevel@tonic-gate 		Colpts = (COLP) getspace((UNS) ((Ncols + 1) *
6067c478bd9Sstevel@tonic-gate 		    sizeof (*Colpts)));
6077c478bd9Sstevel@tonic-gate 		Colpts[0].c_lno = 0;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* is stdin not a tty? */
6117c478bd9Sstevel@tonic-gate 	if (Ttyout && (Pause || Formfeed) && !ttyname(fileno(stdin)))
6127c478bd9Sstevel@tonic-gate 		Ttyin = fopen("/dev/tty", "r");
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	return (eargc);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static	int
6197c478bd9Sstevel@tonic-gate print(char *name)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	static	int	notfirst = 0;
6227c478bd9Sstevel@tonic-gate 	char	*date = NULL;
6237c478bd9Sstevel@tonic-gate 	char	*head = NULL;
6247c478bd9Sstevel@tonic-gate 	int	c;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (Multi != 'm' && mustopen(name, &Files[0]) == NULL)
6277c478bd9Sstevel@tonic-gate 		return (0);
6287c478bd9Sstevel@tonic-gate 	if (Multi == 'm' && Nfiles == 0 && mustopen(name, &Files[0]) == NULL)
6297c478bd9Sstevel@tonic-gate 		die("cannot open stdin");
6307c478bd9Sstevel@tonic-gate 	if (Buffer)
6317c478bd9Sstevel@tonic-gate 		(void) ungetwc(Files->f_nextc, Files->f_f);
6327c478bd9Sstevel@tonic-gate 	if (Lnumb)
6337c478bd9Sstevel@tonic-gate 		Lnumb = 1;
6347c478bd9Sstevel@tonic-gate 	for (Page = 0; ; putpage()) {
6357c478bd9Sstevel@tonic-gate 		if (C == WEOF && !(fold && Buffer))
6367c478bd9Sstevel@tonic-gate 			break;
6377c478bd9Sstevel@tonic-gate 		if (Buffer)
6387c478bd9Sstevel@tonic-gate 			nexbuf();
6397c478bd9Sstevel@tonic-gate 		Inpos = 0;
6407c478bd9Sstevel@tonic-gate 		if (get(0) == WEOF)
6417c478bd9Sstevel@tonic-gate 			break;
6427c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
6437c478bd9Sstevel@tonic-gate 		if (++Page >= Fpage) {
6447c478bd9Sstevel@tonic-gate 			/* Pause if -p and not first page */
6457c478bd9Sstevel@tonic-gate 			if (Ttyout && Pause && !notfirst++) {
6467c478bd9Sstevel@tonic-gate 				PROMPT();	/* prompt with bell and pause */
6477c478bd9Sstevel@tonic-gate 				while ((c = getc(Ttyin)) != EOF && c != '\n')
6487c478bd9Sstevel@tonic-gate 					;
6497c478bd9Sstevel@tonic-gate 			}
6507c478bd9Sstevel@tonic-gate 			if (Margin == 0)
6517c478bd9Sstevel@tonic-gate 				continue;
6527c478bd9Sstevel@tonic-gate 			if (date == NULL)
6537c478bd9Sstevel@tonic-gate 				date = GETDATE();
6547c478bd9Sstevel@tonic-gate 			if (head == NULL)
6557c478bd9Sstevel@tonic-gate 				head = Head != NULL ? Head :
6567c478bd9Sstevel@tonic-gate 				    Nfiles < 2 ? Files->f_name : nulls;
6577c478bd9Sstevel@tonic-gate 			(void) printf("\n\n");
6587c478bd9Sstevel@tonic-gate 			Nspace = Offset;
6597c478bd9Sstevel@tonic-gate 			putspace();
6607c478bd9Sstevel@tonic-gate 			(void) printf(HEAD);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 	C = '\0';
6647c478bd9Sstevel@tonic-gate 	return (1);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate static	void
6697c478bd9Sstevel@tonic-gate putpage()
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	int	colno;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (fold) {
6747c478bd9Sstevel@tonic-gate 		foldpage();
6757c478bd9Sstevel@tonic-gate 		return;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	for (Line = Margin / 2; ; (void) get(0)) {
6787c478bd9Sstevel@tonic-gate 		for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
6797c478bd9Sstevel@tonic-gate 			if (Lnumb && (C != WEOF) &&
6807c478bd9Sstevel@tonic-gate 			    (((colno == 0) && (Multi == 'm')) ||
6817c478bd9Sstevel@tonic-gate 			    (Multi != 'm'))) {
6827c478bd9Sstevel@tonic-gate 				if (Page >= Fpage) {
6837c478bd9Sstevel@tonic-gate 					putspace();
6847c478bd9Sstevel@tonic-gate 					(void) printf("%*ld%wc", Numw, Buffer ?
6857c478bd9Sstevel@tonic-gate 					    Colpts[colno].c_lno++ :
6867c478bd9Sstevel@tonic-gate 					    Lnumb, Nsepc);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 					/* Move Outpos for number field */
6897c478bd9Sstevel@tonic-gate 					Outpos += Numw;
6907c478bd9Sstevel@tonic-gate 					if (Nsepc == '\t')
6917c478bd9Sstevel@tonic-gate 						Outpos +=
6927c478bd9Sstevel@tonic-gate 						    DEFTAB - (Outpos % DEFTAB);
6937c478bd9Sstevel@tonic-gate 					else
6947c478bd9Sstevel@tonic-gate 						Outpos++;
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 				++Lnumb;
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 			for (Lcolpos = 0, Pcolpos = 0;
6997c478bd9Sstevel@tonic-gate 			    C != '\n' && C != '\f' && C != WEOF;
7007c478bd9Sstevel@tonic-gate 			    (void) get(colno))
7017c478bd9Sstevel@tonic-gate 				(void) put(C);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 			if ((C == WEOF) || (++colno == Ncols) ||
7047c478bd9Sstevel@tonic-gate 			    ((C == '\n') && (get(colno) == WEOF)))
7057c478bd9Sstevel@tonic-gate 				break;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 			if (Sepc)
7087c478bd9Sstevel@tonic-gate 				(void) put(Sepc);
7097c478bd9Sstevel@tonic-gate 			else if ((Nspace += Colw - Lcolpos + 1) < 1)
7107c478bd9Sstevel@tonic-gate 				Nspace = 1;
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		if (C == WEOF) {
7147c478bd9Sstevel@tonic-gate 			if (Margin != 0)
7157c478bd9Sstevel@tonic-gate 				break;
7167c478bd9Sstevel@tonic-gate 			if (colno != 0)
7177c478bd9Sstevel@tonic-gate 				(void) put('\n');
7187c478bd9Sstevel@tonic-gate 			return;
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 		if (C == '\f')
7217c478bd9Sstevel@tonic-gate 			break;
7227c478bd9Sstevel@tonic-gate 		(void) put('\n');
7237c478bd9Sstevel@tonic-gate 		if (Dblspace == 2 && Line < Plength)
7247c478bd9Sstevel@tonic-gate 			(void) put('\n');
7257c478bd9Sstevel@tonic-gate 		if (Line >= Plength)
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	if (Formfeed)
7297c478bd9Sstevel@tonic-gate 		(void) put('\f');
7307c478bd9Sstevel@tonic-gate 	else
7317c478bd9Sstevel@tonic-gate 		while (Line < Length)
7327c478bd9Sstevel@tonic-gate 			(void) put('\n');
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static	void
7377c478bd9Sstevel@tonic-gate foldpage()
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	int	colno;
7407c478bd9Sstevel@tonic-gate 	int	keep;
7417c478bd9Sstevel@tonic-gate 	int	i;
7427c478bd9Sstevel@tonic-gate 	int	pLcolpos;
7437c478bd9Sstevel@tonic-gate 	static	int	sl;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	for (Line = Margin / 2; ; (void) get(0)) {
7467c478bd9Sstevel@tonic-gate 		for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
7477c478bd9Sstevel@tonic-gate 			if (Lnumb && Multi == 'm' && foldcol) {
7487c478bd9Sstevel@tonic-gate 				if (!Fcol[colno].skip) {
7497c478bd9Sstevel@tonic-gate 					unget(colno);
7507c478bd9Sstevel@tonic-gate 					putspace();
7517c478bd9Sstevel@tonic-gate 					if (!colno) {
7527c478bd9Sstevel@tonic-gate 						for (i = 0; i <= Numw; i++)
7537c478bd9Sstevel@tonic-gate 							(void) printf(" ");
7547c478bd9Sstevel@tonic-gate 						(void) printf("%wc", Nsepc);
7557c478bd9Sstevel@tonic-gate 					}
7567c478bd9Sstevel@tonic-gate 					for (i = 0; i <= Colw; i++)
7577c478bd9Sstevel@tonic-gate 						(void) printf(" ");
7587c478bd9Sstevel@tonic-gate 					(void) put(Sepc);
7597c478bd9Sstevel@tonic-gate 					if (++colno == Ncols)
7607c478bd9Sstevel@tonic-gate 						break;
7617c478bd9Sstevel@tonic-gate 					(void) get(colno);
7627c478bd9Sstevel@tonic-gate 					continue;
7637c478bd9Sstevel@tonic-gate 				} else if (!colno)
7647c478bd9Sstevel@tonic-gate 					Lnumb = sl;
7657c478bd9Sstevel@tonic-gate 			}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 			if (Lnumb && (C != WEOF) &&
7687c478bd9Sstevel@tonic-gate 			    ((colno == 0 && Multi == 'm') || (Multi != 'm'))) {
7697c478bd9Sstevel@tonic-gate 				if (Page >= Fpage) {
7707c478bd9Sstevel@tonic-gate 					putspace();
7717c478bd9Sstevel@tonic-gate 					if ((foldcol &&
7727c478bd9Sstevel@tonic-gate 					    Fcol[colno].skip && Multi != 'a') ||
7737c478bd9Sstevel@tonic-gate 					    (Fcol[0].fold && Multi == 'a') ||
7747c478bd9Sstevel@tonic-gate 					    (Buffer && Colpts[colno].c_skip)) {
7757c478bd9Sstevel@tonic-gate 						for (i = 0; i < Numw; i++)
7767c478bd9Sstevel@tonic-gate 							(void) printf(" ");
7777c478bd9Sstevel@tonic-gate 						(void) printf("%wc", Nsepc);
7787c478bd9Sstevel@tonic-gate 						if (Buffer) {
7797c478bd9Sstevel@tonic-gate 							Colpts[colno].c_lno++;
7807c478bd9Sstevel@tonic-gate 							Colpts[colno].c_skip =
7817c478bd9Sstevel@tonic-gate 							    0;
7827c478bd9Sstevel@tonic-gate 						}
7837c478bd9Sstevel@tonic-gate 					}
7847c478bd9Sstevel@tonic-gate 					else
7857c478bd9Sstevel@tonic-gate 					(void) printf("%*ld%wc", Numw, Buffer ?
7867c478bd9Sstevel@tonic-gate 					    Colpts[colno].c_lno++ :
7877c478bd9Sstevel@tonic-gate 					    Lnumb, Nsepc);
7887c478bd9Sstevel@tonic-gate 				}
7897c478bd9Sstevel@tonic-gate 				sl = Lnumb++;
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 			pLcolpos = 0;
7927c478bd9Sstevel@tonic-gate 			for (Lcolpos = 0, Pcolpos = 0;
7937c478bd9Sstevel@tonic-gate 			    C != '\n' && C != '\f' && C != WEOF;
7947c478bd9Sstevel@tonic-gate 			    (void) get(colno)) {
7957c478bd9Sstevel@tonic-gate 				if (put(C)) {
7967c478bd9Sstevel@tonic-gate 					unget(colno);
7977c478bd9Sstevel@tonic-gate 					Fcol[(Multi == 'a') ? 0 : colno].fold
7987c478bd9Sstevel@tonic-gate 					    = 1;
7997c478bd9Sstevel@tonic-gate 					break;
8007c478bd9Sstevel@tonic-gate 				} else if (Multi == 'a') {
8017c478bd9Sstevel@tonic-gate 					Fcol[0].fold = 0;
8027c478bd9Sstevel@tonic-gate 				}
8037c478bd9Sstevel@tonic-gate 				pLcolpos = Lcolpos;
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 			if (Buffer) {
8067c478bd9Sstevel@tonic-gate 				alleof = 1;
8077c478bd9Sstevel@tonic-gate 				for (i = 0; i < Ncols; i++)
8087c478bd9Sstevel@tonic-gate 					if (!Fcol[i].eof)
8097c478bd9Sstevel@tonic-gate 						alleof = 0;
8107c478bd9Sstevel@tonic-gate 				if (alleof || ++colno == Ncols)
8117c478bd9Sstevel@tonic-gate 					break;
8127c478bd9Sstevel@tonic-gate 			} else if (C == EOF || ++colno == Ncols)
8137c478bd9Sstevel@tonic-gate 				break;
8147c478bd9Sstevel@tonic-gate 			keep = C;
8157c478bd9Sstevel@tonic-gate 			(void) get(colno);
8167c478bd9Sstevel@tonic-gate 			if (keep == '\n' && C == WEOF)
8177c478bd9Sstevel@tonic-gate 				break;
8187c478bd9Sstevel@tonic-gate 			if (Sepc)
8197c478bd9Sstevel@tonic-gate 				(void) put(Sepc);
8207c478bd9Sstevel@tonic-gate 			else if ((Nspace += Colw - pLcolpos + 1) < 1)
8217c478bd9Sstevel@tonic-gate 				Nspace = 1;
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 		foldcol = 0;
8247c478bd9Sstevel@tonic-gate 		if (Lnumb && Multi != 'a') {
8257c478bd9Sstevel@tonic-gate 			for (i = 0; i < Ncols; i++) {
8267c478bd9Sstevel@tonic-gate 				Fcol[i].skip = Fcol[i].fold;
8277c478bd9Sstevel@tonic-gate 				foldcol +=  Fcol[i].fold;
8287c478bd9Sstevel@tonic-gate 				Fcol[i].fold = 0;
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 		if (C == WEOF) {
8327c478bd9Sstevel@tonic-gate 			if (Margin != 0)
8337c478bd9Sstevel@tonic-gate 				break;
8347c478bd9Sstevel@tonic-gate 			if (colno != 0)
8357c478bd9Sstevel@tonic-gate 				(void) put('\n');
8367c478bd9Sstevel@tonic-gate 			return;
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 		if (C == '\f')
8397c478bd9Sstevel@tonic-gate 			break;
8407c478bd9Sstevel@tonic-gate 		(void) put('\n');
8417c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
8427c478bd9Sstevel@tonic-gate 		if (Dblspace == 2 && Line < Plength)
8437c478bd9Sstevel@tonic-gate 			(void) put('\n');
8447c478bd9Sstevel@tonic-gate 		if (Line >= Plength)
8457c478bd9Sstevel@tonic-gate 			break;
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	if (Formfeed)
8487c478bd9Sstevel@tonic-gate 		(void) put('\f');
8497c478bd9Sstevel@tonic-gate 	else while (Line < Length)
8507c478bd9Sstevel@tonic-gate 		(void) put('\n');
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate static	void
8557c478bd9Sstevel@tonic-gate nexbuf()
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	wchar_t	*s = Buffer;
8587c478bd9Sstevel@tonic-gate 	COLP	p = Colpts;
8597c478bd9Sstevel@tonic-gate 	int	j;
8607c478bd9Sstevel@tonic-gate 	int	c;
8617c478bd9Sstevel@tonic-gate 	int	bline = 0;
8627c478bd9Sstevel@tonic-gate 	wchar_t	wc;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (fold) {
8657c478bd9Sstevel@tonic-gate 		foldbuf();
8667c478bd9Sstevel@tonic-gate 		return;
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 	for (; ; ) {
8697c478bd9Sstevel@tonic-gate 		p->c_ptr0 = p->c_ptr = s;
8707c478bd9Sstevel@tonic-gate 		if (p == &Colpts[Ncols])
8717c478bd9Sstevel@tonic-gate 			return;
8727c478bd9Sstevel@tonic-gate 		(p++)->c_lno = Lnumb + bline;
8737c478bd9Sstevel@tonic-gate 		for (j = (Length - Margin)/Dblspace; --j >= 0; ++bline) {
8747c478bd9Sstevel@tonic-gate 			for (Inpos = 0; ; ) {
8757c478bd9Sstevel@tonic-gate 				errno = 0;
8767c478bd9Sstevel@tonic-gate 				wc = _fgetwc_pr(Files->f_f, &c);
8777c478bd9Sstevel@tonic-gate 				if (wc == WEOF) {
8787c478bd9Sstevel@tonic-gate 					/* If there is an illegal character, */
8797c478bd9Sstevel@tonic-gate 					/* handle it as a byte sequence. */
8807c478bd9Sstevel@tonic-gate 					if (errno == EILSEQ) {
8817c478bd9Sstevel@tonic-gate 						if (Inpos < Colw - 1) {
8827c478bd9Sstevel@tonic-gate 							*s = c;
8837c478bd9Sstevel@tonic-gate 							if (++s >= Bufend)
8847c478bd9Sstevel@tonic-gate die("page-buffer overflow");
8857c478bd9Sstevel@tonic-gate 						}
8867c478bd9Sstevel@tonic-gate 						Inpos++;
8877c478bd9Sstevel@tonic-gate 						Error++;
8887c478bd9Sstevel@tonic-gate 						return;
8897c478bd9Sstevel@tonic-gate 					} else {
8907c478bd9Sstevel@tonic-gate 						/* Real EOF */
8917c478bd9Sstevel@tonic-gate for (*s = WEOF; p <= &Colpts[Ncols]; ++p)
8927c478bd9Sstevel@tonic-gate 	p->c_ptr0 = p->c_ptr = s;
8937c478bd9Sstevel@tonic-gate 						balance(bline);
8947c478bd9Sstevel@tonic-gate 						return;
8957c478bd9Sstevel@tonic-gate 					}
8967c478bd9Sstevel@tonic-gate 				}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 				if (isascii(wc)) {
8997c478bd9Sstevel@tonic-gate 					if (isprint(wc))
9007c478bd9Sstevel@tonic-gate 						Inpos++;
9017c478bd9Sstevel@tonic-gate 				} else if (iswprint(wc)) {
9027c478bd9Sstevel@tonic-gate 					Inpos += wcwidth(wc);
9037c478bd9Sstevel@tonic-gate 				}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 				if (Inpos <= Colw || wc == '\n') {
9067c478bd9Sstevel@tonic-gate 					*s = wc;
9077c478bd9Sstevel@tonic-gate 					if (++s >= Bufend)
9087c478bd9Sstevel@tonic-gate 						die("page-buffer overflow");
9097c478bd9Sstevel@tonic-gate 				}
9107c478bd9Sstevel@tonic-gate 				if (wc == '\n')
9117c478bd9Sstevel@tonic-gate 					break;
9127c478bd9Sstevel@tonic-gate 				switch (wc) {
9137c478bd9Sstevel@tonic-gate 				case '\b':
9147c478bd9Sstevel@tonic-gate 					if (Inpos == 0)
9157c478bd9Sstevel@tonic-gate 						--s;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 				case ESC:
9207c478bd9Sstevel@tonic-gate 					if (Inpos > 0)
9217c478bd9Sstevel@tonic-gate 						--Inpos;
9227c478bd9Sstevel@tonic-gate 				}
9237c478bd9Sstevel@tonic-gate 			}
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate static	void
9307c478bd9Sstevel@tonic-gate foldbuf()
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	int	num;
9337c478bd9Sstevel@tonic-gate 	int	i;
9347c478bd9Sstevel@tonic-gate 	int	colno = 0;
9357c478bd9Sstevel@tonic-gate 	int	size = Buflen;
9367c478bd9Sstevel@tonic-gate 	wchar_t	*s;
9377c478bd9Sstevel@tonic-gate 	wchar_t	*d;
9387c478bd9Sstevel@tonic-gate 	COLP	p = Colpts;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ncols; i++)
9417c478bd9Sstevel@tonic-gate 		Fcol[i].eof = 0;
9427c478bd9Sstevel@tonic-gate 	d = Buffer;
9437c478bd9Sstevel@tonic-gate 	if (Bufptr != Bufend) {
9447c478bd9Sstevel@tonic-gate 		s = Bufptr;
9457c478bd9Sstevel@tonic-gate 		while (s < Bufend)
9467c478bd9Sstevel@tonic-gate 			*d++ = *s++;
9477c478bd9Sstevel@tonic-gate 		size -= (Bufend - Bufptr);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	Bufptr = Buffer;
9507c478bd9Sstevel@tonic-gate 	p->c_ptr0 = p->c_ptr = Buffer;
9517c478bd9Sstevel@tonic-gate 	if (p->c_lno == 0) {
9527c478bd9Sstevel@tonic-gate 		p->c_lno = Lnumb;
9537c478bd9Sstevel@tonic-gate 		p->c_skip = 0;
9547c478bd9Sstevel@tonic-gate 	} else {
9557c478bd9Sstevel@tonic-gate 		p->c_lno = Colpts[Ncols-1].c_lno;
9567c478bd9Sstevel@tonic-gate 		p->c_skip = Colpts[Ncols].c_skip;
9577c478bd9Sstevel@tonic-gate 		if (p->c_skip)
9587c478bd9Sstevel@tonic-gate 			p->c_lno--;
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 	if ((num = freadw(d, size, Files->f_f)) != size) {
9617c478bd9Sstevel@tonic-gate 		for (*(d+num) = WEOF; (++p) <= &Colpts[Ncols]; ) {
9627c478bd9Sstevel@tonic-gate 			p->c_ptr0 = p->c_ptr = (d+num);
9637c478bd9Sstevel@tonic-gate 		}
9647c478bd9Sstevel@tonic-gate 		balance(0);
9657c478bd9Sstevel@tonic-gate 		return;
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 	i = (Length - Margin) / Dblspace;
9687c478bd9Sstevel@tonic-gate 	do {
9697c478bd9Sstevel@tonic-gate 		(void) readbuf(&Bufptr, i, p++);
9707c478bd9Sstevel@tonic-gate 	} while (++colno < Ncols);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate static	void
9757c478bd9Sstevel@tonic-gate balance(int bline)	/* line balancing for last page */
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	wchar_t	*s = Buffer;
9787c478bd9Sstevel@tonic-gate 	COLP	p = Colpts;
9797c478bd9Sstevel@tonic-gate 	int	colno = 0;
9807c478bd9Sstevel@tonic-gate 	int	j;
9817c478bd9Sstevel@tonic-gate 	int	c;
9827c478bd9Sstevel@tonic-gate 	int	l;
9837c478bd9Sstevel@tonic-gate 	int	lines;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	if (!fold) {
9867c478bd9Sstevel@tonic-gate 		c = bline % Ncols;
9877c478bd9Sstevel@tonic-gate 		l = (bline + Ncols - 1)/Ncols;
9887c478bd9Sstevel@tonic-gate 		bline = 0;
9897c478bd9Sstevel@tonic-gate 		do {
9907c478bd9Sstevel@tonic-gate 			for (j = 0; j < l; ++j)
9917c478bd9Sstevel@tonic-gate 				while (*s++ != '\n')
9927c478bd9Sstevel@tonic-gate 					;
9937c478bd9Sstevel@tonic-gate 			(++p)->c_lno = Lnumb + (bline += l);
9947c478bd9Sstevel@tonic-gate 			p->c_ptr0 = p->c_ptr = s;
9957c478bd9Sstevel@tonic-gate 			if (++colno == c)
9967c478bd9Sstevel@tonic-gate 				--l;
9977c478bd9Sstevel@tonic-gate 		} while (colno < Ncols - 1);
9987c478bd9Sstevel@tonic-gate 	} else {
9997c478bd9Sstevel@tonic-gate 		lines = readbuf(&s, 0, 0);
10007c478bd9Sstevel@tonic-gate 		l = (lines + Ncols - 1)/Ncols;
10017c478bd9Sstevel@tonic-gate 		if (l > ((Length - Margin) / Dblspace)) {
10027c478bd9Sstevel@tonic-gate 			l = (Length - Margin) / Dblspace;
10037c478bd9Sstevel@tonic-gate 			c = Ncols;
10047c478bd9Sstevel@tonic-gate 		} else {
10057c478bd9Sstevel@tonic-gate 			c = lines % Ncols;
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 		s = Buffer;
10087c478bd9Sstevel@tonic-gate 		do {
10097c478bd9Sstevel@tonic-gate 			(void) readbuf(&s, l, p++);
10107c478bd9Sstevel@tonic-gate 			if (++colno == c)
10117c478bd9Sstevel@tonic-gate 				--l;
10127c478bd9Sstevel@tonic-gate 		} while (colno < Ncols);
10137c478bd9Sstevel@tonic-gate 		Bufptr = s;
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate static	int
10197c478bd9Sstevel@tonic-gate readbuf(wchar_t **s, int lincol, COLP p)
10207c478bd9Sstevel@tonic-gate {
10217c478bd9Sstevel@tonic-gate 	int	lines = 0;
10227c478bd9Sstevel@tonic-gate 	int	chars = 0;
10237c478bd9Sstevel@tonic-gate 	int	width;
10247c478bd9Sstevel@tonic-gate 	int	nls = 0;
10257c478bd9Sstevel@tonic-gate 	int	move;
10267c478bd9Sstevel@tonic-gate 	int	skip = 0;
10277c478bd9Sstevel@tonic-gate 	int	decr = 0;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	width = (Ncols == 1) ? Linew : Colw;
10307c478bd9Sstevel@tonic-gate 	while (**s != WEOF) {
10317c478bd9Sstevel@tonic-gate 		switch (**s) {
10327c478bd9Sstevel@tonic-gate 			case '\n':
10337c478bd9Sstevel@tonic-gate 				lines++; nls++; chars = 0; skip = 0;
10347c478bd9Sstevel@tonic-gate 				break;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 			case '\b':
10377c478bd9Sstevel@tonic-gate 			case ESC:
10387c478bd9Sstevel@tonic-gate 				if (chars) chars--;
10397c478bd9Sstevel@tonic-gate 				break;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 			case '\t':
10427c478bd9Sstevel@tonic-gate 				move = Itabn - ((chars + Itabn) % Itabn);
10437c478bd9Sstevel@tonic-gate 				move = (move < width-chars) ? move :
10447c478bd9Sstevel@tonic-gate 				    width-chars;
10457c478bd9Sstevel@tonic-gate 				chars += move;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 			default:
10487c478bd9Sstevel@tonic-gate 				if (isascii(**s)) {
10497c478bd9Sstevel@tonic-gate 					if (isprint(**s))
10507c478bd9Sstevel@tonic-gate 						chars++;
10517c478bd9Sstevel@tonic-gate 				} else if (iswprint(**s)) {
10527c478bd9Sstevel@tonic-gate 					chars += wcwidth(**s);
10537c478bd9Sstevel@tonic-gate 				}
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 		if (chars > width) {
10567c478bd9Sstevel@tonic-gate 			lines++;
10577c478bd9Sstevel@tonic-gate 			skip++;
10587c478bd9Sstevel@tonic-gate 			decr++;
10597c478bd9Sstevel@tonic-gate 			chars = 0;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 		if (lincol && lines == lincol) {
10627c478bd9Sstevel@tonic-gate 			(p+1)->c_lno = p->c_lno + nls;
10637c478bd9Sstevel@tonic-gate 			(++p)->c_skip = skip;
10647c478bd9Sstevel@tonic-gate 			if (**s == '\n') (*s)++;
10657c478bd9Sstevel@tonic-gate 			p->c_ptr0 = p->c_ptr = (wchar_t *)*s;
10667c478bd9Sstevel@tonic-gate 			return (0);
10677c478bd9Sstevel@tonic-gate 		}
10687c478bd9Sstevel@tonic-gate 		if (decr)
10697c478bd9Sstevel@tonic-gate 			decr = 0;
10707c478bd9Sstevel@tonic-gate 		else
10717c478bd9Sstevel@tonic-gate 			(*s)++;
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 	return (lines);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate static	wint_t
10787c478bd9Sstevel@tonic-gate get(int colno)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	static	int	peekc = 0;
10817c478bd9Sstevel@tonic-gate 	COLP	p;
10827c478bd9Sstevel@tonic-gate 	FILS	*q;
10837c478bd9Sstevel@tonic-gate 	int	c;
10847c478bd9Sstevel@tonic-gate 	wchar_t		wc, w;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (peekc) {
10877c478bd9Sstevel@tonic-gate 		peekc = 0;
10887c478bd9Sstevel@tonic-gate 		wc = Etabc;
10897c478bd9Sstevel@tonic-gate 	} else if (Buffer) {
10907c478bd9Sstevel@tonic-gate 		p = &Colpts[colno];
10917c478bd9Sstevel@tonic-gate 		if (p->c_ptr >= (p+1)->c_ptr0)
10927c478bd9Sstevel@tonic-gate 			wc = WEOF;
10937c478bd9Sstevel@tonic-gate 		else if ((wc = *p->c_ptr) != WEOF)
10947c478bd9Sstevel@tonic-gate 			++p->c_ptr;
10957c478bd9Sstevel@tonic-gate 		if (fold && wc == WEOF)
10967c478bd9Sstevel@tonic-gate 			Fcol[colno].eof = 1;
10977c478bd9Sstevel@tonic-gate 	} else if ((wc =
10987c478bd9Sstevel@tonic-gate 		(q = &Files[Multi == 'a' ? 0 : colno])->f_nextc) == WEOF) {
10997c478bd9Sstevel@tonic-gate 		for (q = &Files[Nfiles]; --q >= Files && q->f_nextc == WEOF; )
11007c478bd9Sstevel@tonic-gate 			;
11017c478bd9Sstevel@tonic-gate 		if (q >= Files)
11027c478bd9Sstevel@tonic-gate 			wc = '\n';
11037c478bd9Sstevel@tonic-gate 	} else {
11047c478bd9Sstevel@tonic-gate 		errno = 0;
11057c478bd9Sstevel@tonic-gate 		w = _fgetwc_pr(q->f_f, &c);
11067c478bd9Sstevel@tonic-gate 		if (w == WEOF && errno == EILSEQ) {
11077c478bd9Sstevel@tonic-gate 			q->f_nextc = (wchar_t)c;
11087c478bd9Sstevel@tonic-gate 		} else {
11097c478bd9Sstevel@tonic-gate 			q->f_nextc = w;
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	if (Etabn != 0 && wc == Etabc) {
11147c478bd9Sstevel@tonic-gate 		++Inpos;
11157c478bd9Sstevel@tonic-gate 		peekc = ETABS;
11167c478bd9Sstevel@tonic-gate 		wc = ' ';
11177c478bd9Sstevel@tonic-gate 		return (C = wc);
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	if (wc == WEOF)
11217c478bd9Sstevel@tonic-gate 		return (C = wc);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	if (isascii(wc)) {
11247c478bd9Sstevel@tonic-gate 		if (isprint(wc)) {
11257c478bd9Sstevel@tonic-gate 			Inpos++;
11267c478bd9Sstevel@tonic-gate 			return (C = wc);
11277c478bd9Sstevel@tonic-gate 		}
11287c478bd9Sstevel@tonic-gate 	} else if (iswprint(wc)) {
11297c478bd9Sstevel@tonic-gate 		Inpos += wcwidth(wc);
11307c478bd9Sstevel@tonic-gate 		return (C = wc);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	switch (wc) {
11347c478bd9Sstevel@tonic-gate 	case '\b':
11357c478bd9Sstevel@tonic-gate 	case ESC:
11367c478bd9Sstevel@tonic-gate 		if (Inpos > 0)
11377c478bd9Sstevel@tonic-gate 			--Inpos;
11387c478bd9Sstevel@tonic-gate 		break;
11397c478bd9Sstevel@tonic-gate 	case '\f':
11407c478bd9Sstevel@tonic-gate 		if (Ncols == 1)
11417c478bd9Sstevel@tonic-gate 			break;
11427c478bd9Sstevel@tonic-gate 		wc = '\n';
11437c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
11447c478bd9Sstevel@tonic-gate 	case '\n':
11457c478bd9Sstevel@tonic-gate 	case '\r':
11467c478bd9Sstevel@tonic-gate 		Inpos = 0;
11477c478bd9Sstevel@tonic-gate 		break;
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 	return (C = wc);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate static	int
11547c478bd9Sstevel@tonic-gate put(wchar_t wc)
11557c478bd9Sstevel@tonic-gate {
11567c478bd9Sstevel@tonic-gate 	int	move = 0;
11577c478bd9Sstevel@tonic-gate 	int	width = Colw;
11587c478bd9Sstevel@tonic-gate 	int	sp = Lcolpos;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (fold && Ncols == 1)
11617c478bd9Sstevel@tonic-gate 		width = Linew;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	switch (wc) {
11647c478bd9Sstevel@tonic-gate 	case ' ':
11657c478bd9Sstevel@tonic-gate 		/* If column not full or this is separator char */
11667c478bd9Sstevel@tonic-gate 		if ((!fold && Ncols < 2) || (Lcolpos < width) ||
11677c478bd9Sstevel@tonic-gate 		    ((Sepc == wc) && (Lcolpos == width))) {
11687c478bd9Sstevel@tonic-gate 			++Nspace;
11697c478bd9Sstevel@tonic-gate 			++Lcolpos;
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 		if (fold && sp == Lcolpos)
11727c478bd9Sstevel@tonic-gate 			if (Lcolpos >= width)
11737c478bd9Sstevel@tonic-gate 				return (1);
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		return (0);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	case '\t':
11787c478bd9Sstevel@tonic-gate 		if (Itabn == 0)
11797c478bd9Sstevel@tonic-gate 			break;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		/* If column not full or this is separator char */
11827c478bd9Sstevel@tonic-gate 		if ((Lcolpos < width) ||
11837c478bd9Sstevel@tonic-gate 		    ((Sepc == wc) && (Lcolpos == width))) {
11847c478bd9Sstevel@tonic-gate 			move = Itabn - ((Lcolpos + Itabn) % Itabn);
11857c478bd9Sstevel@tonic-gate 			move = (move < width-Lcolpos) ? move : width-Lcolpos;
11867c478bd9Sstevel@tonic-gate 			Nspace += move;
11877c478bd9Sstevel@tonic-gate 			Lcolpos += move;
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 		if (fold && sp == Lcolpos)
11907c478bd9Sstevel@tonic-gate 			if (Lcolpos >= width)
11917c478bd9Sstevel@tonic-gate 				return (1);
11927c478bd9Sstevel@tonic-gate 		return (0);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	case '\b':
11957c478bd9Sstevel@tonic-gate 		if (Lcolpos == 0)
11967c478bd9Sstevel@tonic-gate 			return (0);
11977c478bd9Sstevel@tonic-gate 		if (Nspace > 0) {
11987c478bd9Sstevel@tonic-gate 			--Nspace;
11997c478bd9Sstevel@tonic-gate 			--Lcolpos;
12007c478bd9Sstevel@tonic-gate 			return (0);
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 		if (Lcolpos > Pcolpos) {
12037c478bd9Sstevel@tonic-gate 			--Lcolpos;
12047c478bd9Sstevel@tonic-gate 			return (0);
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	case ESC:
12107c478bd9Sstevel@tonic-gate 		move = -1;
12117c478bd9Sstevel@tonic-gate 		break;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	case '\n':
12147c478bd9Sstevel@tonic-gate 		++Line;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	case '\r':
12197c478bd9Sstevel@tonic-gate 	case '\f':
12207c478bd9Sstevel@tonic-gate 		Pcolpos = 0;
12217c478bd9Sstevel@tonic-gate 		Lcolpos = 0;
12227c478bd9Sstevel@tonic-gate 		Nspace = 0;
12237c478bd9Sstevel@tonic-gate 		Outpos = 0;
12247c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
12257c478bd9Sstevel@tonic-gate 	default:
12267c478bd9Sstevel@tonic-gate 		if (isascii(wc)) {
12277c478bd9Sstevel@tonic-gate 			if (isprint(wc))
12287c478bd9Sstevel@tonic-gate 				move = 1;
12297c478bd9Sstevel@tonic-gate 			else
12307c478bd9Sstevel@tonic-gate 				move = 0;
12317c478bd9Sstevel@tonic-gate 		} else if (iswprint(wc)) {
12327c478bd9Sstevel@tonic-gate 			move = wcwidth(wc);
12337c478bd9Sstevel@tonic-gate 		} else {
12347c478bd9Sstevel@tonic-gate 			move = 0;
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 		break;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 	if (Page < Fpage)
12397c478bd9Sstevel@tonic-gate 		return (0);
12407c478bd9Sstevel@tonic-gate 	if (Lcolpos > 0 || move > 0)
12417c478bd9Sstevel@tonic-gate 		Lcolpos += move;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	putspace();
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/* If column not full or this is separator char */
12467c478bd9Sstevel@tonic-gate 	if ((!fold && Ncols < 2) || (Lcolpos <= width) ||
12477c478bd9Sstevel@tonic-gate 	    ((Sepc == wc) && (Lcolpos > width))) {
12487c478bd9Sstevel@tonic-gate 		(void) fputwc(wc, stdout);
12497c478bd9Sstevel@tonic-gate 		Outpos += move;
12507c478bd9Sstevel@tonic-gate 		Pcolpos = Lcolpos;
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if (fold && Lcolpos > width)
12547c478bd9Sstevel@tonic-gate 		return (1);
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	return (0);
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate static	void
12617c478bd9Sstevel@tonic-gate putspace(void)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	int nc = 0;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	for (; Nspace > 0; Outpos += nc, Nspace -= nc) {
12667c478bd9Sstevel@tonic-gate #ifdef XPG4
12677c478bd9Sstevel@tonic-gate 		/* XPG4:  -i:  replace multiple SPACE chars with tab chars */
12687c478bd9Sstevel@tonic-gate 		if ((Nspace >= 2 && Itabn > 0 &&
12697c478bd9Sstevel@tonic-gate 			Nspace >= (nc = Itabn - Outpos % Itabn)) && !fold) {
12707c478bd9Sstevel@tonic-gate #else
12717c478bd9Sstevel@tonic-gate 		/* Solaris:  -i:  replace white space with tab chars */
12727c478bd9Sstevel@tonic-gate 		if ((Itabn > 0 && Nspace >= (nc = Itabn - Outpos % Itabn)) &&
12737c478bd9Sstevel@tonic-gate 			!fold) {
12747c478bd9Sstevel@tonic-gate #endif
12757c478bd9Sstevel@tonic-gate 			(void) fputwc(Itabc, stdout);
12767c478bd9Sstevel@tonic-gate 		} else {
12777c478bd9Sstevel@tonic-gate 			nc = 1;
12787c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
12797c478bd9Sstevel@tonic-gate 		}
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate static	void
12857c478bd9Sstevel@tonic-gate unget(int colno)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	if (Buffer) {
12887c478bd9Sstevel@tonic-gate 		if (*(Colpts[colno].c_ptr-1) != '\t')
12897c478bd9Sstevel@tonic-gate 			--(Colpts[colno].c_ptr);
12907c478bd9Sstevel@tonic-gate 		if (Colpts[colno].c_lno)
12917c478bd9Sstevel@tonic-gate 			Colpts[colno].c_lno--;
12927c478bd9Sstevel@tonic-gate 	} else {
12937c478bd9Sstevel@tonic-gate 		if ((Multi == 'm' && colno == 0) || Multi != 'm')
12947c478bd9Sstevel@tonic-gate 			if (Lnumb && !foldcol)
12957c478bd9Sstevel@tonic-gate 				Lnumb--;
12967c478bd9Sstevel@tonic-gate 		colno = (Multi == 'a') ? 0 : colno;
12977c478bd9Sstevel@tonic-gate 		(void) ungetwc(Files[colno].f_nextc, Files[colno].f_f);
12987c478bd9Sstevel@tonic-gate 		Files[colno].f_nextc = C;
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate /*
13047c478bd9Sstevel@tonic-gate  * Defer message about failure to open file to prevent messing up
13057c478bd9Sstevel@tonic-gate  * alignment of page with tear perforations or form markers.
13067c478bd9Sstevel@tonic-gate  * Treat empty file as special case and report as diagnostic.
13077c478bd9Sstevel@tonic-gate  */
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate static	FILE *
13107c478bd9Sstevel@tonic-gate mustopen(char *s, FILS *f)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	char	*empty_file_msg = gettext("%s -- empty file");
13137c478bd9Sstevel@tonic-gate 	int	c;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	if (*s == '\0') {
13167c478bd9Sstevel@tonic-gate 		f->f_name = STDINNAME();
13177c478bd9Sstevel@tonic-gate 		f->f_f = stdin;
13187c478bd9Sstevel@tonic-gate 	} else if ((f->f_f = fopen(f->f_name = s, "r")) == NULL) {
13197c478bd9Sstevel@tonic-gate 		s = ffiler(f->f_name);
13207c478bd9Sstevel@tonic-gate 		s = strcpy((char *)getspace((UNS) strlen(s) + 1), s);
13217c478bd9Sstevel@tonic-gate 	}
13227c478bd9Sstevel@tonic-gate 	if (f->f_f != NULL) {
13237c478bd9Sstevel@tonic-gate 		errno = 0;
13247c478bd9Sstevel@tonic-gate 		f->f_nextc = _fgetwc_pr(f->f_f, &c);
13257c478bd9Sstevel@tonic-gate 		if (f->f_nextc != WEOF) {
13267c478bd9Sstevel@tonic-gate 			return (f->f_f);
13277c478bd9Sstevel@tonic-gate 		} else {	/* WEOF */
13287c478bd9Sstevel@tonic-gate 			if (errno == EILSEQ) {
13297c478bd9Sstevel@tonic-gate 				f->f_nextc = (wchar_t)c;
13307c478bd9Sstevel@tonic-gate 				return (f->f_f);
13317c478bd9Sstevel@tonic-gate 			}
13327c478bd9Sstevel@tonic-gate 			if (Multi == 'm')
13337c478bd9Sstevel@tonic-gate 				return (f->f_f);
13347c478bd9Sstevel@tonic-gate 		}
13357c478bd9Sstevel@tonic-gate 		(void) sprintf(s = (char *)getspace((UNS) strlen(f->f_name)
13367c478bd9Sstevel@tonic-gate 		    + 1 + (UNS) strlen(empty_file_msg)),
13377c478bd9Sstevel@tonic-gate 		    empty_file_msg, f->f_name);
13387c478bd9Sstevel@tonic-gate 		(void) fclose(f->f_f);
13397c478bd9Sstevel@tonic-gate 	}
13407c478bd9Sstevel@tonic-gate 	Error = 1;
13417c478bd9Sstevel@tonic-gate 	if (Report)
13427c478bd9Sstevel@tonic-gate 		if (Ttyout) {	/* accumulate error reports */
13437c478bd9Sstevel@tonic-gate 			Lasterr = Lasterr->e_nextp =
13447c478bd9Sstevel@tonic-gate 			    (ERR *) getspace((UNS) sizeof (ERR));
13457c478bd9Sstevel@tonic-gate 			Lasterr->e_nextp = NULL;
13467c478bd9Sstevel@tonic-gate 			Lasterr->e_mess = s;
13477c478bd9Sstevel@tonic-gate 		} else {	/* ok to print error report now */
13487c478bd9Sstevel@tonic-gate 			cerror(s);
13497c478bd9Sstevel@tonic-gate 			(void) putc('\n', stderr);
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 	return ((FILE *)NULL);
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate static	ANY *
13567c478bd9Sstevel@tonic-gate getspace(UNS n)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	ANY *t;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	if ((t = (ANY *) malloc(n)) == NULL)
13617c478bd9Sstevel@tonic-gate 		die("out of space");
13627c478bd9Sstevel@tonic-gate 	return (t);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate static	void
13677c478bd9Sstevel@tonic-gate die(char *s)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	++Error;
13707c478bd9Sstevel@tonic-gate 	errprint();
13717c478bd9Sstevel@tonic-gate 	cerror(s);
13727c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
13737c478bd9Sstevel@tonic-gate 	exit(1);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate static	void
13807c478bd9Sstevel@tonic-gate errprint()	/* print accumulated error reports */
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
13837c478bd9Sstevel@tonic-gate 	for (; Err != NULL; Err = Err->e_nextp) {
13847c478bd9Sstevel@tonic-gate 		cerror(Err->e_mess);
13857c478bd9Sstevel@tonic-gate 		(void) putc('\n', stderr);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 	done();
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate static	void
13927c478bd9Sstevel@tonic-gate fixtty()
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	struct stat sbuf;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	setbuf(stdout, obuf);
13977c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
13987c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, onintr);
13997c478bd9Sstevel@tonic-gate 	if (Ttyout = ttyname(fileno(stdout))) {	/* is stdout a tty? */
14007c478bd9Sstevel@tonic-gate 		(void) stat(Ttyout, &sbuf);
14017c478bd9Sstevel@tonic-gate 		Mode = sbuf.st_mode;		/* save permissions */
14027c478bd9Sstevel@tonic-gate 		(void) chmod(Ttyout, (S_IREAD|S_IWRITE));
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate static	void
14087c478bd9Sstevel@tonic-gate onintr()
14097c478bd9Sstevel@tonic-gate {
14107c478bd9Sstevel@tonic-gate 	++Error;
14117c478bd9Sstevel@tonic-gate 	errprint();
14127c478bd9Sstevel@tonic-gate 	_exit(1);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate static	char *
14177c478bd9Sstevel@tonic-gate GETDATE()	/* return date file was last modified */
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate 	static	char	*now = NULL;
14207c478bd9Sstevel@tonic-gate 	static	struct	stat	sbuf;
14217c478bd9Sstevel@tonic-gate 	static	struct	stat	nbuf;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if (Nfiles > 1 || Files->f_name == nulls) {
14247c478bd9Sstevel@tonic-gate 		if (now == NULL) {
14257c478bd9Sstevel@tonic-gate 			(void) time(&nbuf.st_mtime);
14267c478bd9Sstevel@tonic-gate 			(void) cftime(time_buf,
14277c478bd9Sstevel@tonic-gate 				dcgettext(NULL, FORMAT, LC_TIME),
14287c478bd9Sstevel@tonic-gate 			    &nbuf.st_mtime);
14297c478bd9Sstevel@tonic-gate 			now = time_buf;
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		return (now);
14327c478bd9Sstevel@tonic-gate 	} else {
14337c478bd9Sstevel@tonic-gate 		(void) stat(Files->f_name, &sbuf);
14347c478bd9Sstevel@tonic-gate 		(void) cftime(time_buf, dcgettext(NULL, FORMAT, LC_TIME),
14357c478bd9Sstevel@tonic-gate 			&sbuf.st_mtime);
14367c478bd9Sstevel@tonic-gate 		return (time_buf);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate static	char *
14427c478bd9Sstevel@tonic-gate ffiler(char *s)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	static char buf[100];
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, gettext("can't open %s"), s);
14477c478bd9Sstevel@tonic-gate 	return (buf);
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate static	void
14527c478bd9Sstevel@tonic-gate usage(int rc)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
14557c478bd9Sstevel@tonic-gate "usage: pr [-# [-w #] [-a]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]]  \\\n"
14567c478bd9Sstevel@tonic-gate "          [-o #] [-l #] [-s[char]] [-h header] [-F] [+#] [file ...]\n\n"
14577c478bd9Sstevel@tonic-gate "       pr [-m [-w #]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]] [-0 #] \\\n"
14587c478bd9Sstevel@tonic-gate "          [-l #] [-s[char]] [-h header] [-F] [+#] file1 file2 ...\n"
14597c478bd9Sstevel@tonic-gate ));
14607c478bd9Sstevel@tonic-gate 	exit(rc);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate static wint_t
14647c478bd9Sstevel@tonic-gate _fgetwc_pr(FILE *f, int *ic)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	int	i;
14677c478bd9Sstevel@tonic-gate 	int	len;
14687c478bd9Sstevel@tonic-gate 	char	mbuf[MB_LEN_MAX];
14697c478bd9Sstevel@tonic-gate 	int	c;
14707c478bd9Sstevel@tonic-gate 	wchar_t	wc;
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	c = getc(f);
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (c == EOF)
14757c478bd9Sstevel@tonic-gate 		return (WEOF);
14767c478bd9Sstevel@tonic-gate 	if (mbcurmax == 1 || isascii(c)) {
14777c478bd9Sstevel@tonic-gate 		return ((wint_t)c);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 	mbuf[0] = (char)c;
14807c478bd9Sstevel@tonic-gate 	for (i = 1; i < mbcurmax; i++) {
14817c478bd9Sstevel@tonic-gate 		c = getc(f);
14827c478bd9Sstevel@tonic-gate 		if (c == EOF) {
14837c478bd9Sstevel@tonic-gate 			break;
14847c478bd9Sstevel@tonic-gate 		} else {
14857c478bd9Sstevel@tonic-gate 			mbuf[i] = (char)c;
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate 	mbuf[i] = 0;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	len = mbtowc(&wc, mbuf, i);
14917c478bd9Sstevel@tonic-gate 	if (len == -1) {
14927c478bd9Sstevel@tonic-gate 		/* Illegal character */
14937c478bd9Sstevel@tonic-gate 		/* Set the first byte to *ic */
14947c478bd9Sstevel@tonic-gate 		*ic = mbuf[0];
14957c478bd9Sstevel@tonic-gate 		/* Push back remaining characters */
14967c478bd9Sstevel@tonic-gate 		for (i--; i > 0; i--) {
14977c478bd9Sstevel@tonic-gate 			(void) ungetc(mbuf[i], f);
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 		errno = EILSEQ;
15007c478bd9Sstevel@tonic-gate 		return (WEOF);
15017c478bd9Sstevel@tonic-gate 	} else {
15027c478bd9Sstevel@tonic-gate 		/* Push back over-read characters */
15037c478bd9Sstevel@tonic-gate 		for (i--; i >= len; i--) {
15047c478bd9Sstevel@tonic-gate 			(void) ungetc(mbuf[i], f);
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 		return ((wint_t)wc);
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate static size_t
15117c478bd9Sstevel@tonic-gate freadw(wchar_t *ptr, size_t nitems, FILE *f)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	size_t	i;
15147c478bd9Sstevel@tonic-gate 	size_t	ret;
15157c478bd9Sstevel@tonic-gate 	int	c;
15167c478bd9Sstevel@tonic-gate 	wchar_t	*p;
15177c478bd9Sstevel@tonic-gate 	wint_t	wc;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	if (feof(f)) {
15207c478bd9Sstevel@tonic-gate 		return (0);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	p = ptr;
15247c478bd9Sstevel@tonic-gate 	ret = 0;
15257c478bd9Sstevel@tonic-gate 	for (i = 0; i < nitems; i++) {
15267c478bd9Sstevel@tonic-gate 		errno = 0;
15277c478bd9Sstevel@tonic-gate 		wc = _fgetwc_pr(f, &c);
15287c478bd9Sstevel@tonic-gate 		if (wc == WEOF) {
15297c478bd9Sstevel@tonic-gate 			if (errno == EILSEQ) {
15307c478bd9Sstevel@tonic-gate 				*p++ = (wchar_t)c;
15317c478bd9Sstevel@tonic-gate 				ret++;
15327c478bd9Sstevel@tonic-gate 			} else {
15337c478bd9Sstevel@tonic-gate 				return (ret);
15347c478bd9Sstevel@tonic-gate 			}
15357c478bd9Sstevel@tonic-gate 		} else {
15367c478bd9Sstevel@tonic-gate 			*p++ = (wchar_t)wc;
15377c478bd9Sstevel@tonic-gate 			ret++;
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 	return (ret);
15417c478bd9Sstevel@tonic-gate }
1542