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