Xref: utzoo comp.unix.questions:32066 comp.unix.misc:1531 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!casbah.acns.nwu.edu!ftpbox!white!motcid!renkel From: renkel@motcid.UUCP (Will Renkel) Newsgroups: comp.unix.questions,comp.unix.misc Subject: Re: tee like program to pipe to another program? Summary: here is code similar to what others do with "tpipe" Message-ID: <4842@apricot12.UUCP> Date: 10 Jun 91 14:36:12 GMT References: <1991Jun6.093939.9346@dartvax.dartmouth.edu> Organization: Motorola Inc., Cellular Infrastructure Div., Arlington Heights, IL Lines: 97 In article , pd@pd@x.co.uk (Paul Davey) writes: > > >>>>> On 6 Jun 91 09:39:39 GMT, pete@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@x.co.uk IXI Limited > Paul Davey pd@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 /* 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 \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); } } }