How do I get EOF from a pipe I created?

Tim Cook timcc at csv.viccol.edu.au
Wed Aug 8 01:45:45 AEST 1990


I am having a bit of fun writing a utility that parses the output of a
command that it exec's in a subprocess.  What I am doing is basically
(minus error checking):

   int pipe_descriptors[2] ;

   pipe (pipe_descriptors) ;
   if (fork ()) {
      /* Subprocess */
      dup2 (pipe_descriptors[1], 1) ;
      execl ("/dir/command", "command", "arg", 0) ;
      /*NOTREACHED*/ }

   /* Parent continues here */
   pipe_stream = fdopen (pipe_descriptors[0], "r") ;

   while (! feof (pipe_stream)) {
      fgets (buffer, sizeof (buffer) - 1, pipe_stream) ;

      /* Parsing of what is in "buffer"... */
      }

Well, I get the output of "command" coming through on "pipe_stream", but
I don't get end-of-file.  The fgets call just blocks when there is nothing
left in the pipe (and not because the last record output by "command" was
not terminated by a newline).

Can anyone help me set this up so that I see EOF?  Can anyone also tell me
how to have the parent process retain control of the tty that it was
invoked on?  At the moment, the shell regains control when the process
exec-ed by the child completes, indicating that the child got control.



More information about the Comp.unix.wizards mailing list