Dissimilar Program Interaction?

Stuart D. Gathman stuart at BMS-AT.UUCP
Sat Oct 4 06:20:38 AEST 1986


In article <19300054 at uiucdcsb>, kadie at uiucdcsb.cs.uiuc.edu writes:

> Looking through the UNIX library I found a function that
> does almost what I want, namely popen. As in:
>      FILE *pstrm;
>      pstrm = popen("cat >response","w")
> This will start cat running such that the executive can
> make stream writes (fprint) to it. The problem is 
> that I want to both fscan and fprint with each subprocesses.

> An alternative function is the pipe command. It allows
> read and write. Unfortunately I don't know how to
> connect the pipe(s) to standard input and standard output
> of the sub-program.

/* coming right up . . . */

int twoway_pipe(p,cmd)
  int *p;
  char *cmd;
{
  int pr[2], pw[2], pid;
  if (pipe(pr)) return -1;
  if (pipe(pw)) {
    close(pr[0]); close(pr[1]);
    return -1;
  }
  pid = fork();
  if (pid<0) return -1;
  if (pid) {		/* parent */
    close(pr[1]); close(pw[0]);
    p[0] = pr[0]; p[1] = pw[1];
    return 0;
  }
  close(0); close(1);
  dup(pw[0]); dup(pr[1]);	/* define stdin & stdout */

  /* you might want to close other (all>2) files here */

  close(pr[0]); close(pw[1]);	/* close original pipe fd's */
  close(pr[1]); close(pw[0]);
  execlp("/bin/sh","sh","-c",cmd,0);
  return -1;
}

> Also this does not permit fscan and fprint.

{
  FILE *in, *out;
  int p[2];
  if (twoway_pipe(p,"mycommand")) perror("makepipe");
  rdstrm = fdopen(p[0],"r");	/* make fd's into streams */
  wrstrm = fdopen(p[1],"w");
  . . .
}

P.S.  I can't stand fscan.  I can't stand null terminated strings either.
	Fortunately 'C' doesn't force you to use them.
-- 
Stuart D. Gathman	<..!seismo!{vrdxhq|dgis}!BMS-AT!stuart>



More information about the Comp.unix mailing list