xref: /onnv-gate/usr/src/cmd/cat/cat.c (revision 7311:b0f136ddcae6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54305Scasper  * Common Development and Distribution License (the "License").
64305Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
267303SJohn.Sonnenschein@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	Concatenate files.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include	<stdio.h>
360Sstevel@tonic-gate #include	<stdlib.h>
370Sstevel@tonic-gate #include	<ctype.h>
380Sstevel@tonic-gate #include	<sys/types.h>
390Sstevel@tonic-gate #include	<sys/stat.h>
400Sstevel@tonic-gate #include	<locale.h>
410Sstevel@tonic-gate #include	<unistd.h>
420Sstevel@tonic-gate #include	<sys/mman.h>
434305Scasper #include	<errno.h>
444305Scasper #include	<string.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include	<widec.h>
470Sstevel@tonic-gate #include	<wctype.h>
480Sstevel@tonic-gate #include	<limits.h>
490Sstevel@tonic-gate #include	<libintl.h>
500Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	MAXMAPSIZE	(8*1024*1024)	/* map at most 8MB */
530Sstevel@tonic-gate #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little files */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static int vncat(FILE *);
560Sstevel@tonic-gate static int cat(FILE *, struct stat *, struct stat *, char *);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int	silent = 0;		/* s flag */
590Sstevel@tonic-gate static int	visi_mode = 0;		/* v flag */
600Sstevel@tonic-gate static int	visi_tab = 0;		/* t flag */
610Sstevel@tonic-gate static int	visi_newline = 0;	/* e flag */
620Sstevel@tonic-gate static int	bflg = 0;		/* b flag */
630Sstevel@tonic-gate static int	nflg = 0;		/* n flag */
640Sstevel@tonic-gate static long	ibsize;
650Sstevel@tonic-gate static long	obsize;
660Sstevel@tonic-gate static unsigned	char	buf[SMALLFILESIZE];
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 
690Sstevel@tonic-gate int
main(int argc,char ** argv)700Sstevel@tonic-gate main(int argc, char **argv)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	FILE *fi;
730Sstevel@tonic-gate 	int c;
740Sstevel@tonic-gate 	extern	int optind;
750Sstevel@tonic-gate 	int	errflg = 0;
760Sstevel@tonic-gate 	int	stdinflg = 0;
770Sstevel@tonic-gate 	int	status = 0;
780Sstevel@tonic-gate 	int	estatus = 0;
790Sstevel@tonic-gate 	struct stat source, target;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
820Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
830Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #ifdef STANDALONE
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 * If the first argument is NULL,
900Sstevel@tonic-gate 	 * discard arguments until we find cat.
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	if (argv[0][0] == '\0')
930Sstevel@tonic-gate 		argc = getargv("cat", &argv, 0);
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/*
970Sstevel@tonic-gate 	 * Process the options for cat.
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "usvtebn")) != EOF) {
1010Sstevel@tonic-gate 		switch (c) {
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		case 'u':
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 			/*
1060Sstevel@tonic-gate 			 * If not standalone, set stdout to
1070Sstevel@tonic-gate 			 * completely unbuffered I/O when
1080Sstevel@tonic-gate 			 * the 'u' option is used.
1090Sstevel@tonic-gate 			 */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #ifndef	STANDALONE
1120Sstevel@tonic-gate 			setbuf(stdout, (char *)NULL);
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate 			continue;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		case 's':
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			/*
1190Sstevel@tonic-gate 			 * The 's' option requests silent mode
1200Sstevel@tonic-gate 			 * where no messages are written.
1210Sstevel@tonic-gate 			 */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 			silent++;
1240Sstevel@tonic-gate 			continue;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		case 'v':
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 			/*
1290Sstevel@tonic-gate 			 * The 'v' option requests that non-printing
1300Sstevel@tonic-gate 			 * characters (with the exception of newlines,
1310Sstevel@tonic-gate 			 * form-feeds, and tabs) be displayed visibly.
1320Sstevel@tonic-gate 			 *
1330Sstevel@tonic-gate 			 * Control characters are printed as "^x".
1340Sstevel@tonic-gate 			 * DEL characters are printed as "^?".
1350Sstevel@tonic-gate 			 * Non-printable  and non-contrlol characters with the
1360Sstevel@tonic-gate 			 * 8th bit set are printed as "M-x".
1370Sstevel@tonic-gate 			 */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 			visi_mode++;
1400Sstevel@tonic-gate 			continue;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		case 't':
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 			/*
1450Sstevel@tonic-gate 			 * When in visi_mode, this option causes tabs
1460Sstevel@tonic-gate 			 * to be displayed as "^I".
1470Sstevel@tonic-gate 			 */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 			visi_tab++;
1500Sstevel@tonic-gate 			continue;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		case 'e':
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 			/*
1550Sstevel@tonic-gate 			 * When in visi_mode, this option causes newlines
1560Sstevel@tonic-gate 			 * and form-feeds to be displayed as "$" at the end
1570Sstevel@tonic-gate 			 * of the line prior to the newline.
1580Sstevel@tonic-gate 			 */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 			visi_newline++;
1610Sstevel@tonic-gate 			continue;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		case 'b':
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 			/*
1660Sstevel@tonic-gate 			 * Precede each line output with its line number,
1670Sstevel@tonic-gate 			 * but omit the line numbers from blank lines.
1680Sstevel@tonic-gate 			 */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 			bflg++;
1710Sstevel@tonic-gate 			nflg++;
1720Sstevel@tonic-gate 			continue;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		case 'n':
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 			/*
1770Sstevel@tonic-gate 			 * Precede each line output with its line number.
1780Sstevel@tonic-gate 			 */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			nflg++;
1810Sstevel@tonic-gate 			continue;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		case '?':
1840Sstevel@tonic-gate 			errflg++;
1850Sstevel@tonic-gate 			break;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		break;
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if (errflg) {
1910Sstevel@tonic-gate 		if (!silent)
1920Sstevel@tonic-gate 			(void) fprintf(stderr,
1930Sstevel@tonic-gate 			    gettext("usage: cat [ -usvtebn ] [-|file] ...\n"));
1940Sstevel@tonic-gate 		exit(2);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/*
1980Sstevel@tonic-gate 	 * Stat stdout to be sure it is defined.
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (fstat(fileno(stdout), &target) < 0) {
2020Sstevel@tonic-gate 		if (!silent)
2030Sstevel@tonic-gate 			(void) fprintf(stderr,
2040Sstevel@tonic-gate 			    gettext("cat: Cannot stat stdout\n"));
2050Sstevel@tonic-gate 		exit(2);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	obsize = target.st_blksize;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * If no arguments given, then use stdin for input.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (optind == argc) {
2140Sstevel@tonic-gate 		argc++;
2150Sstevel@tonic-gate 		stdinflg++;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Process each remaining argument,
2200Sstevel@tonic-gate 	 * unless there is an error with stdout.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	for (argv = &argv[optind];
2250Sstevel@tonic-gate 	    optind < argc && !ferror(stdout); optind++, argv++) {
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		/*
2280Sstevel@tonic-gate 		 * If the argument was '-' or there were no files
2290Sstevel@tonic-gate 		 * specified, take the input from stdin.
2300Sstevel@tonic-gate 		 */
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		if (stdinflg ||
2330Sstevel@tonic-gate 		    ((*argv)[0] == '-' && (*argv)[1] == '\0'))
2340Sstevel@tonic-gate 			fi = stdin;
2350Sstevel@tonic-gate 		else {
2360Sstevel@tonic-gate 			/*
2370Sstevel@tonic-gate 			 * Attempt to open each specified file.
2380Sstevel@tonic-gate 			 */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 			if ((fi = fopen(*argv, "r")) == NULL) {
2410Sstevel@tonic-gate 				if (!silent)
242*7311SJohn.Sonnenschein@Sun.COM 					(void) fprintf(stderr, gettext(
243*7311SJohn.Sonnenschein@Sun.COM 					    "cat: cannot open %s: %s\n"),
244*7311SJohn.Sonnenschein@Sun.COM 					    *argv, strerror(errno));
2450Sstevel@tonic-gate 				status = 2;
2460Sstevel@tonic-gate 				continue;
2470Sstevel@tonic-gate 			}
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		/*
2510Sstevel@tonic-gate 		 * Stat source to make sure it is defined.
2520Sstevel@tonic-gate 		 */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		if (fstat(fileno(fi), &source) < 0) {
2550Sstevel@tonic-gate 			if (!silent)
2560Sstevel@tonic-gate 				(void) fprintf(stderr,
2574305Scasper 				    gettext("cat: cannot stat %s: %s\n"),
2584305Scasper 				    (stdinflg) ? "-" : *argv, strerror(errno));
2590Sstevel@tonic-gate 			status = 2;
2600Sstevel@tonic-gate 			continue;
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		/*
2650Sstevel@tonic-gate 		 * If the source is not a character special file, socket or a
2660Sstevel@tonic-gate 		 * block special file, make sure it is not identical
2670Sstevel@tonic-gate 		 * to the target.
2680Sstevel@tonic-gate 		 */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		if (!S_ISCHR(target.st_mode) &&
2710Sstevel@tonic-gate 		    !S_ISBLK(target.st_mode) &&
2720Sstevel@tonic-gate 		    !S_ISSOCK(target.st_mode) &&
2730Sstevel@tonic-gate 		    IDENTICAL(target, source)) {
2740Sstevel@tonic-gate 			if (!silent)
2757303SJohn.Sonnenschein@Sun.COM 			(void) fprintf(stderr,
2760Sstevel@tonic-gate 			    gettext("cat: input/output files '%s' identical\n"),
2777303SJohn.Sonnenschein@Sun.COM 			    stdinflg?"-": *argv);
2780Sstevel@tonic-gate 			if (fclose(fi) != 0)
2790Sstevel@tonic-gate 				(void) fprintf(stderr,
2804305Scasper 				    gettext("cat: close error: %s\n"),
2814305Scasper 				    strerror(errno));
2820Sstevel@tonic-gate 			status = 2;
2830Sstevel@tonic-gate 			continue;
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 		ibsize = source.st_blksize;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		/*
2880Sstevel@tonic-gate 		 * If in visible mode and/or nflg, use vncat;
2890Sstevel@tonic-gate 		 * otherwise, use cat.
2900Sstevel@tonic-gate 		 */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		if (visi_mode || nflg)
2930Sstevel@tonic-gate 			estatus = vncat(fi);
2940Sstevel@tonic-gate 		else
2950Sstevel@tonic-gate 			estatus = cat(fi, &source, &target,
2960Sstevel@tonic-gate 			    fi != stdin ? *argv : "standard input");
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		if (estatus)
2990Sstevel@tonic-gate 			status = estatus;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		/*
3020Sstevel@tonic-gate 		 * If the input is not stdin, close the source file.
3030Sstevel@tonic-gate 		 */
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if (fi != stdin) {
3060Sstevel@tonic-gate 			if (fclose(fi) != 0)
3070Sstevel@tonic-gate 				if (!silent)
3080Sstevel@tonic-gate 					(void) fprintf(stderr,
3094305Scasper 					    gettext("cat: close error: %s\n"),
3104305Scasper 					    strerror(errno));
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * Display any error with stdout operations.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
3190Sstevel@tonic-gate 		if (!silent)
3200Sstevel@tonic-gate 			perror(gettext("cat: close error"));
3210Sstevel@tonic-gate 		status = 2;
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	return (status);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static int
cat(FILE * fi,struct stat * statp,struct stat * outp,char * filenm)3290Sstevel@tonic-gate cat(FILE *fi, struct stat *statp, struct stat *outp, char *filenm)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	int nitems;
3320Sstevel@tonic-gate 	int nwritten;
3330Sstevel@tonic-gate 	int offset;
3340Sstevel@tonic-gate 	int fi_desc;
3350Sstevel@tonic-gate 	long buffsize;
3360Sstevel@tonic-gate 	char *bufferp;
3370Sstevel@tonic-gate 	off_t mapsize, munmapsize;
3380Sstevel@tonic-gate 	off_t filesize;
3390Sstevel@tonic-gate 	off_t mapoffset;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	fi_desc = fileno(fi);
3420Sstevel@tonic-gate 	if (S_ISREG(statp->st_mode) && (lseek(fi_desc, (off_t)0, SEEK_CUR)
3430Sstevel@tonic-gate 	    == 0) && (statp->st_size > SMALLFILESIZE)) {
3440Sstevel@tonic-gate 		mapsize = (off_t)MAXMAPSIZE;
3450Sstevel@tonic-gate 		if (statp->st_size < mapsize)
3460Sstevel@tonic-gate 			mapsize = statp->st_size;
3470Sstevel@tonic-gate 		munmapsize = mapsize;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		/*
3500Sstevel@tonic-gate 		 * Mmap time!
3510Sstevel@tonic-gate 		 */
3520Sstevel@tonic-gate 		bufferp = mmap((caddr_t)NULL, (size_t)mapsize, PROT_READ,
3537303SJohn.Sonnenschein@Sun.COM 		    MAP_SHARED, fi_desc, (off_t)0);
3540Sstevel@tonic-gate 		if (bufferp == (caddr_t)-1)
3550Sstevel@tonic-gate 			mapsize = 0;	/* I guess we can't mmap today */
3560Sstevel@tonic-gate 	} else
3570Sstevel@tonic-gate 		mapsize = 0;		/* can't mmap non-regular files */
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (mapsize != 0) {
3600Sstevel@tonic-gate 		int	read_error = 0;
3610Sstevel@tonic-gate 		char	x;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		/*
3640Sstevel@tonic-gate 		 * NFS V2 will let root open a file it does not have permission
3650Sstevel@tonic-gate 		 * to read. This read() is here to make sure that the access
3660Sstevel@tonic-gate 		 * time on the input file will be updated. The VSC tests for
3670Sstevel@tonic-gate 		 * cat do this:
3680Sstevel@tonic-gate 		 *	cat file > /dev/null
3690Sstevel@tonic-gate 		 * In this case the write()/mmap() pair will not read the file
3700Sstevel@tonic-gate 		 * and the access time will not be updated.
3710Sstevel@tonic-gate 		 */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		if (read(fi_desc, &x, 1) == -1)
3740Sstevel@tonic-gate 			read_error = 1;
3750Sstevel@tonic-gate 		mapoffset = 0;
3760Sstevel@tonic-gate 		filesize = statp->st_size;
3770Sstevel@tonic-gate 		for (;;) {
3780Sstevel@tonic-gate 			/*
3790Sstevel@tonic-gate 			 * Note that on some systems (V7), very large writes to
3800Sstevel@tonic-gate 			 * a pipe return less than the requested size of the
3810Sstevel@tonic-gate 			 * write.  In this case, multiple writes are required.
3820Sstevel@tonic-gate 			 */
3830Sstevel@tonic-gate 			offset = 0;
3840Sstevel@tonic-gate 			nitems = (int)mapsize;
3850Sstevel@tonic-gate 			do {
3860Sstevel@tonic-gate 				if ((nwritten = write(fileno(stdout),
3870Sstevel@tonic-gate 				    &bufferp[offset], (size_t)nitems)) < 0) {
3880Sstevel@tonic-gate 					if (!silent) {
3890Sstevel@tonic-gate 						if (read_error == 1)
3900Sstevel@tonic-gate 							(void) fprintf(
3910Sstevel@tonic-gate 							    stderr, gettext(
3920Sstevel@tonic-gate 							    "cat: cannot read "
3930Sstevel@tonic-gate 							    "%s: "), filenm);
3940Sstevel@tonic-gate 						else
395*7311SJohn.Sonnenschein@Sun.COM 							(void) fprintf(stderr,
396*7311SJohn.Sonnenschein@Sun.COM 							    gettext(
397*7311SJohn.Sonnenschein@Sun.COM 							    "cat: write "
398*7311SJohn.Sonnenschein@Sun.COM 							    "error: "));
3990Sstevel@tonic-gate 						perror("");
4000Sstevel@tonic-gate 					}
4010Sstevel@tonic-gate 					(void) munmap(bufferp,
4027303SJohn.Sonnenschein@Sun.COM 					    (size_t)munmapsize);
4030Sstevel@tonic-gate 					(void) lseek(fi_desc, (off_t)mapoffset,
4040Sstevel@tonic-gate 					    SEEK_SET);
4050Sstevel@tonic-gate 					return (2);
4060Sstevel@tonic-gate 				}
4070Sstevel@tonic-gate 				offset += nwritten;
4080Sstevel@tonic-gate 			} while ((nitems -= nwritten) > 0);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			filesize -= mapsize;
4110Sstevel@tonic-gate 			mapoffset += mapsize;
4120Sstevel@tonic-gate 			if (filesize == 0)
4130Sstevel@tonic-gate 				break;
4140Sstevel@tonic-gate 			if (filesize < mapsize)
4150Sstevel@tonic-gate 				mapsize = filesize;
4160Sstevel@tonic-gate 			if (mmap(bufferp, (size_t)mapsize, PROT_READ,
4170Sstevel@tonic-gate 			    MAP_SHARED|MAP_FIXED, fi_desc,
4180Sstevel@tonic-gate 			    mapoffset) == (caddr_t)-1) {
4190Sstevel@tonic-gate 				if (!silent)
4200Sstevel@tonic-gate 					perror(gettext("cat: mmap error"));
4210Sstevel@tonic-gate 				(void) munmap(bufferp, (size_t)munmapsize);
4220Sstevel@tonic-gate 				(void) lseek(fi_desc, (off_t)mapoffset,
4237303SJohn.Sonnenschein@Sun.COM 				    SEEK_SET);
4240Sstevel@tonic-gate 				return (1);
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 		}
4270Sstevel@tonic-gate 		/*
4280Sstevel@tonic-gate 		 * Move the file pointer past what we read. Shell scripts
4290Sstevel@tonic-gate 		 * rely on cat to do this, so that successive commands in
4300Sstevel@tonic-gate 		 * the script won't re-read the same data.
4310Sstevel@tonic-gate 		 */
4320Sstevel@tonic-gate 		(void) lseek(fi_desc, (off_t)mapoffset, SEEK_SET);
4330Sstevel@tonic-gate 		(void) munmap(bufferp, (size_t)munmapsize);
4340Sstevel@tonic-gate 	} else {
435871Scasper 		if (S_ISREG(statp->st_mode) && S_ISREG(outp->st_mode)) {
4360Sstevel@tonic-gate 			bufferp = (char *)buf;
4370Sstevel@tonic-gate 			buffsize = SMALLFILESIZE;
4380Sstevel@tonic-gate 		} else {
4390Sstevel@tonic-gate 			if (obsize)
4400Sstevel@tonic-gate 				/*
4410Sstevel@tonic-gate 				 * common case, use output blksize
4420Sstevel@tonic-gate 				 */
4430Sstevel@tonic-gate 				buffsize = obsize;
4440Sstevel@tonic-gate 			else if (ibsize)
4450Sstevel@tonic-gate 				buffsize = ibsize;
4460Sstevel@tonic-gate 			else
4470Sstevel@tonic-gate 				buffsize = (long)BUFSIZ;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 			if (buffsize <= SMALLFILESIZE) {
4500Sstevel@tonic-gate 				bufferp = (char *)buf;
4510Sstevel@tonic-gate 			} else if ((bufferp =
4520Sstevel@tonic-gate 			    malloc((size_t)buffsize)) == NULL) {
4530Sstevel@tonic-gate 				perror(gettext("cat: no memory"));
4540Sstevel@tonic-gate 				return (1);
4550Sstevel@tonic-gate 			}
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/*
4590Sstevel@tonic-gate 		 * While not end of file, copy blocks to stdout.
4600Sstevel@tonic-gate 		 */
4610Sstevel@tonic-gate 		while ((nitems = read(fi_desc, bufferp, (size_t)buffsize)) >
4620Sstevel@tonic-gate 		    0) {
4630Sstevel@tonic-gate 			offset = 0;
4640Sstevel@tonic-gate 			/*
4650Sstevel@tonic-gate 			 * Note that on some systems (V7), very large writes
4660Sstevel@tonic-gate 			 * to a pipe return less than the requested size of
4670Sstevel@tonic-gate 			 * the write.  In this case, multiple writes are
4680Sstevel@tonic-gate 			 * required.
4690Sstevel@tonic-gate 			 */
4700Sstevel@tonic-gate 			do {
4710Sstevel@tonic-gate 				nwritten = write(1, bufferp+offset,
4727303SJohn.Sonnenschein@Sun.COM 				    (size_t)nitems);
4730Sstevel@tonic-gate 				if (nwritten < 0) {
4740Sstevel@tonic-gate 					if (!silent) {
4750Sstevel@tonic-gate 						if (nwritten == -1)
4760Sstevel@tonic-gate 							nwritten = 0l;
4770Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(\
4780Sstevel@tonic-gate "cat: output error (%d/%d characters written)\n"), nwritten, nitems);
4790Sstevel@tonic-gate 						perror("");
4800Sstevel@tonic-gate 					}
4810Sstevel@tonic-gate 					if (bufferp != (char *)buf)
4820Sstevel@tonic-gate 						free(bufferp);
4830Sstevel@tonic-gate 					return (2);
4840Sstevel@tonic-gate 				}
4850Sstevel@tonic-gate 				offset += nwritten;
4860Sstevel@tonic-gate 			} while ((nitems -= nwritten) > 0);
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		if (bufferp != (char *)buf)
4890Sstevel@tonic-gate 			free(bufferp);
4900Sstevel@tonic-gate 		if (nitems < 0) {
4910Sstevel@tonic-gate 			(void) fprintf(stderr,
4920Sstevel@tonic-gate 			    gettext("cat: input error on %s: "), filenm);
4930Sstevel@tonic-gate 			perror("");
4947303SJohn.Sonnenschein@Sun.COM 			return (1);
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	return (0);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static int
vncat(fi)5020Sstevel@tonic-gate vncat(fi)
5030Sstevel@tonic-gate 	FILE *fi;
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	int c;
5060Sstevel@tonic-gate 	int	lno;
5070Sstevel@tonic-gate 	int	boln;	/* = 1 if at beginning of line */
5080Sstevel@tonic-gate 			/* = 0 otherwise */
5090Sstevel@tonic-gate 	wchar_t	wc;
5100Sstevel@tonic-gate 	int	len, n;
5110Sstevel@tonic-gate 	unsigned char	*p1, *p2;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	lno = 1;
5140Sstevel@tonic-gate 	boln = 1;
5150Sstevel@tonic-gate 	p1 = p2 = buf;
5160Sstevel@tonic-gate 	for (;;) {
5170Sstevel@tonic-gate 		if (p1 >= p2) {
5180Sstevel@tonic-gate 			p1 = buf;
5190Sstevel@tonic-gate 			if ((len = fread(p1, 1, BUFSIZ, fi)) <= 0)
5200Sstevel@tonic-gate 				break;
5210Sstevel@tonic-gate 			p2 = p1 + len;
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 		c = *p1++;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * Display newlines as "$<newline>"
5270Sstevel@tonic-gate 		 * if visi_newline set
5280Sstevel@tonic-gate 		 */
5290Sstevel@tonic-gate 		if (c == '\n') {
5300Sstevel@tonic-gate 			if (nflg && boln && !bflg)
5310Sstevel@tonic-gate 				(void) printf("%6d\t", lno++);
5320Sstevel@tonic-gate 			boln = 1;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 			if (visi_mode && visi_newline)
5350Sstevel@tonic-gate 				(void) putchar('$');
5360Sstevel@tonic-gate 			(void) putchar(c);
5370Sstevel@tonic-gate 			continue;
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		if (nflg && boln)
5410Sstevel@tonic-gate 			(void) printf("%6d\t", lno++);
5420Sstevel@tonic-gate 		boln = 0;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 		/*
5450Sstevel@tonic-gate 		 * For non-printable and non-cntrl chars,
5460Sstevel@tonic-gate 		 * use the "M-x" notation.
5470Sstevel@tonic-gate 		 */
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		if (isascii(c)) {
5500Sstevel@tonic-gate 			if (isprint(c) || visi_mode == 0) {
5510Sstevel@tonic-gate 				(void) putchar(c);
5520Sstevel@tonic-gate 				continue;
5530Sstevel@tonic-gate 			}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 			/*
5560Sstevel@tonic-gate 			 * For non-printable ascii characters.
5570Sstevel@tonic-gate 			 */
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 			if (iscntrl(c)) {
5600Sstevel@tonic-gate 				/* For cntrl characters. */
5610Sstevel@tonic-gate 				if ((c == '\t') || (c == '\f')) {
5620Sstevel@tonic-gate 					/*
5630Sstevel@tonic-gate 					 * Display tab as "^I" if visi_tab set
5640Sstevel@tonic-gate 					 */
5650Sstevel@tonic-gate 					if (visi_mode && visi_tab) {
5660Sstevel@tonic-gate 						(void) putchar('^');
5670Sstevel@tonic-gate 						(void) putchar(c^0100);
5680Sstevel@tonic-gate 					} else
5690Sstevel@tonic-gate 						(void) putchar(c);
5700Sstevel@tonic-gate 					continue;
5710Sstevel@tonic-gate 				}
5720Sstevel@tonic-gate 				(void) putchar('^');
5730Sstevel@tonic-gate 				(void) putchar(c^0100);
5740Sstevel@tonic-gate 				continue;
5750Sstevel@tonic-gate 			}
5760Sstevel@tonic-gate 			continue;
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		/*
5800Sstevel@tonic-gate 		 * For non-ascii characters.
5810Sstevel@tonic-gate 		 */
5820Sstevel@tonic-gate 		p1--;
5830Sstevel@tonic-gate 		if ((len = (p2 - p1)) < MB_LEN_MAX) {
5840Sstevel@tonic-gate 			for (n = 0; n < len; n++)
5850Sstevel@tonic-gate 				buf[n] = *p1++;
5860Sstevel@tonic-gate 			p1 = buf;
5870Sstevel@tonic-gate 			p2 = p1 + n;
5880Sstevel@tonic-gate 			if ((len = fread(p2, 1, BUFSIZ - n, fi)) > 0)
5890Sstevel@tonic-gate 				p2 += len;
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		if ((len = (p2 - p1)) > MB_LEN_MAX)
5930Sstevel@tonic-gate 			len = MB_LEN_MAX;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)p1, len)) > 0) {
5960Sstevel@tonic-gate 			if (iswprint(wc) || visi_mode == 0) {
5970Sstevel@tonic-gate 				(void) putwchar(wc);
5980Sstevel@tonic-gate 				p1 += len;
5990Sstevel@tonic-gate 				continue;
6000Sstevel@tonic-gate 			}
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		(void) putchar('M');
6040Sstevel@tonic-gate 		(void) putchar('-');
6050Sstevel@tonic-gate 		c -= 0200;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 		if (isprint(c)) {
6080Sstevel@tonic-gate 			(void) putchar(c);
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		/* For non-printable characters. */
6120Sstevel@tonic-gate 		if (iscntrl(c)) {
6130Sstevel@tonic-gate 			/* For cntrl characters. */
6140Sstevel@tonic-gate 			if ((c == '\t') || (c == '\f')) {
6150Sstevel@tonic-gate 				/*
6160Sstevel@tonic-gate 				 * Display tab as "^I" if visi_tab set
6170Sstevel@tonic-gate 				 */
6180Sstevel@tonic-gate 				if (visi_mode && visi_tab) {
6190Sstevel@tonic-gate 					(void) putchar('^');
6200Sstevel@tonic-gate 					(void) putchar(c^0100);
6210Sstevel@tonic-gate 				} else
6220Sstevel@tonic-gate 					(void) putchar(c);
6230Sstevel@tonic-gate 			} else {
6240Sstevel@tonic-gate 				(void) putchar('^');
6250Sstevel@tonic-gate 				(void) putchar(c^0100);
6260Sstevel@tonic-gate 			}
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 		p1++;
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	return (0);
6310Sstevel@tonic-gate }
632