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