Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcvax!ukc!dcl-cs!bath63!ma6nrr From: ma6nrr@bath63.ux63.bath.ac.uk (Rashbrook) Newsgroups: comp.lang.c Subject: Re: pipe flushing Message-ID: <658@bath63.ux63.bath.ac.uk> Date: Sun, 14-Dec-86 18:39:06 EST Article-I.D.: bath63.658 Posted: Sun Dec 14 18:39:06 1986 Date-Received: Sat, 20-Dec-86 22:10:00 EST References: <523@bath63.bath.ac.uk> <721@dg_rtp.UUCP> Reply-To: ma6nrr@ux63.bath.ac.uk (Rashbrook) Organization: University of Bath, England Lines: 98 Sorry,reply on my rn doesn't work. If you are Wayne Throop,or you think you can help,read on,else junk quickly. Please could you either a) mail me the fflush manual (I can't find it anywhere) b) show me what to do: Here is the actual program (called sio,at the moment,it could be called tee): ------------------cut here,it's your vdu------------------ /********************************************************/ /* */ /* Program to synchronise i/o for a program into a file.*/ /* Usage: sio file program */ /* */ /********************************************************/ #include#include #include #define lenu (unsigned)len char buffer[128]; /* read/write buffer arbitrary length */ int pipe0[2],pipe1[2],tty,outfile,len; void exit(); /* this is for lint */ main(argc,argv,envp) int argc; char *argv[],*envp[]; { if (argc!=3) { /* bad no. of args */ fprintf(stderr,"Usage: %s file program\n",*argv); exit(2); } if (outfile=creat(*++argv,0666),outfile==EOF) { /* attempt to open file */ fprintf(stderr,"%s: cannot open\n",*argv); exit(1); } pipe(pipe0); /* pipe for i/p to program */ pipe(pipe1); /* pipe for o/p from program */ if (fork()) parent(); else child(++argv,envp); /* now execute required program */ exit(0); /* this is for lint */ } stop() { while ((len=read(pipe1[0],buffer,128))>0) { /* clear output buffer */ write(tty,buffer,lenu); /* NB lenu is a #define ! */ write(outfile,buffer,lenu); } exit(0); } parent() /* parent writes to outfile */ { int (*signal())(); /* this is for lint */ close(pipe0[0]); /* enable read from pipe without waiting */ fcntl(pipe1[0],F_SETFL,fcntl(pipe1[0],F_GETFL,0)|O_NDELAY); tty=open("/dev/tty",O_RDWR | O_NDELAY); /* open to console */ signal(18,stop); /* trap program's death */ for (;;) { /* transfer i/o until program dies */ len=read(tty,buffer,128); write(pipe0[1],buffer,lenu); write(outfile,buffer,lenu); len=read(pipe1[0],buffer,128); if (len==EOF) stop(); /* so it's messy,but it's the only way with O_NDELAY */ write(tty,buffer,lenu); write(outfile,buffer,lenu); } } child(argv,envp) char *argv[],*envp[]; { close(0); /* these aren't needed any more */ close(1); if (dup(pipe0[0])!=0 || dup(pipe1[1])!=1) { /* connect pipes to stdin/out */ fprintf(stderr,"error in pipes\n"); exit(1); } close(pipe0[1]); close(pipe1[0]); close(outfile); setbuf(stdout,(char *)0); /* test lines */ printf("Hit return:\n"); while (getchar()!='\n'); /* this correctly prints up prompt, waits for return,then acts as program options |tee file */ if (execve(*argv,argv+1,envp)==EOF) fprintf(stderr,"%s: cannot execute\n",*argv); /* finally execute program (hopefully!) */ } /* so I don't need an if,so what? */ /* END! Thanks for any help. */