home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / condor40.zip / CONDOR / src / ckpt_lib / t_cat.c < prev    next >
C/C++ Source or Header  |  1989-05-15  |  6KB  |  270 lines

  1. /* 
  2. ** Copyright 1986, 1987, 1988, 1989 University of Wisconsin
  3. ** 
  4. ** Permission to use, copy, modify, and distribute this software and its
  5. ** documentation for any purpose and without fee is hereby granted,
  6. ** provided that the above copyright notice appear in all copies and that
  7. ** both that copyright notice and this permission notice appear in
  8. ** supporting documentation, and that the name of the University of
  9. ** Wisconsin not be used in advertising or publicity pertaining to
  10. ** distribution of the software without specific, written prior
  11. ** permission.  The University of Wisconsin makes no representations about
  12. ** the suitability of this software for any purpose.  It is provided "as
  13. ** is" without express or implied warranty.
  14. ** 
  15. ** THE UNIVERSITY OF WISCONSIN DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. ** THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. ** FITNESS. IN NO EVENT SHALL THE UNIVERSITY OF WISCONSIN  BE LIABLE FOR
  18. ** ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. ** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  21. ** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ** 
  23. ** Authors:  Allan Bricker and Michael J. Litzkow,
  24. **              University of Wisconsin, Computer Sciences Dept.
  25. ** 
  26. */ 
  27.  
  28.  
  29. /*
  30.  * Copyright (c) 1980 Regents of the University of California.
  31.  * All rights reserved.  The Berkeley software License Agreement
  32.  * specifies the terms and conditions for redistribution.
  33.  */
  34.  
  35. #ifndef lint
  36. static char sccsid[] = "@(#)cat.c    5.2 (Berkeley) 12/6/85";
  37. #endif not lint
  38.  
  39. /*
  40.  * Concatenate files.
  41.  */
  42.  
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46.  
  47. /* #define OPTSIZE BUFSIZ    /* define this only if not 4.2 BSD or beyond */
  48.  
  49. int    bflg, eflg, nflg, sflg, tflg, uflg, vflg;
  50. int    spaced, col, lno, inline, ibsize, obsize;
  51.  
  52. main(argc, argv)
  53. char **argv;
  54. {
  55.     int fflg = 0;
  56.     register FILE *fi;
  57.     register c;
  58.     int dev, ino = -1;
  59.     struct stat statb;
  60.     int retval = 0;
  61.  
  62.     lno = 1;
  63. #ifdef UW
  64.     if(strcmp(*argv, "num") == 0)
  65.         nflg++;
  66.     if(strcmp(*argv, "see") == 0)
  67.         vflg++;
  68.     if(strcmp(*argv, "ssp") == 0)
  69.         sflg++;
  70. #endif UW
  71.     for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) {
  72.         switch(argv[1][1]) {
  73.         case 0:
  74.             break;
  75.         case 'u':
  76.             setbuf(stdout, (char *)NULL);
  77.             uflg++;
  78.             continue;
  79.         case 'n':
  80.             nflg++;
  81.             continue;
  82.         case 'b':
  83.             bflg++;
  84.             nflg++;
  85.             continue;
  86.         case 'v':
  87.             vflg++;
  88.             continue;
  89.         case 's':
  90.             sflg++;
  91.             continue;
  92.         case 'e':
  93.             eflg++;
  94.             vflg++;
  95.             continue;
  96.         case 't':
  97.             tflg++;
  98.             vflg++;
  99.             continue;
  100.         }
  101.         break;
  102.     }
  103.     if (fstat(fileno(stdout), &statb) == 0) {
  104.         statb.st_mode &= S_IFMT;
  105.         if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
  106.             dev = statb.st_dev;
  107.             ino = statb.st_ino;
  108.         }
  109. #ifndef    OPTSIZE
  110.         obsize = statb.st_blksize;
  111. #endif
  112.     }
  113.     else
  114.         obsize = 0;
  115.     if (argc < 2) {
  116.         argc = 2;
  117.         fflg++;
  118.     }
  119.     while (--argc > 0) {
  120.         if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0')
  121.             fi = stdin;
  122.         else {
  123.             if ((fi = fopen(*argv, "r")) == NULL) {
  124.                 perror(*argv);
  125.                 retval = 1;
  126.                 continue;
  127.             }
  128.         }
  129.         if (fstat(fileno(fi), &statb) == 0) {
  130.             if ((statb.st_mode & S_IFMT) == S_IFREG &&
  131.                 statb.st_dev==dev && statb.st_ino==ino) {
  132.                 fprintf(stderr, "cat: input %s is output\n",
  133.                    fflg?"-": *argv);
  134.                 fclose(fi);
  135.                 retval = 1;
  136.                 continue;
  137.             }
  138. #ifndef    OPTSIZE
  139.             ibsize = statb.st_blksize;
  140. #endif
  141.         }
  142.         else
  143.             ibsize = 0;
  144.         if (nflg||sflg||vflg)
  145.             copyopt(fi);
  146.         else if (uflg) {
  147.             while ((c = getc(fi)) != EOF)
  148.                 putchar(c);
  149.         } else
  150.             retval |= fastcat(fileno(fi));    /* no flags specified */
  151.         if (fi!=stdin)
  152.             fclose(fi);
  153.         else
  154.             clearerr(fi);        /* reset sticky eof */
  155.         if (ferror(stdout)) {
  156.             fprintf(stderr, "cat: output write error\n");
  157.             retval = 1;
  158.             break;
  159.         }
  160.     }
  161.     exit(retval);
  162. }
  163.  
  164. copyopt(f)
  165.     register FILE *f;
  166. {
  167.     register int c;
  168.  
  169. top:
  170.     c = getc(f);
  171.     if (c == EOF)
  172.         return;
  173.     if (c == '\n') {
  174.         if (inline == 0) {
  175.             if (sflg && spaced)
  176.                 goto top;
  177.             spaced = 1;
  178.         }
  179.         if (nflg && bflg==0 && inline == 0)
  180.             printf("%6d\t", lno++);
  181.         if (eflg)
  182.             putchar('$');
  183.         putchar('\n');
  184.         inline = 0;
  185.         goto top;
  186.     }
  187.     if (nflg && inline == 0)
  188.         printf("%6d\t", lno++);
  189.     inline = 1;
  190.     if (vflg) {
  191.         if (tflg==0 && c == '\t')
  192.             putchar(c);
  193.         else {
  194.             if (c > 0177) {
  195.                 printf("M-");
  196.                 c &= 0177;
  197.             }
  198.             if (c < ' ')
  199.                 printf("^%c", c+'@');
  200.             else if (c == 0177)
  201.                 printf("^?");
  202.             else
  203.                 putchar(c);
  204.         }
  205.     } else
  206.         putchar(c);
  207.     spaced = 0;
  208.     goto top;
  209. }
  210.  
  211. fastcat(fd)
  212. register int fd;
  213. {
  214.     register int    buffsize, n, nwritten, offset;
  215.     register char    *buff;
  216.     int nreads = 0;
  217.     struct stat    statbuff;
  218.     char        *malloc();
  219.  
  220. #ifndef    OPTSIZE
  221.     if (obsize)
  222.         buffsize = obsize;    /* common case, use output blksize */
  223.     else if (ibsize)
  224.         buffsize = ibsize;
  225.     else
  226.         buffsize = BUFSIZ;
  227. #else
  228.     buffsize = OPTSIZE;
  229. #endif
  230.  
  231. fprintf(stderr, "fastcat: buffsize = %d\n", buffsize);
  232.     if ((buff = malloc(buffsize)) == NULL) {
  233.         perror("cat: no memory");
  234.         return (1);
  235.     }
  236.  
  237.     /*
  238.      * Note that on some systems (V7), very large writes to a pipe
  239.      * return less than the requested size of the write.
  240.      * In this case, multiple writes are required.
  241.      */
  242.     while ((n = read(fd, buff, buffsize)) > 0) {
  243. fprintf(stderr, "fastcat: read %d returned %d bytes\n", ++nreads, n);
  244.         offset = 0;
  245.         do {
  246.             nwritten = write(fileno(stdout), &buff[offset], n);
  247.             if (nwritten <= 0) {
  248.                 perror("cat: write error");
  249.                 exit(2);
  250.             }
  251.             offset += nwritten;
  252.         } while ((n -= nwritten) > 0);
  253.  
  254.         ckpt();
  255.     }
  256.  
  257.     free(buff);
  258.     if (n < 0) {
  259.         perror("cat: read error");
  260.         return (1);
  261.     }
  262.     return (0);
  263. }
  264.  
  265. Delay(lc)
  266. int lc;
  267. {
  268.     while( lc-- > 0 ) ;
  269. }
  270.