home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / tpipe / tpipe.c < prev   
Encoding:
C/C++ Source or Header  |  1990-06-07  |  1.6 KB  |  63 lines

  1. /* tpipe.c -- tee a pipeline into two pipelines. Like tee(1) but 
  2.    argument is a command or pipeline rather than a file.
  3.  
  4.    See the man page tpipe(1) supplied with this software.
  5.  
  6.    This version uses the unix system calls popen(3), read(2), and
  7.    write(2).  It uses write(2) to write directly to the fileno() of
  8.    of the file pointer stream returned by popen.
  9.  
  10.    I've tried it out under BSD, System V, and an older version of unix,
  11.    but:
  12.  
  13.    THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT EXPRESS OR IMPLIED 
  14.    WARRANTY.
  15.  
  16.    Version 1.02 (4 Mar 1989) (Use fileno())
  17.  
  18. --
  19. David B Rosen, Cognitive & Neural Systems                  rosen@bucasb.bu.edu
  20. Center for Adaptive Systems                 rosen%bucasb@{buacca,bu-it}.bu.edu
  21. Boston University              {mit-eddie,harvard,uunet}!bu.edu!thalamus!rosen
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26.  
  27. /*#define NOHACK*/
  28.  
  29. #ifndef BUFSIZ
  30. #define BUFSIZ 2048
  31. #endif /*BUFSIZ*/
  32.  
  33. int main(argc, argv)
  34.      int argc;
  35.      char *argv[];
  36. {
  37.   char buf[BUFSIZ];
  38.   register FILE *subpipeline = NULL;
  39.   register unsigned n;
  40.  
  41.   if (argc == 2){
  42.     if (*argv[1]) {
  43.       if ((subpipeline = popen(argv[1],"w")) == NULL) {
  44.     fprintf(stderr, "%s: can't create subpipeline %s\n", argv[0], argv[1]);
  45.     exit(1);
  46.       }
  47.     }
  48.   } else if (argc > 2) {
  49.     fprintf(stderr, "usage: %s [pipeline]\n", argv[0]);
  50.     exit(2);
  51.   }
  52.  
  53.   while ((n = read(0, buf, BUFSIZ)) > 0) {
  54.     write(1, buf, n); /* write to standard output */
  55.     if (subpipeline) {  /* write to subpipeline: */
  56.       write((int)fileno(subpipeline), buf, n);
  57.     }
  58.   }
  59.  
  60.   if (subpipeline) pclose(subpipeline);
  61.   return 0;
  62. }
  63.