1: #ifndef lint
   2: static  char *Sccsid = "@(#)cat.c	4.2 (Berkeley) 10/9/80";
   3: #endif
   4: /*
   5:  * Concatenate files.
   6:  */
   7: 
   8: #include <stdio.h>
   9: #include <sys/types.h>
  10: #include <sys/stat.h>
  11: 
  12: extern char _sobuf[];
  13: int bflg, eflg, nflg, sflg, tflg, vflg;
  14: int spaced, lno, inline;
  15: 
  16: main(argc, argv)
  17: char **argv;
  18: {
  19:     int fflg = 0;
  20:     register FILE *fi;
  21:     register c;
  22:     int dev, ino = -1;
  23:     struct stat statb;
  24: 
  25:     lno = 1;
  26:     setbuf(stdout, _sobuf);
  27:     for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) {
  28:         switch(argv[1][1]) {
  29:         case 0:
  30:             break;
  31:         case 'u':
  32:             setbuf(stdout, (char *)NULL);
  33:             continue;
  34:         case 'n':
  35:             nflg++;
  36:             continue;
  37:         case 'b':
  38:             bflg++;
  39:             nflg++;
  40:             continue;
  41:         case 'v':
  42:             vflg++;
  43:             continue;
  44:         case 's':
  45:             sflg++;
  46:             continue;
  47:         case 'e':
  48:             eflg++;
  49:             vflg++;
  50:             continue;
  51:         case 't':
  52:             tflg++;
  53:             vflg++;
  54:             continue;
  55:         }
  56:         break;
  57:     }
  58:     fstat(fileno(stdout), &statb);
  59:     statb.st_mode &= S_IFMT;
  60:     if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
  61:         dev = statb.st_dev;
  62:         ino = statb.st_ino;
  63:     }
  64:     if (argc < 2) {
  65:         argc = 2;
  66:         fflg++;
  67:     }
  68:     while (--argc > 0) {
  69:         if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0')
  70:             fi = stdin;
  71:         else {
  72:             if ((fi = fopen(*argv, "r")) == NULL) {
  73:                 fprintf(stderr, "cat: can't open %s\n", *argv);
  74:                 continue;
  75:             }
  76:         }
  77:         fstat(fileno(fi), &statb);
  78:         if (statb.st_dev==dev && statb.st_ino==ino) {
  79:             fprintf(stderr, "cat: input %s is output\n",
  80:                fflg?"-": *argv);
  81:             fclose(fi);
  82:             continue;
  83:         }
  84:         if (nflg||sflg||vflg)
  85:             copyopt(fi);
  86:         else {
  87:             while ((c = getc(fi)) != EOF)
  88:                 putchar(c);
  89:         }
  90:         if (fi!=stdin)
  91:             fclose(fi);
  92:     }
  93:     if (ferror(stdout))
  94:         fprintf(stderr, "cat: output write error\n");
  95:     return(0);
  96: }
  97: 
  98: copyopt(f)
  99:     register FILE *f;
 100: {
 101:     register int c;
 102: 
 103: top:
 104:     c = getc(f);
 105:     if (c == EOF)
 106:         return;
 107:     if (c == '\n') {
 108:         if (inline == 0) {
 109:             if (sflg && spaced)
 110:                 goto top;
 111:             spaced = 1;
 112:         }
 113:         if (nflg && bflg==0 && inline == 0)
 114:             printf("%6d\t", lno++);
 115:         if (eflg)
 116:             putchar('$');
 117:         putchar('\n');
 118:         inline = 0;
 119:         goto top;
 120:     }
 121:     if (nflg && inline == 0)
 122:         printf("%6d\t", lno++);
 123:     inline = 1;
 124:     if (vflg) {
 125:         if (tflg==0 && c == '\t')
 126:             putchar(c);
 127:         else {
 128:             if (c > 0177) {
 129:                 printf("M-");
 130:                 c &= 0177;
 131:             }
 132:             if (c < ' ')
 133:                 printf("^%c", c+'@');
 134:             else if (c == 0177)
 135:                 printf("^?");
 136:             else
 137:                 putchar(c);
 138:         }
 139:     } else
 140:         putchar(c);
 141:     spaced = 0;
 142:     goto top;
 143: }

Defined functions

copyopt defined in line 98; used 1 times
  • in line 85
main defined in line 16; never used

Defined variables

Sccsid defined in line 2; never used
bflg defined in line 13; used 2 times
eflg defined in line 13; used 2 times
inline defined in line 14; used 5 times
lno defined in line 14; used 3 times
nflg defined in line 13; used 5 times
sflg defined in line 13; used 3 times
spaced defined in line 14; used 3 times
tflg defined in line 13; used 2 times
vflg defined in line 13; used 5 times
Last modified: 1982-09-26
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 865
Valid CSS Valid XHTML 1.0 Strict