home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / cat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  1.1 KB  |  64 lines

  1. /*
  2.  * Concatenate files.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8.  
  9. char    stdbuf[BUFSIZ];
  10.  
  11. main(argc, argv)
  12. char **argv;
  13. {
  14.     int fflg = 0;
  15.     register FILE *fi;
  16.     register c;
  17.     int dev, ino = -1;
  18.     struct stat statb;
  19.  
  20.     setbuf(stdout, stdbuf);
  21.     for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) {
  22.         switch(argv[1][1]) {
  23.         case 0:
  24.             break;
  25.         case 'u':
  26.             setbuf(stdout, (char *)NULL);
  27.             continue;
  28.         }
  29.         break;
  30.     }
  31.     fstat(fileno(stdout), &statb);
  32.     statb.st_mode &= S_IFMT;
  33.     if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
  34.         dev = statb.st_dev;
  35.         ino = statb.st_ino;
  36.     }
  37.     if (argc < 2) {
  38.         argc = 2;
  39.         fflg++;
  40.     }
  41.     while (--argc > 0) {
  42.         if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0')
  43.             fi = stdin;
  44.         else {
  45.             if ((fi = fopen(*argv, "r")) == NULL) {
  46.                 fprintf(stderr, "cat: can't open %s\n", *argv);
  47.                 continue;
  48.             }
  49.         }
  50.         fstat(fileno(fi), &statb);
  51.         if (statb.st_dev==dev && statb.st_ino==ino) {
  52.             fprintf(stderr, "cat: input %s is output\n",
  53.                fflg?"-": *argv);
  54.             fclose(fi);
  55.             continue;
  56.         }
  57.         while ((c = getc(fi)) != EOF)
  58.             putchar(c);
  59.         if (fi!=stdin)
  60.             fclose(fi);
  61.     }
  62.     return(0);
  63. }
  64.