home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10274 < prev    next >
Encoding:
Text File  |  1992-08-21  |  1.3 KB  |  58 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!cs.utexas.edu!mercury.unt.edu!ponder.csci.unt.edu!gene
  3. From: gene@ponder.csci.unt.edu (Gene De Lisa)
  4. Subject: need help reading from a child
  5. Message-ID: <1992Aug21.202934.3736@mercury.unt.edu>
  6. Sender: usenet@mercury.unt.edu (UNT USENet Adminstrator)
  7. Organization: University of North Texas, Denton
  8. Date: Fri, 21 Aug 1992 20:29:34 GMT
  9. Lines: 47
  10.  
  11.  
  12. This code hangs on the read (or fread). I simply want to
  13. exec the child and read both its stdout and stderr.
  14. suggestions?
  15. (previous imbrications commented out)
  16.  
  17.  
  18.      if (pipe(pipefds) < 0) {
  19.       perror("pipe");
  20.       return(NULL);
  21.      }
  22.  
  23.      if ((pid = fork()) < 0) {
  24.       perror("fork");
  25.       return(NULL);
  26.      }
  27.      if (pid == 0) {
  28. #if 0
  29.       close(0);
  30.       close(1);
  31.       close(2);
  32.       for(j=3; j<getdtablesize(); j++)
  33.            close(j);
  34. #endif
  35.       dup2(pipefds[0],0);
  36.       dup2(pipefds[1],1);
  37.       dup2(pipefds[1],2);
  38.       execvp(*args, args);
  39.       perror("on execvp");
  40.       exit(-1);
  41.      }
  42.     if ((fp = fdopen(pipefds[0], "r")) == NULL) {
  43.       perror("fdopen");
  44.      }
  45.  
  46.      /*while ((n=read(pipefds[0], &buf[count], sizeof(buf))) > 0) */
  47.  
  48.      while ((n=fread(&buf[count], sizeof(char), sizeof(buf), fp)) > 0) {
  49.       count += n;
  50.       printf("c=%d\n", count);
  51.      }
  52.  
  53.      fclose(fp);
  54.      while (wait((int *) 0) != pid)
  55.       ;
  56.  
  57.  
  58.