xref: /illumos-gate/usr/src/cmd/cat/cat.c (revision 5b095dea)
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
5*5b095deaScasper  * Common Development and Distribution License (the "License").
6*5b095deaScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*5b095deaScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	Concatenate files.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<stdlib.h>
387c478bd9Sstevel@tonic-gate #include	<ctype.h>
397c478bd9Sstevel@tonic-gate #include	<sys/types.h>
407c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
417c478bd9Sstevel@tonic-gate #include	<locale.h>
427c478bd9Sstevel@tonic-gate #include	<unistd.h>
437c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
44*5b095deaScasper #include	<errno.h>
45*5b095deaScasper #include	<string.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include	<widec.h>
487c478bd9Sstevel@tonic-gate #include	<wctype.h>
497c478bd9Sstevel@tonic-gate #include	<limits.h>
507c478bd9Sstevel@tonic-gate #include	<libintl.h>
517c478bd9Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	MAXMAPSIZE	(8*1024*1024)	/* map at most 8MB */
547c478bd9Sstevel@tonic-gate #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little files */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int vncat(FILE *);
577c478bd9Sstevel@tonic-gate static int cat(FILE *, struct stat *, struct stat *, char *);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int	silent = 0;		/* s flag */
607c478bd9Sstevel@tonic-gate static int	visi_mode = 0;		/* v flag */
617c478bd9Sstevel@tonic-gate static int	visi_tab = 0;		/* t flag */
627c478bd9Sstevel@tonic-gate static int	visi_newline = 0;	/* e flag */
637c478bd9Sstevel@tonic-gate static int	bflg = 0;		/* b flag */
647c478bd9Sstevel@tonic-gate static int	nflg = 0;		/* n flag */
657c478bd9Sstevel@tonic-gate static long	ibsize;
667c478bd9Sstevel@tonic-gate static long	obsize;
677c478bd9Sstevel@tonic-gate static unsigned	char	buf[SMALLFILESIZE];
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
717c478bd9Sstevel@tonic-gate main(int argc, char **argv)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	FILE *fi;
747c478bd9Sstevel@tonic-gate 	int c;
757c478bd9Sstevel@tonic-gate 	extern	int optind;
767c478bd9Sstevel@tonic-gate 	int	errflg = 0;
777c478bd9Sstevel@tonic-gate 	int	stdinflg = 0;
787c478bd9Sstevel@tonic-gate 	int	status = 0;
797c478bd9Sstevel@tonic-gate 	int	estatus = 0;
807c478bd9Sstevel@tonic-gate 	struct stat source, target;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
837c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
847c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef STANDALONE
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * If the first argument is NULL,
917c478bd9Sstevel@tonic-gate 	 * discard arguments until we find cat.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	if (argv[0][0] == '\0')
947c478bd9Sstevel@tonic-gate 		argc = getargv("cat", &argv, 0);
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * Process the options for cat.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "usvtebn")) != EOF) {
1027c478bd9Sstevel@tonic-gate 		switch (c) {
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		case 'u':
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 			/*
1077c478bd9Sstevel@tonic-gate 			 * If not standalone, set stdout to
1087c478bd9Sstevel@tonic-gate 			 * completely unbuffered I/O when
1097c478bd9Sstevel@tonic-gate 			 * the 'u' option is used.
1107c478bd9Sstevel@tonic-gate 			 */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #ifndef	STANDALONE
1137c478bd9Sstevel@tonic-gate 			setbuf(stdout, (char *)NULL);
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 			continue;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		case 's':
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 			/*
1207c478bd9Sstevel@tonic-gate 			 * The 's' option requests silent mode
1217c478bd9Sstevel@tonic-gate 			 * where no messages are written.
1227c478bd9Sstevel@tonic-gate 			 */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 			silent++;
1257c478bd9Sstevel@tonic-gate 			continue;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		case 'v':
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 			/*
1307c478bd9Sstevel@tonic-gate 			 * The 'v' option requests that non-printing
1317c478bd9Sstevel@tonic-gate 			 * characters (with the exception of newlines,
1327c478bd9Sstevel@tonic-gate 			 * form-feeds, and tabs) be displayed visibly.
1337c478bd9Sstevel@tonic-gate 			 *
1347c478bd9Sstevel@tonic-gate 			 * Control characters are printed as "^x".
1357c478bd9Sstevel@tonic-gate 			 * DEL characters are printed as "^?".
1367c478bd9Sstevel@tonic-gate 			 * Non-printable  and non-contrlol characters with the
1377c478bd9Sstevel@tonic-gate 			 * 8th bit set are printed as "M-x".
1387c478bd9Sstevel@tonic-gate 			 */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 			visi_mode++;
1417c478bd9Sstevel@tonic-gate 			continue;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		case 't':
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 			/*
1467c478bd9Sstevel@tonic-gate 			 * When in visi_mode, this option causes tabs
1477c478bd9Sstevel@tonic-gate 			 * to be displayed as "^I".
1487c478bd9Sstevel@tonic-gate 			 */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 			visi_tab++;
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		case 'e':
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 			/*
1567c478bd9Sstevel@tonic-gate 			 * When in visi_mode, this option causes newlines
1577c478bd9Sstevel@tonic-gate 			 * and form-feeds to be displayed as "$" at the end
1587c478bd9Sstevel@tonic-gate 			 * of the line prior to the newline.
1597c478bd9Sstevel@tonic-gate 			 */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 			visi_newline++;
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case 'b':
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 			/*
1677c478bd9Sstevel@tonic-gate 			 * Precede each line output with its line number,
1687c478bd9Sstevel@tonic-gate 			 * but omit the line numbers from blank lines.
1697c478bd9Sstevel@tonic-gate 			 */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			bflg++;
1727c478bd9Sstevel@tonic-gate 			nflg++;
1737c478bd9Sstevel@tonic-gate 			continue;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		case 'n':
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 			/*
1787c478bd9Sstevel@tonic-gate 			 * Precede each line output with its line number.
1797c478bd9Sstevel@tonic-gate 			 */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			nflg++;
1827c478bd9Sstevel@tonic-gate 			continue;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case '?':
1857c478bd9Sstevel@tonic-gate 			errflg++;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (errflg) {
1927c478bd9Sstevel@tonic-gate 		if (!silent)
1937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1947c478bd9Sstevel@tonic-gate 			    gettext("usage: cat [ -usvtebn ] [-|file] ...\n"));
1957c478bd9Sstevel@tonic-gate 		exit(2);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Stat stdout to be sure it is defined.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (fstat(fileno(stdout), &target) < 0) {
2037c478bd9Sstevel@tonic-gate 		if (!silent)
2047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2057c478bd9Sstevel@tonic-gate 			    gettext("cat: Cannot stat stdout\n"));
2067c478bd9Sstevel@tonic-gate 		exit(2);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	obsize = target.st_blksize;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * If no arguments given, then use stdin for input.
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (optind == argc) {
2157c478bd9Sstevel@tonic-gate 		argc++;
2167c478bd9Sstevel@tonic-gate 		stdinflg++;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * Process each remaining argument,
2217c478bd9Sstevel@tonic-gate 	 * unless there is an error with stdout.
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	for (argv = &argv[optind];
2267c478bd9Sstevel@tonic-gate 	    optind < argc && !ferror(stdout); optind++, argv++) {
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		/*
2297c478bd9Sstevel@tonic-gate 		 * If the argument was '-' or there were no files
2307c478bd9Sstevel@tonic-gate 		 * specified, take the input from stdin.
2317c478bd9Sstevel@tonic-gate 		 */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (stdinflg ||
2347c478bd9Sstevel@tonic-gate 		    ((*argv)[0] == '-' && (*argv)[1] == '\0'))
2357c478bd9Sstevel@tonic-gate 			fi = stdin;
2367c478bd9Sstevel@tonic-gate 		else {
2377c478bd9Sstevel@tonic-gate 			/*
2387c478bd9Sstevel@tonic-gate 			 * Attempt to open each specified file.
2397c478bd9Sstevel@tonic-gate 			 */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 			if ((fi = fopen(*argv, "r")) == NULL) {
2427c478bd9Sstevel@tonic-gate 				if (!silent)
2437c478bd9Sstevel@tonic-gate 				    (void) fprintf(stderr,
244*5b095deaScasper 				    gettext("cat: cannot open %s: %s\n"),
245*5b095deaScasper 					    *argv, strerror(errno));
2467c478bd9Sstevel@tonic-gate 				status = 2;
2477c478bd9Sstevel@tonic-gate 				continue;
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		/*
2527c478bd9Sstevel@tonic-gate 		 * Stat source to make sure it is defined.
2537c478bd9Sstevel@tonic-gate 		 */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		if (fstat(fileno(fi), &source) < 0) {
2567c478bd9Sstevel@tonic-gate 			if (!silent)
2577c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
258*5b095deaScasper 				    gettext("cat: cannot stat %s: %s\n"),
259*5b095deaScasper 				    (stdinflg) ? "-" : *argv, strerror(errno));
2607c478bd9Sstevel@tonic-gate 			status = 2;
2617c478bd9Sstevel@tonic-gate 			continue;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		/*
2667c478bd9Sstevel@tonic-gate 		 * If the source is not a character special file, socket or a
2677c478bd9Sstevel@tonic-gate 		 * block special file, make sure it is not identical
2687c478bd9Sstevel@tonic-gate 		 * to the target.
2697c478bd9Sstevel@tonic-gate 		 */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		if (!S_ISCHR(target.st_mode) &&
2727c478bd9Sstevel@tonic-gate 		    !S_ISBLK(target.st_mode) &&
2737c478bd9Sstevel@tonic-gate 		    !S_ISSOCK(target.st_mode) &&
2747c478bd9Sstevel@tonic-gate 		    IDENTICAL(target, source)) {
2757c478bd9Sstevel@tonic-gate 			if (!silent)
2767c478bd9Sstevel@tonic-gate 			    (void) fprintf(stderr,
2777c478bd9Sstevel@tonic-gate 			    gettext("cat: input/output files '%s' identical\n"),
2787c478bd9Sstevel@tonic-gate 				stdinflg?"-": *argv);
2797c478bd9Sstevel@tonic-gate 			if (fclose(fi) != 0)
2807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
281*5b095deaScasper 				    gettext("cat: close error: %s\n"),
282*5b095deaScasper 				    strerror(errno));
2837c478bd9Sstevel@tonic-gate 			status = 2;
2847c478bd9Sstevel@tonic-gate 			continue;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		ibsize = source.st_blksize;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		/*
2897c478bd9Sstevel@tonic-gate 		 * If in visible mode and/or nflg, use vncat;
2907c478bd9Sstevel@tonic-gate 		 * otherwise, use cat.
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		if (visi_mode || nflg)
2947c478bd9Sstevel@tonic-gate 			estatus = vncat(fi);
2957c478bd9Sstevel@tonic-gate 		else
2967c478bd9Sstevel@tonic-gate 			estatus = cat(fi, &source, &target,
2977c478bd9Sstevel@tonic-gate 			    fi != stdin ? *argv : "standard input");
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		if (estatus)
3007c478bd9Sstevel@tonic-gate 			status = estatus;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		/*
3037c478bd9Sstevel@tonic-gate 		 * If the input is not stdin, close the source file.
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		if (fi != stdin) {
3077c478bd9Sstevel@tonic-gate 			if (fclose(fi) != 0)
3087c478bd9Sstevel@tonic-gate 				if (!silent)
3097c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
310*5b095deaScasper 					    gettext("cat: close error: %s\n"),
311*5b095deaScasper 					    strerror(errno));
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Display any error with stdout operations.
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
3207c478bd9Sstevel@tonic-gate 		if (!silent)
3217c478bd9Sstevel@tonic-gate 			perror(gettext("cat: close error"));
3227c478bd9Sstevel@tonic-gate 		status = 2;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	return (status);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate static int
3307c478bd9Sstevel@tonic-gate cat(FILE *fi, struct stat *statp, struct stat *outp, char *filenm)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	int nitems;
3337c478bd9Sstevel@tonic-gate 	int nwritten;
3347c478bd9Sstevel@tonic-gate 	int offset;
3357c478bd9Sstevel@tonic-gate 	int fi_desc;
3367c478bd9Sstevel@tonic-gate 	long buffsize;
3377c478bd9Sstevel@tonic-gate 	char *bufferp;
3387c478bd9Sstevel@tonic-gate 	off_t mapsize, munmapsize;
3397c478bd9Sstevel@tonic-gate 	off_t filesize;
3407c478bd9Sstevel@tonic-gate 	off_t mapoffset;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	fi_desc = fileno(fi);
3437c478bd9Sstevel@tonic-gate 	if (S_ISREG(statp->st_mode) && (lseek(fi_desc, (off_t)0, SEEK_CUR)
3447c478bd9Sstevel@tonic-gate 	    == 0) && (statp->st_size > SMALLFILESIZE)) {
3457c478bd9Sstevel@tonic-gate 		mapsize = (off_t)MAXMAPSIZE;
3467c478bd9Sstevel@tonic-gate 		if (statp->st_size < mapsize)
3477c478bd9Sstevel@tonic-gate 			mapsize = statp->st_size;
3487c478bd9Sstevel@tonic-gate 		munmapsize = mapsize;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		/*
3517c478bd9Sstevel@tonic-gate 		 * Mmap time!
3527c478bd9Sstevel@tonic-gate 		 */
3537c478bd9Sstevel@tonic-gate 		bufferp = mmap((caddr_t)NULL, (size_t)mapsize, PROT_READ,
3547c478bd9Sstevel@tonic-gate 			MAP_SHARED, fi_desc, (off_t)0);
3557c478bd9Sstevel@tonic-gate 		if (bufferp == (caddr_t)-1)
3567c478bd9Sstevel@tonic-gate 			mapsize = 0;	/* I guess we can't mmap today */
3577c478bd9Sstevel@tonic-gate 	} else
3587c478bd9Sstevel@tonic-gate 		mapsize = 0;		/* can't mmap non-regular files */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (mapsize != 0) {
3617c478bd9Sstevel@tonic-gate 		int	read_error = 0;
3627c478bd9Sstevel@tonic-gate 		char	x;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		/*
3657c478bd9Sstevel@tonic-gate 		 * NFS V2 will let root open a file it does not have permission
3667c478bd9Sstevel@tonic-gate 		 * to read. This read() is here to make sure that the access
3677c478bd9Sstevel@tonic-gate 		 * time on the input file will be updated. The VSC tests for
3687c478bd9Sstevel@tonic-gate 		 * cat do this:
3697c478bd9Sstevel@tonic-gate 		 *	cat file > /dev/null
3707c478bd9Sstevel@tonic-gate 		 * In this case the write()/mmap() pair will not read the file
3717c478bd9Sstevel@tonic-gate 		 * and the access time will not be updated.
3727c478bd9Sstevel@tonic-gate 		 */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if (read(fi_desc, &x, 1) == -1)
3757c478bd9Sstevel@tonic-gate 			read_error = 1;
3767c478bd9Sstevel@tonic-gate 		mapoffset = 0;
3777c478bd9Sstevel@tonic-gate 		filesize = statp->st_size;
3787c478bd9Sstevel@tonic-gate 		for (;;) {
3797c478bd9Sstevel@tonic-gate 			/*
3807c478bd9Sstevel@tonic-gate 			 * Note that on some systems (V7), very large writes to
3817c478bd9Sstevel@tonic-gate 			 * a pipe return less than the requested size of the
3827c478bd9Sstevel@tonic-gate 			 * write.  In this case, multiple writes are required.
3837c478bd9Sstevel@tonic-gate 			 */
3847c478bd9Sstevel@tonic-gate 			offset = 0;
3857c478bd9Sstevel@tonic-gate 			nitems = (int)mapsize;
3867c478bd9Sstevel@tonic-gate 			do {
3877c478bd9Sstevel@tonic-gate 				if ((nwritten = write(fileno(stdout),
3887c478bd9Sstevel@tonic-gate 				    &bufferp[offset], (size_t)nitems)) < 0) {
3897c478bd9Sstevel@tonic-gate 					if (!silent) {
3907c478bd9Sstevel@tonic-gate 						if (read_error == 1)
3917c478bd9Sstevel@tonic-gate 							(void) fprintf(
3927c478bd9Sstevel@tonic-gate 							    stderr, gettext(
3937c478bd9Sstevel@tonic-gate 							    "cat: cannot read "
3947c478bd9Sstevel@tonic-gate 							    "%s: "), filenm);
3957c478bd9Sstevel@tonic-gate 						else
3967c478bd9Sstevel@tonic-gate 							(void) fprintf(
3977c478bd9Sstevel@tonic-gate 							stderr, gettext(
3987c478bd9Sstevel@tonic-gate 							"cat: write error: "));
3997c478bd9Sstevel@tonic-gate 						perror("");
4007c478bd9Sstevel@tonic-gate 					}
4017c478bd9Sstevel@tonic-gate 					(void) munmap(bufferp,
4027c478bd9Sstevel@tonic-gate 						(size_t)munmapsize);
4037c478bd9Sstevel@tonic-gate 					(void) lseek(fi_desc, (off_t)mapoffset,
4047c478bd9Sstevel@tonic-gate 					    SEEK_SET);
4057c478bd9Sstevel@tonic-gate 					return (2);
4067c478bd9Sstevel@tonic-gate 				}
4077c478bd9Sstevel@tonic-gate 				offset += nwritten;
4087c478bd9Sstevel@tonic-gate 			} while ((nitems -= nwritten) > 0);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			filesize -= mapsize;
4117c478bd9Sstevel@tonic-gate 			mapoffset += mapsize;
4127c478bd9Sstevel@tonic-gate 			if (filesize == 0)
4137c478bd9Sstevel@tonic-gate 				break;
4147c478bd9Sstevel@tonic-gate 			if (filesize < mapsize)
4157c478bd9Sstevel@tonic-gate 				mapsize = filesize;
4167c478bd9Sstevel@tonic-gate 			if (mmap(bufferp, (size_t)mapsize, PROT_READ,
4177c478bd9Sstevel@tonic-gate 			    MAP_SHARED|MAP_FIXED, fi_desc,
4187c478bd9Sstevel@tonic-gate 			    mapoffset) == (caddr_t)-1) {
4197c478bd9Sstevel@tonic-gate 				if (!silent)
4207c478bd9Sstevel@tonic-gate 					perror(gettext("cat: mmap error"));
4217c478bd9Sstevel@tonic-gate 				(void) munmap(bufferp, (size_t)munmapsize);
4227c478bd9Sstevel@tonic-gate 				(void) lseek(fi_desc, (off_t)mapoffset,
4237c478bd9Sstevel@tonic-gate 					SEEK_SET);
4247c478bd9Sstevel@tonic-gate 				return (1);
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		/*
4287c478bd9Sstevel@tonic-gate 		 * Move the file pointer past what we read. Shell scripts
4297c478bd9Sstevel@tonic-gate 		 * rely on cat to do this, so that successive commands in
4307c478bd9Sstevel@tonic-gate 		 * the script won't re-read the same data.
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		(void) lseek(fi_desc, (off_t)mapoffset, SEEK_SET);
4337c478bd9Sstevel@tonic-gate 		(void) munmap(bufferp, (size_t)munmapsize);
4347c478bd9Sstevel@tonic-gate 	} else {
4354bc0a2efScasper 		if (S_ISREG(statp->st_mode) && S_ISREG(outp->st_mode)) {
4367c478bd9Sstevel@tonic-gate 			bufferp = (char *)buf;
4377c478bd9Sstevel@tonic-gate 			buffsize = SMALLFILESIZE;
4387c478bd9Sstevel@tonic-gate 		} else {
4397c478bd9Sstevel@tonic-gate 			if (obsize)
4407c478bd9Sstevel@tonic-gate 				/*
4417c478bd9Sstevel@tonic-gate 				 * common case, use output blksize
4427c478bd9Sstevel@tonic-gate 				 */
4437c478bd9Sstevel@tonic-gate 				buffsize = obsize;
4447c478bd9Sstevel@tonic-gate 			else if (ibsize)
4457c478bd9Sstevel@tonic-gate 				buffsize = ibsize;
4467c478bd9Sstevel@tonic-gate 			else
4477c478bd9Sstevel@tonic-gate 				buffsize = (long)BUFSIZ;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 			if (buffsize <= SMALLFILESIZE) {
4507c478bd9Sstevel@tonic-gate 				bufferp = (char *)buf;
4517c478bd9Sstevel@tonic-gate 			} else if ((bufferp =
4527c478bd9Sstevel@tonic-gate 			    malloc((size_t)buffsize)) == NULL) {
4537c478bd9Sstevel@tonic-gate 				perror(gettext("cat: no memory"));
4547c478bd9Sstevel@tonic-gate 				return (1);
4557c478bd9Sstevel@tonic-gate 			}
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		/*
4597c478bd9Sstevel@tonic-gate 		 * While not end of file, copy blocks to stdout.
4607c478bd9Sstevel@tonic-gate 		 */
4617c478bd9Sstevel@tonic-gate 		while ((nitems = read(fi_desc, bufferp, (size_t)buffsize)) >
4627c478bd9Sstevel@tonic-gate 		    0) {
4637c478bd9Sstevel@tonic-gate 			offset = 0;
4647c478bd9Sstevel@tonic-gate 			/*
4657c478bd9Sstevel@tonic-gate 			 * Note that on some systems (V7), very large writes
4667c478bd9Sstevel@tonic-gate 			 * to a pipe return less than the requested size of
4677c478bd9Sstevel@tonic-gate 			 * the write.  In this case, multiple writes are
4687c478bd9Sstevel@tonic-gate 			 * required.
4697c478bd9Sstevel@tonic-gate 			 */
4707c478bd9Sstevel@tonic-gate 			do {
4717c478bd9Sstevel@tonic-gate 				nwritten = write(1, bufferp+offset,
4727c478bd9Sstevel@tonic-gate 					(size_t)nitems);
4737c478bd9Sstevel@tonic-gate 				if (nwritten < 0) {
4747c478bd9Sstevel@tonic-gate 					if (!silent) {
4757c478bd9Sstevel@tonic-gate 						if (nwritten == -1)
4767c478bd9Sstevel@tonic-gate 							nwritten = 0l;
4777c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(\
4787c478bd9Sstevel@tonic-gate "cat: output error (%d/%d characters written)\n"), nwritten, nitems);
4797c478bd9Sstevel@tonic-gate 						perror("");
4807c478bd9Sstevel@tonic-gate 					}
4817c478bd9Sstevel@tonic-gate 					if (bufferp != (char *)buf)
4827c478bd9Sstevel@tonic-gate 						free(bufferp);
4837c478bd9Sstevel@tonic-gate 					return (2);
4847c478bd9Sstevel@tonic-gate 				}
4857c478bd9Sstevel@tonic-gate 				offset += nwritten;
4867c478bd9Sstevel@tonic-gate 			} while ((nitems -= nwritten) > 0);
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		if (bufferp != (char *)buf)
4897c478bd9Sstevel@tonic-gate 			free(bufferp);
4907c478bd9Sstevel@tonic-gate 		if (nitems < 0) {
4917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4927c478bd9Sstevel@tonic-gate 			    gettext("cat: input error on %s: "), filenm);
4937c478bd9Sstevel@tonic-gate 			perror("");
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	return (0);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate static int
5017c478bd9Sstevel@tonic-gate vncat(fi)
5027c478bd9Sstevel@tonic-gate 	FILE *fi;
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	int c;
5057c478bd9Sstevel@tonic-gate 	int	lno;
5067c478bd9Sstevel@tonic-gate 	int	boln;	/* = 1 if at beginning of line */
5077c478bd9Sstevel@tonic-gate 			/* = 0 otherwise */
5087c478bd9Sstevel@tonic-gate 	wchar_t	wc;
5097c478bd9Sstevel@tonic-gate 	int	len, n;
5107c478bd9Sstevel@tonic-gate 	unsigned char	*p1, *p2;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	lno = 1;
5137c478bd9Sstevel@tonic-gate 	boln = 1;
5147c478bd9Sstevel@tonic-gate 	p1 = p2 = buf;
5157c478bd9Sstevel@tonic-gate 	for (;;) {
5167c478bd9Sstevel@tonic-gate 		if (p1 >= p2) {
5177c478bd9Sstevel@tonic-gate 			p1 = buf;
5187c478bd9Sstevel@tonic-gate 			if ((len = fread(p1, 1, BUFSIZ, fi)) <= 0)
5197c478bd9Sstevel@tonic-gate 				break;
5207c478bd9Sstevel@tonic-gate 			p2 = p1 + len;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		c = *p1++;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Display newlines as "$<newline>"
5267c478bd9Sstevel@tonic-gate 		 * if visi_newline set
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		if (c == '\n') {
5297c478bd9Sstevel@tonic-gate 			if (nflg && boln && !bflg)
5307c478bd9Sstevel@tonic-gate 				(void) printf("%6d\t", lno++);
5317c478bd9Sstevel@tonic-gate 			boln = 1;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 			if (visi_mode && visi_newline)
5347c478bd9Sstevel@tonic-gate 				(void) putchar('$');
5357c478bd9Sstevel@tonic-gate 			(void) putchar(c);
5367c478bd9Sstevel@tonic-gate 			continue;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		if (nflg && boln)
5407c478bd9Sstevel@tonic-gate 			(void) printf("%6d\t", lno++);
5417c478bd9Sstevel@tonic-gate 		boln = 0;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		/*
5447c478bd9Sstevel@tonic-gate 		 * For non-printable and non-cntrl chars,
5457c478bd9Sstevel@tonic-gate 		 * use the "M-x" notation.
5467c478bd9Sstevel@tonic-gate 		 */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		if (isascii(c)) {
5497c478bd9Sstevel@tonic-gate 			if (isprint(c) || visi_mode == 0) {
5507c478bd9Sstevel@tonic-gate 				(void) putchar(c);
5517c478bd9Sstevel@tonic-gate 				continue;
5527c478bd9Sstevel@tonic-gate 			}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			/*
5557c478bd9Sstevel@tonic-gate 			 * For non-printable ascii characters.
5567c478bd9Sstevel@tonic-gate 			 */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 			if (iscntrl(c)) {
5597c478bd9Sstevel@tonic-gate 				/* For cntrl characters. */
5607c478bd9Sstevel@tonic-gate 				if ((c == '\t') || (c == '\f')) {
5617c478bd9Sstevel@tonic-gate 					/*
5627c478bd9Sstevel@tonic-gate 					 * Display tab as "^I" if visi_tab set
5637c478bd9Sstevel@tonic-gate 					 */
5647c478bd9Sstevel@tonic-gate 					if (visi_mode && visi_tab) {
5657c478bd9Sstevel@tonic-gate 						(void) putchar('^');
5667c478bd9Sstevel@tonic-gate 						(void) putchar(c^0100);
5677c478bd9Sstevel@tonic-gate 					} else
5687c478bd9Sstevel@tonic-gate 						(void) putchar(c);
5697c478bd9Sstevel@tonic-gate 					continue;
5707c478bd9Sstevel@tonic-gate 				}
5717c478bd9Sstevel@tonic-gate 				(void) putchar('^');
5727c478bd9Sstevel@tonic-gate 				(void) putchar(c^0100);
5737c478bd9Sstevel@tonic-gate 				continue;
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			continue;
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		/*
5797c478bd9Sstevel@tonic-gate 		 * For non-ascii characters.
5807c478bd9Sstevel@tonic-gate 		 */
5817c478bd9Sstevel@tonic-gate 		p1--;
5827c478bd9Sstevel@tonic-gate 		if ((len = (p2 - p1)) < MB_LEN_MAX) {
5837c478bd9Sstevel@tonic-gate 			for (n = 0; n < len; n++)
5847c478bd9Sstevel@tonic-gate 				buf[n] = *p1++;
5857c478bd9Sstevel@tonic-gate 			p1 = buf;
5867c478bd9Sstevel@tonic-gate 			p2 = p1 + n;
5877c478bd9Sstevel@tonic-gate 			if ((len = fread(p2, 1, BUFSIZ - n, fi)) > 0)
5887c478bd9Sstevel@tonic-gate 				p2 += len;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if ((len = (p2 - p1)) > MB_LEN_MAX)
5927c478bd9Sstevel@tonic-gate 			len = MB_LEN_MAX;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)p1, len)) > 0) {
5957c478bd9Sstevel@tonic-gate 			if (iswprint(wc) || visi_mode == 0) {
5967c478bd9Sstevel@tonic-gate 				(void) putwchar(wc);
5977c478bd9Sstevel@tonic-gate 				p1 += len;
5987c478bd9Sstevel@tonic-gate 				continue;
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		(void) putchar('M');
6037c478bd9Sstevel@tonic-gate 		(void) putchar('-');
6047c478bd9Sstevel@tonic-gate 		c -= 0200;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		if (isprint(c)) {
6077c478bd9Sstevel@tonic-gate 			(void) putchar(c);
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/* For non-printable characters. */
6117c478bd9Sstevel@tonic-gate 		if (iscntrl(c)) {
6127c478bd9Sstevel@tonic-gate 			/* For cntrl characters. */
6137c478bd9Sstevel@tonic-gate 			if ((c == '\t') || (c == '\f')) {
6147c478bd9Sstevel@tonic-gate 				/*
6157c478bd9Sstevel@tonic-gate 				 * Display tab as "^I" if visi_tab set
6167c478bd9Sstevel@tonic-gate 				 */
6177c478bd9Sstevel@tonic-gate 				if (visi_mode && visi_tab) {
6187c478bd9Sstevel@tonic-gate 					(void) putchar('^');
6197c478bd9Sstevel@tonic-gate 					(void) putchar(c^0100);
6207c478bd9Sstevel@tonic-gate 				} else
6217c478bd9Sstevel@tonic-gate 					(void) putchar(c);
6227c478bd9Sstevel@tonic-gate 			} else {
6237c478bd9Sstevel@tonic-gate 				(void) putchar('^');
6247c478bd9Sstevel@tonic-gate 				(void) putchar(c^0100);
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		p1++;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	return (0);
6307c478bd9Sstevel@tonic-gate }
631