[comp.unix.internals] How can I write an FPIPE

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (02/08/91)

djm@dmntor.UUCP (David McKellar) wrote:
>  pipe() returns those int file descriptors so I can't
>  use fprintf().  (sprintf() followed by write() is clumbsy!)
>
>  What I want is an fpipe() that does exactly the same as pipe
>  but returns type FILE.  Possible ?

Do a man on fdopen.

int fpipe(FILE *x[2])
{
int pipes[2];

   if (pipe(pipes) == -1)
      return -1;
   if (!(x[0] = fdopen(pipes[0],"r")))
      {
      close(pipes[0]);
      close(pipes[1]);
      return -1;
      }
   if (!(x[1] = fdopen(pipes[1],"w")))
      {
      fclose(x[0]);
      close(pipes[1]);
      return -1;
      }
   return 0;
}

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
10 PRINT "PRINCETON CS"          | I think sexual ecstasy's overrated.
20 GOTO 10                       | [Your blood pressure just went up.]
Princeton University would like to apologize to everyone for this article.