RE: [gtk-list] Re: Grabbing terminal output



Thx Erik, 

you the man.

But I wil still get Advanced UNIX programming book that was sujested
earlier.

Thx again guyz.

-----Original Message-----
From: Erik Mouw [mailto:J.A.K.Mouw@its.tudelft.nl]
Sent: Wednesday, June 02, 1999 4:39 AM
To: gtk-list@redhat.com
Subject: [gtk-list] Re: Grabbing terminal output


On Tue, 01 Jun 1999 18:23:17 -0400, gtk-list@redhat.com (Steven Hébert)
wrote:
> Here is my problem,
> 
> If I call gcc from a program can I grab th eoutput it generated?

Of course, gcc uses stdout and stderr for output, and you can just
redirect those file descriptors (every shell can do it). Code looks more
or less like:


  int p[2], q[2], pid, status, retval;
 
  pipe(p); /* create pipes */
  pipe(q);

  pid=fork();

  if(pid==-1)
    {
      perror("fork");
      exit(-1);
    }
  else if(pid==0) /* child */
    {
      close(p[0]); /* close read side of stdout pipe */
      close[q[0]); /* same for stderr */
      close(1);    /* close stdout */
      close(2);    /* and stderr */
      dup(p[1]);   /* copy write side of pipe to stdout */
      dup(q[1]);   /* ditto */
      retval=system("gcc -Wall whatever you want");
      _exit(retval);
    }
  else /* parent */
    {
      close(p[1]);  /* close write side of pipes */
      close(q[1]);

      for(;;)
        {
          if(waitpid(pid, &status, WNOHANG) == pid)
            {
              waitpid(pid, &status, WUNTRACED); /* anti zombie code */
              close(p[0]);
              close(q[0]);
              break;
            }
    
          /* use read() to read from p[0] and q[0] */
          /* Copy output to GTK widget */
        }

      return(WEXITSTATUS(status));
    }


For a more advanced version (with proper error checking and using
select()), use the DOSemu (www.dosemu.org) source, function
run_unix_command() in src/base/misc/dos2linux.c. 

For *all* the details, get yourself a copy of "Advanced Programming in the
UNIX Environment" by Richard Stevens (Addison-Wesley).


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/


-- 
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]