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