tee like program to pipe to another program?

Will Renkel renkel at motcid.UUCP
Tue Jun 11 00:36:12 AEST 1991


In article <PD.91Jun7133525 at powys.x.co.uk>, pd at pd@x.co.uk (Paul Davey) writes:
> 
> >>>>> On 6 Jun 91 09:39:39 GMT, pete at othello.dartmouth.edu (Pete Schmitt) said:
> 
> Pete> Is there a tee like program that will pipe down to another program?
> 
> You can get the effect of this on some machines by using tee and a
> named pipe or fifo file. (This is not a general solution.)
> 
> to pipe the output from lotsofdataprogram to stream1 and stream2,
> 
> 
> mknod fifo p
> lotsofdataprogram | tee fifo | stream1 ... &
> cat fifo | stream2 ...
> 
> 
> Don't forget to rm fifo when done with it.
> 
> --
>  Regards,			 pd at x.co.uk          IXI Limited
> 	Paul Davey		 pd at ixi.uucp         62-74 Burleigh St.
> 				 ...!uunet!ixi!pd    Cambridge  U.K.
>  "These are interesting times"   +44 223 462 131     CB1  1OJ      
> 				 USA: 1 800 XDESK 57


Got this code while working at ATT Bell Labs
I call it "branch" and its use is like the following

echo HI | branch 'grep HI | grep HI >stream1' | while read p1
do
echo $p1
done | tee stream2


--------- CUT HERE ----------------.
#include <stdio.h>

/* Similiar to the shell command "tee", but the standard input
 * is copied into two different pipelines at the same time
 *J. Leth, IH 6E-318 x6613.  */

#define STRLEN 255
#define errexit(x,arg)  fprintf(stderr, x, arg); exit(1)

main(argc, argv)
int argc;
char *argv[];
     {
     char *command;
     int fildes[2];  /* file descriptors for created pipe
     * fildes[0] is reader, fildes[1] is writer */
     if (argc != 2)
          {
          printf("\n\nUsage:\tbranch <command>\n");
          printf("Copies standard input to standard output, and\n");
          printf("at the same time copies it to the given command.\n");
          printf("The Command may itself be a pipeline (be careful\n");
          printf("to enclose it in quotes).\n");
          printf("example - cat abc | branch \"grep tss >temp1\" | grep qqq\n");
          errexit("\n\n\n",1);
          }
     command = argv[1];
          if (pipe(fildes) == -1) {
          errexit("branch: can't get new pipe\n",0);
          }
     switch (fork())
          {
          case -1:
          errexit("branch: can't fork\n",0);
          case 0:
          /* brancher process -- Execute command with standard input
          * taken from pipe from forker process. */
          close(fildes[1]);        /* close writer */
          
          /* Duplicate pipe reader as standard input */
          close(0);
          dup(fildes[0]);
          execl("/bin/sh", "/bin/sh", "-c", command, 0);
          default:
               {       /* Forker process */
               int c;
               FILE *pipewrt;
               close(fildes[0]);       /* close reader */
               pipewrt = fdopen(fildes[1], "w");
               setbuf(pipewrt, NULL);
                    while(!feof(stdin) && !ferror(stdin)) {
                    putchar(c = getchar());
                    putc(c, pipewrt);
                    }
               fclose(pipewrt);
               fclose(stdin);
               exit(0);
               }
          }
     }



More information about the Comp.unix.misc mailing list