1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)cat.c	5.2 (Berkeley) 12/6/85";
   9: #endif not lint
  10: 
  11: /*
  12:  * Concatenate files.
  13:  */
  14: 
  15: #include <stdio.h>
  16: #include <sys/types.h>
  17: #include <sys/stat.h>
  18: 
  19: /* #define OPTSIZE BUFSIZ	/* define this only if not 4.2 BSD or beyond */
  20: 
  21: int bflg, eflg, nflg, sflg, tflg, uflg, vflg;
  22: int spaced, col, lno, inline, ibsize, obsize;
  23: 
  24: main(argc, argv)
  25: char **argv;
  26: {
  27:     int fflg = 0;
  28:     register FILE *fi;
  29:     register c;
  30:     int dev, ino = -1;
  31:     struct stat statb;
  32:     int retval = 0;
  33: 
  34:     lno = 1;
  35:     for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) {
  36:         switch(argv[1][1]) {
  37:         case 0:
  38:             break;
  39:         case 'u':
  40:             setbuf(stdout, (char *)NULL);
  41:             uflg++;
  42:             continue;
  43:         case 'n':
  44:             nflg++;
  45:             continue;
  46:         case 'b':
  47:             bflg++;
  48:             nflg++;
  49:             continue;
  50:         case 'v':
  51:             vflg++;
  52:             continue;
  53:         case 's':
  54:             sflg++;
  55:             continue;
  56:         case 'e':
  57:             eflg++;
  58:             vflg++;
  59:             continue;
  60:         case 't':
  61:             tflg++;
  62:             vflg++;
  63:             continue;
  64:         }
  65:         break;
  66:     }
  67:     if (fstat(fileno(stdout), &statb) == 0) {
  68:         statb.st_mode &= S_IFMT;
  69:         if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
  70:             dev = statb.st_dev;
  71:             ino = statb.st_ino;
  72:         }
  73: #ifndef OPTSIZE
  74:         obsize = statb.st_blksize;
  75: #endif
  76:     }
  77:     else
  78:         obsize = 0;
  79:     if (argc < 2) {
  80:         argc = 2;
  81:         fflg++;
  82:     }
  83:     while (--argc > 0) {
  84:         if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0')
  85:             fi = stdin;
  86:         else {
  87:             if ((fi = fopen(*argv, "r")) == NULL) {
  88:                 perror(*argv);
  89:                 retval = 1;
  90:                 continue;
  91:             }
  92:         }
  93:         if (fstat(fileno(fi), &statb) == 0) {
  94:             if ((statb.st_mode & S_IFMT) == S_IFREG &&
  95:                 statb.st_dev==dev && statb.st_ino==ino) {
  96:                 fprintf(stderr, "cat: input %s is output\n",
  97:                    fflg?"-": *argv);
  98:                 fclose(fi);
  99:                 retval = 1;
 100:                 continue;
 101:             }
 102: #ifndef OPTSIZE
 103:             ibsize = statb.st_blksize;
 104: #endif
 105:         }
 106:         else
 107:             ibsize = 0;
 108:         if (nflg||sflg||vflg)
 109:             copyopt(fi);
 110:         else if (uflg) {
 111:             while ((c = getc(fi)) != EOF)
 112:                 putchar(c);
 113:         } else
 114:             retval |= fastcat(fileno(fi));  /* no flags specified */
 115:         if (fi!=stdin)
 116:             fclose(fi);
 117:         else
 118:             clearerr(fi);       /* reset sticky eof */
 119:         if (ferror(stdout)) {
 120:             fprintf(stderr, "cat: output write error\n");
 121:             retval = 1;
 122:             break;
 123:         }
 124:     }
 125:     exit(retval);
 126: }
 127: 
 128: copyopt(f)
 129:     register FILE *f;
 130: {
 131:     register int c;
 132: 
 133: top:
 134:     c = getc(f);
 135:     if (c == EOF)
 136:         return;
 137:     if (c == '\n') {
 138:         if (inline == 0) {
 139:             if (sflg && spaced)
 140:                 goto top;
 141:             spaced = 1;
 142:         }
 143:         if (nflg && bflg==0 && inline == 0)
 144:             printf("%6d\t", lno++);
 145:         if (eflg)
 146:             putchar('$');
 147:         putchar('\n');
 148:         inline = 0;
 149:         goto top;
 150:     }
 151:     if (nflg && inline == 0)
 152:         printf("%6d\t", lno++);
 153:     inline = 1;
 154:     if (vflg) {
 155:         if (tflg==0 && c == '\t')
 156:             putchar(c);
 157:         else {
 158:             if (c > 0177) {
 159:                 printf("M-");
 160:                 c &= 0177;
 161:             }
 162:             if (c < ' ')
 163:                 printf("^%c", c+'@');
 164:             else if (c == 0177)
 165:                 printf("^?");
 166:             else
 167:                 putchar(c);
 168:         }
 169:     } else
 170:         putchar(c);
 171:     spaced = 0;
 172:     goto top;
 173: }
 174: 
 175: fastcat(fd)
 176: register int fd;
 177: {
 178:     register int    buffsize, n, nwritten, offset;
 179:     register char   *buff;
 180:     struct stat statbuff;
 181:     char        *malloc();
 182: 
 183: #ifndef OPTSIZE
 184:     if (obsize)
 185:         buffsize = obsize;  /* common case, use output blksize */
 186:     else if (ibsize)
 187:         buffsize = ibsize;
 188:     else
 189:         buffsize = BUFSIZ;
 190: #else
 191:     buffsize = OPTSIZE;
 192: #endif
 193: 
 194:     if ((buff = malloc(buffsize)) == NULL) {
 195:         perror("cat: no memory");
 196:         return (1);
 197:     }
 198: 
 199:     /*
 200: 	 * Note that on some systems (V7), very large writes to a pipe
 201: 	 * return less than the requested size of the write.
 202: 	 * In this case, multiple writes are required.
 203: 	 */
 204:     while ((n = read(fd, buff, buffsize)) > 0) {
 205:         offset = 0;
 206:         do {
 207:             nwritten = write(fileno(stdout), &buff[offset], n);
 208:             if (nwritten <= 0) {
 209:                 perror("cat: write error");
 210:                 exit(2);
 211:             }
 212:             offset += nwritten;
 213:         } while ((n -= nwritten) > 0);
 214:     }
 215: 
 216:     free(buff);
 217:     if (n < 0) {
 218:         perror("cat: read error");
 219:         return (1);
 220:     }
 221:     return (0);
 222: }

Defined functions

copyopt defined in line 128; used 1 times
fastcat defined in line 175; used 1 times
main defined in line 24; never used

Defined variables

bflg defined in line 21; used 2 times
col defined in line 22; never used
eflg defined in line 21; used 2 times
ibsize defined in line 22; used 4 times
inline defined in line 22; used 5 times
lno defined in line 22; used 3 times
nflg defined in line 21; used 5 times
obsize defined in line 22; used 4 times
sccsid defined in line 8; never used
sflg defined in line 21; used 3 times
spaced defined in line 22; used 3 times
tflg defined in line 21; used 2 times
uflg defined in line 21; used 2 times
vflg defined in line 21; used 5 times
Last modified: 1987-02-08
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1568
Valid CSS Valid XHTML 1.0 Strict