home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2189 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  1.9 KB

  1. From: ch@dce.ie (Charles Bryant)
  2. Newsgroups: comp.unix.programmer,alt.sources
  3. Subject: Re: filters
  4. Message-ID: <1990Dec4.113403.14711@dce.ie>
  5. Date: 4 Dec 90 11:34:03 GMT
  6.  
  7. In article <1990Dec4.105612.14422@dce.ie> I write:
  8. > See <1990Dec4.103255.14195@dce.ie> in alt.sources.
  9.  
  10. Well I have just found out that our news feed has a problem with
  11. alt.sources so rather than disappoint all you eager readers who can't
  12. live without a new cat, here's the said feline:
  13.  
  14.  
  15. To use this you will need:
  16.     read(2), write(2), open(2), close(2), exit(2)
  17. ----------------------------------------------------------------
  18. /* cat.c - the infamous cat(1) without bell and whistles */
  19. #include <fcntl.h>
  20.     /* for O_RDONLY */
  21. #include <stdio.h>
  22.     /* for BUFSIZ only */
  23. #define error(s) write(2, (s), sizeof(s)-1)
  24. #define fileerr(msg, fil) (error(msg), perror(fil), status=1)
  25.  
  26. int status=0;    /* exit status */
  27.  
  28. static void cat(fd, nam)
  29. int fd;
  30. char *nam;
  31. {
  32.  for (;;)
  33.     {
  34.      char iobuff[BUFSIZ];
  35.      register numin;
  36.      numin = read(fd, iobuff, sizeof(iobuff));
  37.      if (numin==0) return;
  38.      if (numin < 0)
  39.         {
  40.          fileerr("Read error: ", nam);
  41.          return;
  42.         }
  43.      {
  44.       register written;
  45.       for (written=0; written<numin;)
  46.         {
  47.          register i = write(1, iobuff+written, numin-written);
  48.          if (i<0)
  49.             {
  50.              fileerr("Write error writing ", nam);
  51.              exit(1);
  52.             }
  53.          written += i;
  54.         }
  55.      }
  56.     }
  57. }
  58.  
  59. int main(argc,argv)
  60. char **argv;
  61. int argc;
  62. {
  63.  int i;
  64.  if (argc==1)
  65.     {
  66.      cat(0, "<stdin>");
  67.      exit(status);
  68.     }
  69.  for (i=1; i<argc; i++)
  70.     {
  71.      int fd=open(argv[i], O_RDONLY);
  72.      if (fd<0)
  73.         {
  74.          fileerr("Can't open ", argv[i]);
  75.          continue;
  76.         }
  77.      cat(fd, argv[i]);
  78.      if (close(fd)) fileerr("Error closing input file: ", argv[i]);
  79.     }
  80.  if (close(1)) fileerr("Error closing output: ","<stdout>");
  81.  exit(status);
  82. }
  83. -- 
  84. Charles Bryant (ch@dce.ie)
  85. --
  86. /usr/ch/.signature: Block device required
  87. -- 
  88. Charles Bryant (ch@dce.ie)
  89. --
  90. /usr/ch/.signature: Block device required
  91.