pipe flushing

Rashbrook ma6nrr at bath63.ux63.bath.ac.uk
Mon Dec 15 09:39:06 AEST 1986


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 <stdio.h>
#include <fcntl.h>
#include <signal.h>
#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. */



More information about the Comp.lang.c mailing list