1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * Concatenate files. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <ctype.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 42*7c478bd9Sstevel@tonic-gate #include <locale.h> 43*7c478bd9Sstevel@tonic-gate #include <unistd.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #include <widec.h> 47*7c478bd9Sstevel@tonic-gate #include <wctype.h> 48*7c478bd9Sstevel@tonic-gate #include <limits.h> 49*7c478bd9Sstevel@tonic-gate #include <libintl.h> 50*7c478bd9Sstevel@tonic-gate #define IDENTICAL(A, B) (A.st_dev == B.st_dev && A.st_ino == B.st_ino) 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate #define MAXMAPSIZE (8*1024*1024) /* map at most 8MB */ 53*7c478bd9Sstevel@tonic-gate #define SMALLFILESIZE (32*1024) /* don't use mmap on little files */ 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate static int vncat(FILE *); 56*7c478bd9Sstevel@tonic-gate static int cat(FILE *, struct stat *, struct stat *, char *); 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate static int silent = 0; /* s flag */ 59*7c478bd9Sstevel@tonic-gate static int visi_mode = 0; /* v flag */ 60*7c478bd9Sstevel@tonic-gate static int visi_tab = 0; /* t flag */ 61*7c478bd9Sstevel@tonic-gate static int visi_newline = 0; /* e flag */ 62*7c478bd9Sstevel@tonic-gate static int bflg = 0; /* b flag */ 63*7c478bd9Sstevel@tonic-gate static int nflg = 0; /* n flag */ 64*7c478bd9Sstevel@tonic-gate static long ibsize; 65*7c478bd9Sstevel@tonic-gate static long obsize; 66*7c478bd9Sstevel@tonic-gate static unsigned char buf[SMALLFILESIZE]; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate int 70*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 71*7c478bd9Sstevel@tonic-gate { 72*7c478bd9Sstevel@tonic-gate FILE *fi; 73*7c478bd9Sstevel@tonic-gate int c; 74*7c478bd9Sstevel@tonic-gate extern int optind; 75*7c478bd9Sstevel@tonic-gate int errflg = 0; 76*7c478bd9Sstevel@tonic-gate int stdinflg = 0; 77*7c478bd9Sstevel@tonic-gate int status = 0; 78*7c478bd9Sstevel@tonic-gate int estatus = 0; 79*7c478bd9Sstevel@tonic-gate struct stat source, target; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 82*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 83*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 84*7c478bd9Sstevel@tonic-gate #endif 85*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate #ifdef STANDALONE 88*7c478bd9Sstevel@tonic-gate /* 89*7c478bd9Sstevel@tonic-gate * If the first argument is NULL, 90*7c478bd9Sstevel@tonic-gate * discard arguments until we find cat. 91*7c478bd9Sstevel@tonic-gate */ 92*7c478bd9Sstevel@tonic-gate if (argv[0][0] == '\0') 93*7c478bd9Sstevel@tonic-gate argc = getargv("cat", &argv, 0); 94*7c478bd9Sstevel@tonic-gate #endif 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate /* 97*7c478bd9Sstevel@tonic-gate * Process the options for cat. 98*7c478bd9Sstevel@tonic-gate */ 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "usvtebn")) != EOF) { 101*7c478bd9Sstevel@tonic-gate switch (c) { 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate case 'u': 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* 106*7c478bd9Sstevel@tonic-gate * If not standalone, set stdout to 107*7c478bd9Sstevel@tonic-gate * completely unbuffered I/O when 108*7c478bd9Sstevel@tonic-gate * the 'u' option is used. 109*7c478bd9Sstevel@tonic-gate */ 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate #ifndef STANDALONE 112*7c478bd9Sstevel@tonic-gate setbuf(stdout, (char *)NULL); 113*7c478bd9Sstevel@tonic-gate #endif 114*7c478bd9Sstevel@tonic-gate continue; 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate case 's': 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate /* 119*7c478bd9Sstevel@tonic-gate * The 's' option requests silent mode 120*7c478bd9Sstevel@tonic-gate * where no messages are written. 121*7c478bd9Sstevel@tonic-gate */ 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate silent++; 124*7c478bd9Sstevel@tonic-gate continue; 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate case 'v': 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate /* 129*7c478bd9Sstevel@tonic-gate * The 'v' option requests that non-printing 130*7c478bd9Sstevel@tonic-gate * characters (with the exception of newlines, 131*7c478bd9Sstevel@tonic-gate * form-feeds, and tabs) be displayed visibly. 132*7c478bd9Sstevel@tonic-gate * 133*7c478bd9Sstevel@tonic-gate * Control characters are printed as "^x". 134*7c478bd9Sstevel@tonic-gate * DEL characters are printed as "^?". 135*7c478bd9Sstevel@tonic-gate * Non-printable and non-contrlol characters with the 136*7c478bd9Sstevel@tonic-gate * 8th bit set are printed as "M-x". 137*7c478bd9Sstevel@tonic-gate */ 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate visi_mode++; 140*7c478bd9Sstevel@tonic-gate continue; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate case 't': 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate /* 145*7c478bd9Sstevel@tonic-gate * When in visi_mode, this option causes tabs 146*7c478bd9Sstevel@tonic-gate * to be displayed as "^I". 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate visi_tab++; 150*7c478bd9Sstevel@tonic-gate continue; 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate case 'e': 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate /* 155*7c478bd9Sstevel@tonic-gate * When in visi_mode, this option causes newlines 156*7c478bd9Sstevel@tonic-gate * and form-feeds to be displayed as "$" at the end 157*7c478bd9Sstevel@tonic-gate * of the line prior to the newline. 158*7c478bd9Sstevel@tonic-gate */ 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate visi_newline++; 161*7c478bd9Sstevel@tonic-gate continue; 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate case 'b': 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate /* 166*7c478bd9Sstevel@tonic-gate * Precede each line output with its line number, 167*7c478bd9Sstevel@tonic-gate * but omit the line numbers from blank lines. 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate bflg++; 171*7c478bd9Sstevel@tonic-gate nflg++; 172*7c478bd9Sstevel@tonic-gate continue; 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate case 'n': 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate /* 177*7c478bd9Sstevel@tonic-gate * Precede each line output with its line number. 178*7c478bd9Sstevel@tonic-gate */ 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate nflg++; 181*7c478bd9Sstevel@tonic-gate continue; 182*7c478bd9Sstevel@tonic-gate 183*7c478bd9Sstevel@tonic-gate case '?': 184*7c478bd9Sstevel@tonic-gate errflg++; 185*7c478bd9Sstevel@tonic-gate break; 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate break; 188*7c478bd9Sstevel@tonic-gate } 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if (errflg) { 191*7c478bd9Sstevel@tonic-gate if (!silent) 192*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 193*7c478bd9Sstevel@tonic-gate gettext("usage: cat [ -usvtebn ] [-|file] ...\n")); 194*7c478bd9Sstevel@tonic-gate exit(2); 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate /* 198*7c478bd9Sstevel@tonic-gate * Stat stdout to be sure it is defined. 199*7c478bd9Sstevel@tonic-gate */ 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate if (fstat(fileno(stdout), &target) < 0) { 202*7c478bd9Sstevel@tonic-gate if (!silent) 203*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 204*7c478bd9Sstevel@tonic-gate gettext("cat: Cannot stat stdout\n")); 205*7c478bd9Sstevel@tonic-gate exit(2); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate obsize = target.st_blksize; 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate /* 210*7c478bd9Sstevel@tonic-gate * If no arguments given, then use stdin for input. 211*7c478bd9Sstevel@tonic-gate */ 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate if (optind == argc) { 214*7c478bd9Sstevel@tonic-gate argc++; 215*7c478bd9Sstevel@tonic-gate stdinflg++; 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate /* 219*7c478bd9Sstevel@tonic-gate * Process each remaining argument, 220*7c478bd9Sstevel@tonic-gate * unless there is an error with stdout. 221*7c478bd9Sstevel@tonic-gate */ 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate for (argv = &argv[optind]; 225*7c478bd9Sstevel@tonic-gate optind < argc && !ferror(stdout); optind++, argv++) { 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate /* 228*7c478bd9Sstevel@tonic-gate * If the argument was '-' or there were no files 229*7c478bd9Sstevel@tonic-gate * specified, take the input from stdin. 230*7c478bd9Sstevel@tonic-gate */ 231*7c478bd9Sstevel@tonic-gate 232*7c478bd9Sstevel@tonic-gate if (stdinflg || 233*7c478bd9Sstevel@tonic-gate ((*argv)[0] == '-' && (*argv)[1] == '\0')) 234*7c478bd9Sstevel@tonic-gate fi = stdin; 235*7c478bd9Sstevel@tonic-gate else { 236*7c478bd9Sstevel@tonic-gate /* 237*7c478bd9Sstevel@tonic-gate * Attempt to open each specified file. 238*7c478bd9Sstevel@tonic-gate */ 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate if ((fi = fopen(*argv, "r")) == NULL) { 241*7c478bd9Sstevel@tonic-gate if (!silent) 242*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 243*7c478bd9Sstevel@tonic-gate gettext("cat: cannot open %s\n"), *argv); 244*7c478bd9Sstevel@tonic-gate status = 2; 245*7c478bd9Sstevel@tonic-gate continue; 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate /* 250*7c478bd9Sstevel@tonic-gate * Stat source to make sure it is defined. 251*7c478bd9Sstevel@tonic-gate */ 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate if (fstat(fileno(fi), &source) < 0) { 254*7c478bd9Sstevel@tonic-gate if (!silent) 255*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 256*7c478bd9Sstevel@tonic-gate gettext("cat: cannot stat %s\n"), 257*7c478bd9Sstevel@tonic-gate (stdinflg) ? "-" : *argv); 258*7c478bd9Sstevel@tonic-gate status = 2; 259*7c478bd9Sstevel@tonic-gate continue; 260*7c478bd9Sstevel@tonic-gate } 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate 263*7c478bd9Sstevel@tonic-gate /* 264*7c478bd9Sstevel@tonic-gate * If the source is not a character special file, socket or a 265*7c478bd9Sstevel@tonic-gate * block special file, make sure it is not identical 266*7c478bd9Sstevel@tonic-gate * to the target. 267*7c478bd9Sstevel@tonic-gate */ 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate if (!S_ISCHR(target.st_mode) && 270*7c478bd9Sstevel@tonic-gate !S_ISBLK(target.st_mode) && 271*7c478bd9Sstevel@tonic-gate !S_ISSOCK(target.st_mode) && 272*7c478bd9Sstevel@tonic-gate IDENTICAL(target, source)) { 273*7c478bd9Sstevel@tonic-gate if (!silent) 274*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 275*7c478bd9Sstevel@tonic-gate gettext("cat: input/output files '%s' identical\n"), 276*7c478bd9Sstevel@tonic-gate stdinflg?"-": *argv); 277*7c478bd9Sstevel@tonic-gate if (fclose(fi) != 0) 278*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 279*7c478bd9Sstevel@tonic-gate gettext("cat: close error\n")); 280*7c478bd9Sstevel@tonic-gate status = 2; 281*7c478bd9Sstevel@tonic-gate continue; 282*7c478bd9Sstevel@tonic-gate } 283*7c478bd9Sstevel@tonic-gate ibsize = source.st_blksize; 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate /* 286*7c478bd9Sstevel@tonic-gate * If in visible mode and/or nflg, use vncat; 287*7c478bd9Sstevel@tonic-gate * otherwise, use cat. 288*7c478bd9Sstevel@tonic-gate */ 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate if (visi_mode || nflg) 291*7c478bd9Sstevel@tonic-gate estatus = vncat(fi); 292*7c478bd9Sstevel@tonic-gate else 293*7c478bd9Sstevel@tonic-gate estatus = cat(fi, &source, &target, 294*7c478bd9Sstevel@tonic-gate fi != stdin ? *argv : "standard input"); 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate if (estatus) 297*7c478bd9Sstevel@tonic-gate status = estatus; 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate /* 300*7c478bd9Sstevel@tonic-gate * If the input is not stdin, close the source file. 301*7c478bd9Sstevel@tonic-gate */ 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate if (fi != stdin) { 304*7c478bd9Sstevel@tonic-gate if (fclose(fi) != 0) 305*7c478bd9Sstevel@tonic-gate if (!silent) 306*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 307*7c478bd9Sstevel@tonic-gate gettext("cat: close error\n")); 308*7c478bd9Sstevel@tonic-gate } 309*7c478bd9Sstevel@tonic-gate } 310*7c478bd9Sstevel@tonic-gate 311*7c478bd9Sstevel@tonic-gate /* 312*7c478bd9Sstevel@tonic-gate * Display any error with stdout operations. 313*7c478bd9Sstevel@tonic-gate */ 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate if (fclose(stdout) != 0) { 316*7c478bd9Sstevel@tonic-gate if (!silent) 317*7c478bd9Sstevel@tonic-gate perror(gettext("cat: close error")); 318*7c478bd9Sstevel@tonic-gate status = 2; 319*7c478bd9Sstevel@tonic-gate } 320*7c478bd9Sstevel@tonic-gate return (status); 321*7c478bd9Sstevel@tonic-gate } 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate static int 326*7c478bd9Sstevel@tonic-gate cat(FILE *fi, struct stat *statp, struct stat *outp, char *filenm) 327*7c478bd9Sstevel@tonic-gate { 328*7c478bd9Sstevel@tonic-gate int nitems; 329*7c478bd9Sstevel@tonic-gate int nwritten; 330*7c478bd9Sstevel@tonic-gate int offset; 331*7c478bd9Sstevel@tonic-gate int fi_desc; 332*7c478bd9Sstevel@tonic-gate long buffsize; 333*7c478bd9Sstevel@tonic-gate char *bufferp; 334*7c478bd9Sstevel@tonic-gate off_t mapsize, munmapsize; 335*7c478bd9Sstevel@tonic-gate off_t filesize; 336*7c478bd9Sstevel@tonic-gate off_t mapoffset; 337*7c478bd9Sstevel@tonic-gate 338*7c478bd9Sstevel@tonic-gate fi_desc = fileno(fi); 339*7c478bd9Sstevel@tonic-gate if (S_ISREG(statp->st_mode) && (lseek(fi_desc, (off_t)0, SEEK_CUR) 340*7c478bd9Sstevel@tonic-gate == 0) && (statp->st_size > SMALLFILESIZE)) { 341*7c478bd9Sstevel@tonic-gate mapsize = (off_t)MAXMAPSIZE; 342*7c478bd9Sstevel@tonic-gate if (statp->st_size < mapsize) 343*7c478bd9Sstevel@tonic-gate mapsize = statp->st_size; 344*7c478bd9Sstevel@tonic-gate munmapsize = mapsize; 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate /* 347*7c478bd9Sstevel@tonic-gate * Mmap time! 348*7c478bd9Sstevel@tonic-gate */ 349*7c478bd9Sstevel@tonic-gate bufferp = mmap((caddr_t)NULL, (size_t)mapsize, PROT_READ, 350*7c478bd9Sstevel@tonic-gate MAP_SHARED, fi_desc, (off_t)0); 351*7c478bd9Sstevel@tonic-gate if (bufferp == (caddr_t)-1) 352*7c478bd9Sstevel@tonic-gate mapsize = 0; /* I guess we can't mmap today */ 353*7c478bd9Sstevel@tonic-gate } else 354*7c478bd9Sstevel@tonic-gate mapsize = 0; /* can't mmap non-regular files */ 355*7c478bd9Sstevel@tonic-gate 356*7c478bd9Sstevel@tonic-gate if (mapsize != 0) { 357*7c478bd9Sstevel@tonic-gate int read_error = 0; 358*7c478bd9Sstevel@tonic-gate char x; 359*7c478bd9Sstevel@tonic-gate 360*7c478bd9Sstevel@tonic-gate /* 361*7c478bd9Sstevel@tonic-gate * NFS V2 will let root open a file it does not have permission 362*7c478bd9Sstevel@tonic-gate * to read. This read() is here to make sure that the access 363*7c478bd9Sstevel@tonic-gate * time on the input file will be updated. The VSC tests for 364*7c478bd9Sstevel@tonic-gate * cat do this: 365*7c478bd9Sstevel@tonic-gate * cat file > /dev/null 366*7c478bd9Sstevel@tonic-gate * In this case the write()/mmap() pair will not read the file 367*7c478bd9Sstevel@tonic-gate * and the access time will not be updated. 368*7c478bd9Sstevel@tonic-gate */ 369*7c478bd9Sstevel@tonic-gate 370*7c478bd9Sstevel@tonic-gate if (read(fi_desc, &x, 1) == -1) 371*7c478bd9Sstevel@tonic-gate read_error = 1; 372*7c478bd9Sstevel@tonic-gate mapoffset = 0; 373*7c478bd9Sstevel@tonic-gate filesize = statp->st_size; 374*7c478bd9Sstevel@tonic-gate for (;;) { 375*7c478bd9Sstevel@tonic-gate /* 376*7c478bd9Sstevel@tonic-gate * Note that on some systems (V7), very large writes to 377*7c478bd9Sstevel@tonic-gate * a pipe return less than the requested size of the 378*7c478bd9Sstevel@tonic-gate * write. In this case, multiple writes are required. 379*7c478bd9Sstevel@tonic-gate */ 380*7c478bd9Sstevel@tonic-gate offset = 0; 381*7c478bd9Sstevel@tonic-gate nitems = (int)mapsize; 382*7c478bd9Sstevel@tonic-gate do { 383*7c478bd9Sstevel@tonic-gate if ((nwritten = write(fileno(stdout), 384*7c478bd9Sstevel@tonic-gate &bufferp[offset], (size_t)nitems)) < 0) { 385*7c478bd9Sstevel@tonic-gate if (!silent) { 386*7c478bd9Sstevel@tonic-gate if (read_error == 1) 387*7c478bd9Sstevel@tonic-gate (void) fprintf( 388*7c478bd9Sstevel@tonic-gate stderr, gettext( 389*7c478bd9Sstevel@tonic-gate "cat: cannot read " 390*7c478bd9Sstevel@tonic-gate "%s: "), filenm); 391*7c478bd9Sstevel@tonic-gate else 392*7c478bd9Sstevel@tonic-gate (void) fprintf( 393*7c478bd9Sstevel@tonic-gate stderr, gettext( 394*7c478bd9Sstevel@tonic-gate "cat: write error: ")); 395*7c478bd9Sstevel@tonic-gate perror(""); 396*7c478bd9Sstevel@tonic-gate } 397*7c478bd9Sstevel@tonic-gate (void) munmap(bufferp, 398*7c478bd9Sstevel@tonic-gate (size_t)munmapsize); 399*7c478bd9Sstevel@tonic-gate (void) lseek(fi_desc, (off_t)mapoffset, 400*7c478bd9Sstevel@tonic-gate SEEK_SET); 401*7c478bd9Sstevel@tonic-gate return (2); 402*7c478bd9Sstevel@tonic-gate } 403*7c478bd9Sstevel@tonic-gate offset += nwritten; 404*7c478bd9Sstevel@tonic-gate } while ((nitems -= nwritten) > 0); 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate filesize -= mapsize; 407*7c478bd9Sstevel@tonic-gate mapoffset += mapsize; 408*7c478bd9Sstevel@tonic-gate if (filesize == 0) 409*7c478bd9Sstevel@tonic-gate break; 410*7c478bd9Sstevel@tonic-gate if (filesize < mapsize) 411*7c478bd9Sstevel@tonic-gate mapsize = filesize; 412*7c478bd9Sstevel@tonic-gate if (mmap(bufferp, (size_t)mapsize, PROT_READ, 413*7c478bd9Sstevel@tonic-gate MAP_SHARED|MAP_FIXED, fi_desc, 414*7c478bd9Sstevel@tonic-gate mapoffset) == (caddr_t)-1) { 415*7c478bd9Sstevel@tonic-gate if (!silent) 416*7c478bd9Sstevel@tonic-gate perror(gettext("cat: mmap error")); 417*7c478bd9Sstevel@tonic-gate (void) munmap(bufferp, (size_t)munmapsize); 418*7c478bd9Sstevel@tonic-gate (void) lseek(fi_desc, (off_t)mapoffset, 419*7c478bd9Sstevel@tonic-gate SEEK_SET); 420*7c478bd9Sstevel@tonic-gate return (1); 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate } 423*7c478bd9Sstevel@tonic-gate /* 424*7c478bd9Sstevel@tonic-gate * Move the file pointer past what we read. Shell scripts 425*7c478bd9Sstevel@tonic-gate * rely on cat to do this, so that successive commands in 426*7c478bd9Sstevel@tonic-gate * the script won't re-read the same data. 427*7c478bd9Sstevel@tonic-gate */ 428*7c478bd9Sstevel@tonic-gate (void) lseek(fi_desc, (off_t)mapoffset, SEEK_SET); 429*7c478bd9Sstevel@tonic-gate (void) munmap(bufferp, (size_t)munmapsize); 430*7c478bd9Sstevel@tonic-gate } else { 431*7c478bd9Sstevel@tonic-gate if (((statp->st_mode & S_IFREG) == S_IFREG) && 432*7c478bd9Sstevel@tonic-gate ((outp->st_mode & S_IFREG) == S_IFREG)) { 433*7c478bd9Sstevel@tonic-gate bufferp = (char *)buf; 434*7c478bd9Sstevel@tonic-gate buffsize = SMALLFILESIZE; 435*7c478bd9Sstevel@tonic-gate } else { 436*7c478bd9Sstevel@tonic-gate if (obsize) 437*7c478bd9Sstevel@tonic-gate /* 438*7c478bd9Sstevel@tonic-gate * common case, use output blksize 439*7c478bd9Sstevel@tonic-gate */ 440*7c478bd9Sstevel@tonic-gate buffsize = obsize; 441*7c478bd9Sstevel@tonic-gate else if (ibsize) 442*7c478bd9Sstevel@tonic-gate buffsize = ibsize; 443*7c478bd9Sstevel@tonic-gate else 444*7c478bd9Sstevel@tonic-gate buffsize = (long)BUFSIZ; 445*7c478bd9Sstevel@tonic-gate 446*7c478bd9Sstevel@tonic-gate if (buffsize <= SMALLFILESIZE) { 447*7c478bd9Sstevel@tonic-gate bufferp = (char *)buf; 448*7c478bd9Sstevel@tonic-gate } else if ((bufferp = 449*7c478bd9Sstevel@tonic-gate malloc((size_t)buffsize)) == NULL) { 450*7c478bd9Sstevel@tonic-gate perror(gettext("cat: no memory")); 451*7c478bd9Sstevel@tonic-gate return (1); 452*7c478bd9Sstevel@tonic-gate } 453*7c478bd9Sstevel@tonic-gate } 454*7c478bd9Sstevel@tonic-gate 455*7c478bd9Sstevel@tonic-gate /* 456*7c478bd9Sstevel@tonic-gate * While not end of file, copy blocks to stdout. 457*7c478bd9Sstevel@tonic-gate */ 458*7c478bd9Sstevel@tonic-gate while ((nitems = read(fi_desc, bufferp, (size_t)buffsize)) > 459*7c478bd9Sstevel@tonic-gate 0) { 460*7c478bd9Sstevel@tonic-gate offset = 0; 461*7c478bd9Sstevel@tonic-gate /* 462*7c478bd9Sstevel@tonic-gate * Note that on some systems (V7), very large writes 463*7c478bd9Sstevel@tonic-gate * to a pipe return less than the requested size of 464*7c478bd9Sstevel@tonic-gate * the write. In this case, multiple writes are 465*7c478bd9Sstevel@tonic-gate * required. 466*7c478bd9Sstevel@tonic-gate */ 467*7c478bd9Sstevel@tonic-gate do { 468*7c478bd9Sstevel@tonic-gate nwritten = write(1, bufferp+offset, 469*7c478bd9Sstevel@tonic-gate (size_t)nitems); 470*7c478bd9Sstevel@tonic-gate if (nwritten < 0) { 471*7c478bd9Sstevel@tonic-gate if (!silent) { 472*7c478bd9Sstevel@tonic-gate if (nwritten == -1) 473*7c478bd9Sstevel@tonic-gate nwritten = 0l; 474*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(\ 475*7c478bd9Sstevel@tonic-gate "cat: output error (%d/%d characters written)\n"), nwritten, nitems); 476*7c478bd9Sstevel@tonic-gate perror(""); 477*7c478bd9Sstevel@tonic-gate } 478*7c478bd9Sstevel@tonic-gate if (bufferp != (char *)buf) 479*7c478bd9Sstevel@tonic-gate free(bufferp); 480*7c478bd9Sstevel@tonic-gate return (2); 481*7c478bd9Sstevel@tonic-gate } 482*7c478bd9Sstevel@tonic-gate offset += nwritten; 483*7c478bd9Sstevel@tonic-gate } while ((nitems -= nwritten) > 0); 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate if (bufferp != (char *)buf) 486*7c478bd9Sstevel@tonic-gate free(bufferp); 487*7c478bd9Sstevel@tonic-gate if (nitems < 0) { 488*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 489*7c478bd9Sstevel@tonic-gate gettext("cat: input error on %s: "), filenm); 490*7c478bd9Sstevel@tonic-gate perror(""); 491*7c478bd9Sstevel@tonic-gate } 492*7c478bd9Sstevel@tonic-gate } 493*7c478bd9Sstevel@tonic-gate 494*7c478bd9Sstevel@tonic-gate return (0); 495*7c478bd9Sstevel@tonic-gate } 496*7c478bd9Sstevel@tonic-gate 497*7c478bd9Sstevel@tonic-gate static int 498*7c478bd9Sstevel@tonic-gate vncat(fi) 499*7c478bd9Sstevel@tonic-gate FILE *fi; 500*7c478bd9Sstevel@tonic-gate { 501*7c478bd9Sstevel@tonic-gate int c; 502*7c478bd9Sstevel@tonic-gate int lno; 503*7c478bd9Sstevel@tonic-gate int boln; /* = 1 if at beginning of line */ 504*7c478bd9Sstevel@tonic-gate /* = 0 otherwise */ 505*7c478bd9Sstevel@tonic-gate wchar_t wc; 506*7c478bd9Sstevel@tonic-gate int len, n; 507*7c478bd9Sstevel@tonic-gate unsigned char *p1, *p2; 508*7c478bd9Sstevel@tonic-gate 509*7c478bd9Sstevel@tonic-gate lno = 1; 510*7c478bd9Sstevel@tonic-gate boln = 1; 511*7c478bd9Sstevel@tonic-gate p1 = p2 = buf; 512*7c478bd9Sstevel@tonic-gate for (;;) { 513*7c478bd9Sstevel@tonic-gate if (p1 >= p2) { 514*7c478bd9Sstevel@tonic-gate p1 = buf; 515*7c478bd9Sstevel@tonic-gate if ((len = fread(p1, 1, BUFSIZ, fi)) <= 0) 516*7c478bd9Sstevel@tonic-gate break; 517*7c478bd9Sstevel@tonic-gate p2 = p1 + len; 518*7c478bd9Sstevel@tonic-gate } 519*7c478bd9Sstevel@tonic-gate c = *p1++; 520*7c478bd9Sstevel@tonic-gate 521*7c478bd9Sstevel@tonic-gate /* 522*7c478bd9Sstevel@tonic-gate * Display newlines as "$<newline>" 523*7c478bd9Sstevel@tonic-gate * if visi_newline set 524*7c478bd9Sstevel@tonic-gate */ 525*7c478bd9Sstevel@tonic-gate if (c == '\n') { 526*7c478bd9Sstevel@tonic-gate if (nflg && boln && !bflg) 527*7c478bd9Sstevel@tonic-gate (void) printf("%6d\t", lno++); 528*7c478bd9Sstevel@tonic-gate boln = 1; 529*7c478bd9Sstevel@tonic-gate 530*7c478bd9Sstevel@tonic-gate if (visi_mode && visi_newline) 531*7c478bd9Sstevel@tonic-gate (void) putchar('$'); 532*7c478bd9Sstevel@tonic-gate (void) putchar(c); 533*7c478bd9Sstevel@tonic-gate continue; 534*7c478bd9Sstevel@tonic-gate } 535*7c478bd9Sstevel@tonic-gate 536*7c478bd9Sstevel@tonic-gate if (nflg && boln) 537*7c478bd9Sstevel@tonic-gate (void) printf("%6d\t", lno++); 538*7c478bd9Sstevel@tonic-gate boln = 0; 539*7c478bd9Sstevel@tonic-gate 540*7c478bd9Sstevel@tonic-gate /* 541*7c478bd9Sstevel@tonic-gate * For non-printable and non-cntrl chars, 542*7c478bd9Sstevel@tonic-gate * use the "M-x" notation. 543*7c478bd9Sstevel@tonic-gate */ 544*7c478bd9Sstevel@tonic-gate 545*7c478bd9Sstevel@tonic-gate if (isascii(c)) { 546*7c478bd9Sstevel@tonic-gate if (isprint(c) || visi_mode == 0) { 547*7c478bd9Sstevel@tonic-gate (void) putchar(c); 548*7c478bd9Sstevel@tonic-gate continue; 549*7c478bd9Sstevel@tonic-gate } 550*7c478bd9Sstevel@tonic-gate 551*7c478bd9Sstevel@tonic-gate /* 552*7c478bd9Sstevel@tonic-gate * For non-printable ascii characters. 553*7c478bd9Sstevel@tonic-gate */ 554*7c478bd9Sstevel@tonic-gate 555*7c478bd9Sstevel@tonic-gate if (iscntrl(c)) { 556*7c478bd9Sstevel@tonic-gate /* For cntrl characters. */ 557*7c478bd9Sstevel@tonic-gate if ((c == '\t') || (c == '\f')) { 558*7c478bd9Sstevel@tonic-gate /* 559*7c478bd9Sstevel@tonic-gate * Display tab as "^I" if visi_tab set 560*7c478bd9Sstevel@tonic-gate */ 561*7c478bd9Sstevel@tonic-gate if (visi_mode && visi_tab) { 562*7c478bd9Sstevel@tonic-gate (void) putchar('^'); 563*7c478bd9Sstevel@tonic-gate (void) putchar(c^0100); 564*7c478bd9Sstevel@tonic-gate } else 565*7c478bd9Sstevel@tonic-gate (void) putchar(c); 566*7c478bd9Sstevel@tonic-gate continue; 567*7c478bd9Sstevel@tonic-gate } 568*7c478bd9Sstevel@tonic-gate (void) putchar('^'); 569*7c478bd9Sstevel@tonic-gate (void) putchar(c^0100); 570*7c478bd9Sstevel@tonic-gate continue; 571*7c478bd9Sstevel@tonic-gate } 572*7c478bd9Sstevel@tonic-gate continue; 573*7c478bd9Sstevel@tonic-gate } 574*7c478bd9Sstevel@tonic-gate 575*7c478bd9Sstevel@tonic-gate /* 576*7c478bd9Sstevel@tonic-gate * For non-ascii characters. 577*7c478bd9Sstevel@tonic-gate */ 578*7c478bd9Sstevel@tonic-gate p1--; 579*7c478bd9Sstevel@tonic-gate if ((len = (p2 - p1)) < MB_LEN_MAX) { 580*7c478bd9Sstevel@tonic-gate for (n = 0; n < len; n++) 581*7c478bd9Sstevel@tonic-gate buf[n] = *p1++; 582*7c478bd9Sstevel@tonic-gate p1 = buf; 583*7c478bd9Sstevel@tonic-gate p2 = p1 + n; 584*7c478bd9Sstevel@tonic-gate if ((len = fread(p2, 1, BUFSIZ - n, fi)) > 0) 585*7c478bd9Sstevel@tonic-gate p2 += len; 586*7c478bd9Sstevel@tonic-gate } 587*7c478bd9Sstevel@tonic-gate 588*7c478bd9Sstevel@tonic-gate if ((len = (p2 - p1)) > MB_LEN_MAX) 589*7c478bd9Sstevel@tonic-gate len = MB_LEN_MAX; 590*7c478bd9Sstevel@tonic-gate 591*7c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)p1, len)) > 0) { 592*7c478bd9Sstevel@tonic-gate if (iswprint(wc) || visi_mode == 0) { 593*7c478bd9Sstevel@tonic-gate (void) putwchar(wc); 594*7c478bd9Sstevel@tonic-gate p1 += len; 595*7c478bd9Sstevel@tonic-gate continue; 596*7c478bd9Sstevel@tonic-gate } 597*7c478bd9Sstevel@tonic-gate } 598*7c478bd9Sstevel@tonic-gate 599*7c478bd9Sstevel@tonic-gate (void) putchar('M'); 600*7c478bd9Sstevel@tonic-gate (void) putchar('-'); 601*7c478bd9Sstevel@tonic-gate c -= 0200; 602*7c478bd9Sstevel@tonic-gate 603*7c478bd9Sstevel@tonic-gate if (isprint(c)) { 604*7c478bd9Sstevel@tonic-gate (void) putchar(c); 605*7c478bd9Sstevel@tonic-gate } 606*7c478bd9Sstevel@tonic-gate 607*7c478bd9Sstevel@tonic-gate /* For non-printable characters. */ 608*7c478bd9Sstevel@tonic-gate if (iscntrl(c)) { 609*7c478bd9Sstevel@tonic-gate /* For cntrl characters. */ 610*7c478bd9Sstevel@tonic-gate if ((c == '\t') || (c == '\f')) { 611*7c478bd9Sstevel@tonic-gate /* 612*7c478bd9Sstevel@tonic-gate * Display tab as "^I" if visi_tab set 613*7c478bd9Sstevel@tonic-gate */ 614*7c478bd9Sstevel@tonic-gate if (visi_mode && visi_tab) { 615*7c478bd9Sstevel@tonic-gate (void) putchar('^'); 616*7c478bd9Sstevel@tonic-gate (void) putchar(c^0100); 617*7c478bd9Sstevel@tonic-gate } else 618*7c478bd9Sstevel@tonic-gate (void) putchar(c); 619*7c478bd9Sstevel@tonic-gate } else { 620*7c478bd9Sstevel@tonic-gate (void) putchar('^'); 621*7c478bd9Sstevel@tonic-gate (void) putchar(c^0100); 622*7c478bd9Sstevel@tonic-gate } 623*7c478bd9Sstevel@tonic-gate } 624*7c478bd9Sstevel@tonic-gate p1++; 625*7c478bd9Sstevel@tonic-gate } 626*7c478bd9Sstevel@tonic-gate return (0); 627*7c478bd9Sstevel@tonic-gate } 628