xref: /titanic_44/usr/src/cmd/cat/cat.c (revision c33f722576e9e15fe9dadf9d555414b41a14c476)
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 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	Concatenate files.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<stdlib.h>
377c478bd9Sstevel@tonic-gate #include	<ctype.h>
387c478bd9Sstevel@tonic-gate #include	<sys/types.h>
397c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
407c478bd9Sstevel@tonic-gate #include	<locale.h>
417c478bd9Sstevel@tonic-gate #include	<unistd.h>
427c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
435b095deaScasper #include	<errno.h>
445b095deaScasper #include	<string.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include	<widec.h>
477c478bd9Sstevel@tonic-gate #include	<wctype.h>
487c478bd9Sstevel@tonic-gate #include	<limits.h>
497c478bd9Sstevel@tonic-gate #include	<libintl.h>
507c478bd9Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	MAXMAPSIZE	(8*1024*1024)	/* map at most 8MB */
537c478bd9Sstevel@tonic-gate #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little files */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int vncat(FILE *);
567c478bd9Sstevel@tonic-gate static int cat(FILE *, struct stat *, struct stat *, char *);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static int	silent = 0;		/* s flag */
597c478bd9Sstevel@tonic-gate static int	visi_mode = 0;		/* v flag */
607c478bd9Sstevel@tonic-gate static int	visi_tab = 0;		/* t flag */
617c478bd9Sstevel@tonic-gate static int	visi_newline = 0;	/* e flag */
627c478bd9Sstevel@tonic-gate static int	bflg = 0;		/* b flag */
637c478bd9Sstevel@tonic-gate static int	nflg = 0;		/* n flag */
647c478bd9Sstevel@tonic-gate static long	ibsize;
657c478bd9Sstevel@tonic-gate static long	obsize;
667c478bd9Sstevel@tonic-gate static unsigned	char	buf[SMALLFILESIZE];
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)707c478bd9Sstevel@tonic-gate main(int argc, char **argv)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	FILE *fi;
737c478bd9Sstevel@tonic-gate 	int c;
747c478bd9Sstevel@tonic-gate 	extern	int optind;
757c478bd9Sstevel@tonic-gate 	int	errflg = 0;
767c478bd9Sstevel@tonic-gate 	int	stdinflg = 0;
777c478bd9Sstevel@tonic-gate 	int	status = 0;
787c478bd9Sstevel@tonic-gate 	int	estatus = 0;
797c478bd9Sstevel@tonic-gate 	struct stat source, target;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
827c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
837c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #ifdef STANDALONE
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * If the first argument is NULL,
907c478bd9Sstevel@tonic-gate 	 * discard arguments until we find cat.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (argv[0][0] == '\0')
937c478bd9Sstevel@tonic-gate 		argc = getargv("cat", &argv, 0);
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * Process the options for cat.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "usvtebn")) != EOF) {
1017c478bd9Sstevel@tonic-gate 		switch (c) {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		case 'u':
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 			/*
1067c478bd9Sstevel@tonic-gate 			 * If not standalone, set stdout to
1077c478bd9Sstevel@tonic-gate 			 * completely unbuffered I/O when
1087c478bd9Sstevel@tonic-gate 			 * the 'u' option is used.
1097c478bd9Sstevel@tonic-gate 			 */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifndef	STANDALONE
1127c478bd9Sstevel@tonic-gate 			setbuf(stdout, (char *)NULL);
1137c478bd9Sstevel@tonic-gate #endif
1147c478bd9Sstevel@tonic-gate 			continue;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		case 's':
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 			/*
1197c478bd9Sstevel@tonic-gate 			 * The 's' option requests silent mode
1207c478bd9Sstevel@tonic-gate 			 * where no messages are written.
1217c478bd9Sstevel@tonic-gate 			 */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 			silent++;
1247c478bd9Sstevel@tonic-gate 			continue;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 		case 'v':
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 			/*
1297c478bd9Sstevel@tonic-gate 			 * The 'v' option requests that non-printing
1307c478bd9Sstevel@tonic-gate 			 * characters (with the exception of newlines,
1317c478bd9Sstevel@tonic-gate 			 * form-feeds, and tabs) be displayed visibly.
1327c478bd9Sstevel@tonic-gate 			 *
1337c478bd9Sstevel@tonic-gate 			 * Control characters are printed as "^x".
1347c478bd9Sstevel@tonic-gate 			 * DEL characters are printed as "^?".
1357c478bd9Sstevel@tonic-gate 			 * Non-printable  and non-contrlol characters with the
1367c478bd9Sstevel@tonic-gate 			 * 8th bit set are printed as "M-x".
1377c478bd9Sstevel@tonic-gate 			 */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 			visi_mode++;
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		case 't':
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 			/*
1457c478bd9Sstevel@tonic-gate 			 * When in visi_mode, this option causes tabs
1467c478bd9Sstevel@tonic-gate 			 * to be displayed as "^I".
1477c478bd9Sstevel@tonic-gate 			 */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 			visi_tab++;
1507c478bd9Sstevel@tonic-gate 			continue;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		case 'e':
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 			/*
1557c478bd9Sstevel@tonic-gate 			 * When in visi_mode, this option causes newlines
1567c478bd9Sstevel@tonic-gate 			 * and form-feeds to be displayed as "$" at the end
1577c478bd9Sstevel@tonic-gate 			 * of the line prior to the newline.
1587c478bd9Sstevel@tonic-gate 			 */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 			visi_newline++;
1617c478bd9Sstevel@tonic-gate 			continue;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		case 'b':
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			/*
1667c478bd9Sstevel@tonic-gate 			 * Precede each line output with its line number,
1677c478bd9Sstevel@tonic-gate 			 * but omit the line numbers from blank lines.
1687c478bd9Sstevel@tonic-gate 			 */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 			bflg++;
1717c478bd9Sstevel@tonic-gate 			nflg++;
1727c478bd9Sstevel@tonic-gate 			continue;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		case 'n':
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 			/*
1777c478bd9Sstevel@tonic-gate 			 * Precede each line output with its line number.
1787c478bd9Sstevel@tonic-gate 			 */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			nflg++;
1817c478bd9Sstevel@tonic-gate 			continue;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		case '?':
1847c478bd9Sstevel@tonic-gate 			errflg++;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		break;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (errflg) {
1917c478bd9Sstevel@tonic-gate 		if (!silent)
1927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1937c478bd9Sstevel@tonic-gate 			    gettext("usage: cat [ -usvtebn ] [-|file] ...\n"));
1947c478bd9Sstevel@tonic-gate 		exit(2);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * Stat stdout to be sure it is defined.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (fstat(fileno(stdout), &target) < 0) {
2027c478bd9Sstevel@tonic-gate 		if (!silent)
2037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2047c478bd9Sstevel@tonic-gate 			    gettext("cat: Cannot stat stdout\n"));
2057c478bd9Sstevel@tonic-gate 		exit(2);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	obsize = target.st_blksize;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * If no arguments given, then use stdin for input.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (optind == argc) {
2147c478bd9Sstevel@tonic-gate 		argc++;
2157c478bd9Sstevel@tonic-gate 		stdinflg++;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Process each remaining argument,
2207c478bd9Sstevel@tonic-gate 	 * unless there is an error with stdout.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	for (argv = &argv[optind];
2257c478bd9Sstevel@tonic-gate 	    optind < argc && !ferror(stdout); optind++, argv++) {
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		/*
2287c478bd9Sstevel@tonic-gate 		 * If the argument was '-' or there were no files
2297c478bd9Sstevel@tonic-gate 		 * specified, take the input from stdin.
2307c478bd9Sstevel@tonic-gate 		 */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (stdinflg ||
2337c478bd9Sstevel@tonic-gate 		    ((*argv)[0] == '-' && (*argv)[1] == '\0'))
2347c478bd9Sstevel@tonic-gate 			fi = stdin;
2357c478bd9Sstevel@tonic-gate 		else {
2367c478bd9Sstevel@tonic-gate 			/*
2377c478bd9Sstevel@tonic-gate 			 * Attempt to open each specified file.
2387c478bd9Sstevel@tonic-gate 			 */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 			if ((fi = fopen(*argv, "r")) == NULL) {
2417c478bd9Sstevel@tonic-gate 				if (!silent)
242*c33f7225SJohn Sonnenschein 					(void) fprintf(stderr, gettext(
243*c33f7225SJohn Sonnenschein 					    "cat: cannot open %s: %s\n"),
2445b095deaScasper 					    *argv, strerror(errno));
2457c478bd9Sstevel@tonic-gate 				status = 2;
2467c478bd9Sstevel@tonic-gate 				continue;
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		/*
2517c478bd9Sstevel@tonic-gate 		 * Stat source to make sure it is defined.
2527c478bd9Sstevel@tonic-gate 		 */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		if (fstat(fileno(fi), &source) < 0) {
2557c478bd9Sstevel@tonic-gate 			if (!silent)
2567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2575b095deaScasper 				    gettext("cat: cannot stat %s: %s\n"),
2585b095deaScasper 				    (stdinflg) ? "-" : *argv, strerror(errno));
2597c478bd9Sstevel@tonic-gate 			status = 2;
2607c478bd9Sstevel@tonic-gate 			continue;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		/*
2657c478bd9Sstevel@tonic-gate 		 * If the source is not a character special file, socket or a
2667c478bd9Sstevel@tonic-gate 		 * block special file, make sure it is not identical
2677c478bd9Sstevel@tonic-gate 		 * to the target.
2687c478bd9Sstevel@tonic-gate 		 */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (!S_ISCHR(target.st_mode) &&
2717c478bd9Sstevel@tonic-gate 		    !S_ISBLK(target.st_mode) &&
2727c478bd9Sstevel@tonic-gate 		    !S_ISSOCK(target.st_mode) &&
2737c478bd9Sstevel@tonic-gate 		    IDENTICAL(target, source)) {
2747c478bd9Sstevel@tonic-gate 			if (!silent)
2757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2767c478bd9Sstevel@tonic-gate 			    gettext("cat: input/output files '%s' identical\n"),
2777c478bd9Sstevel@tonic-gate 			    stdinflg?"-": *argv);
2787c478bd9Sstevel@tonic-gate 			if (fclose(fi) != 0)
2797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2805b095deaScasper 				    gettext("cat: close error: %s\n"),
2815b095deaScasper 				    strerror(errno));
2827c478bd9Sstevel@tonic-gate 			status = 2;
2837c478bd9Sstevel@tonic-gate 			continue;
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 		ibsize = source.st_blksize;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/*
2887c478bd9Sstevel@tonic-gate 		 * If in visible mode and/or nflg, use vncat;
2897c478bd9Sstevel@tonic-gate 		 * otherwise, use cat.
2907c478bd9Sstevel@tonic-gate 		 */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		if (visi_mode || nflg)
2937c478bd9Sstevel@tonic-gate 			estatus = vncat(fi);
2947c478bd9Sstevel@tonic-gate 		else
2957c478bd9Sstevel@tonic-gate 			estatus = cat(fi, &source, &target,
2967c478bd9Sstevel@tonic-gate 			    fi != stdin ? *argv : "standard input");
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		if (estatus)
2997c478bd9Sstevel@tonic-gate 			status = estatus;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		/*
3027c478bd9Sstevel@tonic-gate 		 * If the input is not stdin, close the source file.
3037c478bd9Sstevel@tonic-gate 		 */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		if (fi != stdin) {
3067c478bd9Sstevel@tonic-gate 			if (fclose(fi) != 0)
3077c478bd9Sstevel@tonic-gate 				if (!silent)
3087c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
3095b095deaScasper 					    gettext("cat: close error: %s\n"),
3105b095deaScasper 					    strerror(errno));
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Display any error with stdout operations.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (fclose(stdout) != 0) {
3197c478bd9Sstevel@tonic-gate 		if (!silent)
3207c478bd9Sstevel@tonic-gate 			perror(gettext("cat: close error"));
3217c478bd9Sstevel@tonic-gate 		status = 2;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	return (status);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate static int
cat(FILE * fi,struct stat * statp,struct stat * outp,char * filenm)3297c478bd9Sstevel@tonic-gate cat(FILE *fi, struct stat *statp, struct stat *outp, char *filenm)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	int nitems;
3327c478bd9Sstevel@tonic-gate 	int nwritten;
3337c478bd9Sstevel@tonic-gate 	int offset;
3347c478bd9Sstevel@tonic-gate 	int fi_desc;
3357c478bd9Sstevel@tonic-gate 	long buffsize;
3367c478bd9Sstevel@tonic-gate 	char *bufferp;
3377c478bd9Sstevel@tonic-gate 	off_t mapsize, munmapsize;
3387c478bd9Sstevel@tonic-gate 	off_t filesize;
3397c478bd9Sstevel@tonic-gate 	off_t mapoffset;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	fi_desc = fileno(fi);
3427c478bd9Sstevel@tonic-gate 	if (S_ISREG(statp->st_mode) && (lseek(fi_desc, (off_t)0, SEEK_CUR)
3437c478bd9Sstevel@tonic-gate 	    == 0) && (statp->st_size > SMALLFILESIZE)) {
3447c478bd9Sstevel@tonic-gate 		mapsize = (off_t)MAXMAPSIZE;
3457c478bd9Sstevel@tonic-gate 		if (statp->st_size < mapsize)
3467c478bd9Sstevel@tonic-gate 			mapsize = statp->st_size;
3477c478bd9Sstevel@tonic-gate 		munmapsize = mapsize;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * Mmap time!
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		bufferp = mmap((caddr_t)NULL, (size_t)mapsize, PROT_READ,
3537c478bd9Sstevel@tonic-gate 		    MAP_SHARED, fi_desc, (off_t)0);
3547c478bd9Sstevel@tonic-gate 		if (bufferp == (caddr_t)-1)
3557c478bd9Sstevel@tonic-gate 			mapsize = 0;	/* I guess we can't mmap today */
3567c478bd9Sstevel@tonic-gate 	} else
3577c478bd9Sstevel@tonic-gate 		mapsize = 0;		/* can't mmap non-regular files */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (mapsize != 0) {
3607c478bd9Sstevel@tonic-gate 		int	read_error = 0;
3617c478bd9Sstevel@tonic-gate 		char	x;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		/*
3647c478bd9Sstevel@tonic-gate 		 * NFS V2 will let root open a file it does not have permission
3657c478bd9Sstevel@tonic-gate 		 * to read. This read() is here to make sure that the access
3667c478bd9Sstevel@tonic-gate 		 * time on the input file will be updated. The VSC tests for
3677c478bd9Sstevel@tonic-gate 		 * cat do this:
3687c478bd9Sstevel@tonic-gate 		 *	cat file > /dev/null
3697c478bd9Sstevel@tonic-gate 		 * In this case the write()/mmap() pair will not read the file
3707c478bd9Sstevel@tonic-gate 		 * and the access time will not be updated.
3717c478bd9Sstevel@tonic-gate 		 */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		if (read(fi_desc, &x, 1) == -1)
3747c478bd9Sstevel@tonic-gate 			read_error = 1;
3757c478bd9Sstevel@tonic-gate 		mapoffset = 0;
3767c478bd9Sstevel@tonic-gate 		filesize = statp->st_size;
3777c478bd9Sstevel@tonic-gate 		for (;;) {
3787c478bd9Sstevel@tonic-gate 			/*
3797c478bd9Sstevel@tonic-gate 			 * Note that on some systems (V7), very large writes to
3807c478bd9Sstevel@tonic-gate 			 * a pipe return less than the requested size of the
3817c478bd9Sstevel@tonic-gate 			 * write.  In this case, multiple writes are required.
3827c478bd9Sstevel@tonic-gate 			 */
3837c478bd9Sstevel@tonic-gate 			offset = 0;
3847c478bd9Sstevel@tonic-gate 			nitems = (int)mapsize;
3857c478bd9Sstevel@tonic-gate 			do {
3867c478bd9Sstevel@tonic-gate 				if ((nwritten = write(fileno(stdout),
3877c478bd9Sstevel@tonic-gate 				    &bufferp[offset], (size_t)nitems)) < 0) {
3887c478bd9Sstevel@tonic-gate 					if (!silent) {
3897c478bd9Sstevel@tonic-gate 						if (read_error == 1)
3907c478bd9Sstevel@tonic-gate 							(void) fprintf(
3917c478bd9Sstevel@tonic-gate 							    stderr, gettext(
3927c478bd9Sstevel@tonic-gate 							    "cat: cannot read "
3937c478bd9Sstevel@tonic-gate 							    "%s: "), filenm);
3947c478bd9Sstevel@tonic-gate 						else
395*c33f7225SJohn Sonnenschein 							(void) fprintf(stderr,
396*c33f7225SJohn Sonnenschein 							    gettext(
397*c33f7225SJohn Sonnenschein 							    "cat: write "
398*c33f7225SJohn Sonnenschein 							    "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("");
4941514c4daSJohn Sonnenschein 			return (1);
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	return (0);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static int
vncat(fi)5027c478bd9Sstevel@tonic-gate vncat(fi)
5037c478bd9Sstevel@tonic-gate 	FILE *fi;
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	int c;
5067c478bd9Sstevel@tonic-gate 	int	lno;
5077c478bd9Sstevel@tonic-gate 	int	boln;	/* = 1 if at beginning of line */
5087c478bd9Sstevel@tonic-gate 			/* = 0 otherwise */
5097c478bd9Sstevel@tonic-gate 	wchar_t	wc;
5107c478bd9Sstevel@tonic-gate 	int	len, n;
5117c478bd9Sstevel@tonic-gate 	unsigned char	*p1, *p2;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	lno = 1;
5147c478bd9Sstevel@tonic-gate 	boln = 1;
5157c478bd9Sstevel@tonic-gate 	p1 = p2 = buf;
5167c478bd9Sstevel@tonic-gate 	for (;;) {
5177c478bd9Sstevel@tonic-gate 		if (p1 >= p2) {
5187c478bd9Sstevel@tonic-gate 			p1 = buf;
5197c478bd9Sstevel@tonic-gate 			if ((len = fread(p1, 1, BUFSIZ, fi)) <= 0)
5207c478bd9Sstevel@tonic-gate 				break;
5217c478bd9Sstevel@tonic-gate 			p2 = p1 + len;
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 		c = *p1++;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * Display newlines as "$<newline>"
5277c478bd9Sstevel@tonic-gate 		 * if visi_newline set
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		if (c == '\n') {
5307c478bd9Sstevel@tonic-gate 			if (nflg && boln && !bflg)
5317c478bd9Sstevel@tonic-gate 				(void) printf("%6d\t", lno++);
5327c478bd9Sstevel@tonic-gate 			boln = 1;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 			if (visi_mode && visi_newline)
5357c478bd9Sstevel@tonic-gate 				(void) putchar('$');
5367c478bd9Sstevel@tonic-gate 			(void) putchar(c);
5377c478bd9Sstevel@tonic-gate 			continue;
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		if (nflg && boln)
5417c478bd9Sstevel@tonic-gate 			(void) printf("%6d\t", lno++);
5427c478bd9Sstevel@tonic-gate 		boln = 0;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		/*
5457c478bd9Sstevel@tonic-gate 		 * For non-printable and non-cntrl chars,
5467c478bd9Sstevel@tonic-gate 		 * use the "M-x" notation.
5477c478bd9Sstevel@tonic-gate 		 */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		if (isascii(c)) {
5507c478bd9Sstevel@tonic-gate 			if (isprint(c) || visi_mode == 0) {
5517c478bd9Sstevel@tonic-gate 				(void) putchar(c);
5527c478bd9Sstevel@tonic-gate 				continue;
5537c478bd9Sstevel@tonic-gate 			}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 			/*
5567c478bd9Sstevel@tonic-gate 			 * For non-printable ascii characters.
5577c478bd9Sstevel@tonic-gate 			 */
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 			if (iscntrl(c)) {
5607c478bd9Sstevel@tonic-gate 				/* For cntrl characters. */
5617c478bd9Sstevel@tonic-gate 				if ((c == '\t') || (c == '\f')) {
5627c478bd9Sstevel@tonic-gate 					/*
5637c478bd9Sstevel@tonic-gate 					 * Display tab as "^I" if visi_tab set
5647c478bd9Sstevel@tonic-gate 					 */
5657c478bd9Sstevel@tonic-gate 					if (visi_mode && visi_tab) {
5667c478bd9Sstevel@tonic-gate 						(void) putchar('^');
5677c478bd9Sstevel@tonic-gate 						(void) putchar(c^0100);
5687c478bd9Sstevel@tonic-gate 					} else
5697c478bd9Sstevel@tonic-gate 						(void) putchar(c);
5707c478bd9Sstevel@tonic-gate 					continue;
5717c478bd9Sstevel@tonic-gate 				}
5727c478bd9Sstevel@tonic-gate 				(void) putchar('^');
5737c478bd9Sstevel@tonic-gate 				(void) putchar(c^0100);
5747c478bd9Sstevel@tonic-gate 				continue;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 			continue;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		/*
5807c478bd9Sstevel@tonic-gate 		 * For non-ascii characters.
5817c478bd9Sstevel@tonic-gate 		 */
5827c478bd9Sstevel@tonic-gate 		p1--;
5837c478bd9Sstevel@tonic-gate 		if ((len = (p2 - p1)) < MB_LEN_MAX) {
5847c478bd9Sstevel@tonic-gate 			for (n = 0; n < len; n++)
5857c478bd9Sstevel@tonic-gate 				buf[n] = *p1++;
5867c478bd9Sstevel@tonic-gate 			p1 = buf;
5877c478bd9Sstevel@tonic-gate 			p2 = p1 + n;
5887c478bd9Sstevel@tonic-gate 			if ((len = fread(p2, 1, BUFSIZ - n, fi)) > 0)
5897c478bd9Sstevel@tonic-gate 				p2 += len;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		if ((len = (p2 - p1)) > MB_LEN_MAX)
5937c478bd9Sstevel@tonic-gate 			len = MB_LEN_MAX;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)p1, len)) > 0) {
5967c478bd9Sstevel@tonic-gate 			if (iswprint(wc) || visi_mode == 0) {
5977c478bd9Sstevel@tonic-gate 				(void) putwchar(wc);
5987c478bd9Sstevel@tonic-gate 				p1 += len;
5997c478bd9Sstevel@tonic-gate 				continue;
6007c478bd9Sstevel@tonic-gate 			}
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		(void) putchar('M');
6047c478bd9Sstevel@tonic-gate 		(void) putchar('-');
6057c478bd9Sstevel@tonic-gate 		c -= 0200;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		if (isprint(c)) {
6087c478bd9Sstevel@tonic-gate 			(void) putchar(c);
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/* For non-printable characters. */
6127c478bd9Sstevel@tonic-gate 		if (iscntrl(c)) {
6137c478bd9Sstevel@tonic-gate 			/* For cntrl characters. */
6147c478bd9Sstevel@tonic-gate 			if ((c == '\t') || (c == '\f')) {
6157c478bd9Sstevel@tonic-gate 				/*
6167c478bd9Sstevel@tonic-gate 				 * Display tab as "^I" if visi_tab set
6177c478bd9Sstevel@tonic-gate 				 */
6187c478bd9Sstevel@tonic-gate 				if (visi_mode && visi_tab) {
6197c478bd9Sstevel@tonic-gate 					(void) putchar('^');
6207c478bd9Sstevel@tonic-gate 					(void) putchar(c^0100);
6217c478bd9Sstevel@tonic-gate 				} else
6227c478bd9Sstevel@tonic-gate 					(void) putchar(c);
6237c478bd9Sstevel@tonic-gate 			} else {
6247c478bd9Sstevel@tonic-gate 				(void) putchar('^');
6257c478bd9Sstevel@tonic-gate 				(void) putchar(c^0100);
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		p1++;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	return (0);
6317c478bd9Sstevel@tonic-gate }
632