Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!think!husc6!rice!titan!phil From: phil@titan.rice.edu (William LeFebvre) Newsgroups: comp.sys.amiga.tech Subject: Re: Is there a preferred way to do popen()? Summary: Unix pipe is NOT bi-directional Keywords: popen, pipe, dup Message-ID: <1806@kalliope.rice.edu> Date: 22 Aug 88 20:13:36 GMT References: <618@super.ORG> <601@faui44.informatik.uni-erlangen.de> Sender: usenet@rice.edu Reply-To: phil@Rice.edu (William LeFebvre) Organization: Rice University, Houston Lines: 43 In article <601@faui44.informatik.uni-erlangen.de> mlelstv@faui44.UUCP (Michael van Elst) writes: >In article <618@super.ORG> rminnich@metropolis.super.org (Ronald G Minnich) writes: >>... >> Am i missing something? Is there a way to do popen(), within the >>bounds of a straight workbench 1.2? > >Hmm, you say PIPE: is a non-standard hack ? I know of several >good pipe handlers that work. Anyway, it is difficult to write >a popen() function that matches the UNIX implementation of pipes. > >popen() returns two filehandles that can both be used for >input and for ouput. In usual one task then closes input and >the other closes ouput. Eh?!? Which weird Unix are you using? On every Unix I've ever used, popen returns ONE (that's 1) file stream pointer ("FILE *"). It is opened for either read *OR* write, but never both. Popen takes two arguments: the command to execute and the "type" which is either "r" for reading *OR* "w" for writing. The official and documented semantics for a Unix pipe (as defined by the pipe(2) manual page) are ONE-WAY. Pipe creates two file descriptors: one for reading ONLY and one for writing ONLY. Those are the *documented* semantics. Some versions of Unix (such as BSD) implemented pipes as just another form of network sockets. So it turned out that the descriptors were two ends of a complete socket and both could be read and written. But a Unix program cannot rely on that peculiarity. If it does, it is wrong! However, there is something else about popen that is hard to implement: file descriptor sharing. The child created by popen shares all the parent's files (except for the pipe, of course) including standard in, out, and error. So popen can be used to start an input or output filter. In order for popen to be completely correct, one would have to preserve the "file pointer sharing": if the child writes 10 bytes (to its inherited standard output) then the parent writes 10 bytes (also to standard out), the file should have 20 bytes, not 10. But this difficulty has nothing to do with pipes: this requires emulating the semantics of "dup" and "dup2". William LeFebvre Department of Computer Science Rice University