home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18930 < prev    next >
Encoding:
Internet Message Format  |  1992-12-28  |  1.3 KB

  1. Path: sparky!uunet!sunquest!apollo.sunquest.com!ahd
  2. From: ahd@apollo.sunquest.com (Art Dyer)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: stderr: Question
  5. Keywords: fprintf stderr
  6. Message-ID: <55458@sunquest.UUCP>
  7. Date: 28 Dec 92 16:24:45 GMT
  8. References: <1992Dec21.134608.16856@ifi.unizh.ch>
  9. Sender: news@sunquest.UUCP
  10. Organization: Sunquest Information Systems, Tucson
  11. Lines: 22
  12.  
  13. In article <1992Dec21.134608.16856@ifi.unizh.ch>,
  14. caliaro@avalon.physik.unizh.ch (Aurelio Caliaro) writes:
  15.  
  16. > I would like to redirect all output made by "fprintf(stderr, ...)" to
  17. > a procedure.  Is that possible without changing the source everytime
  18. > that stderr comes?
  19.  
  20. I've used the following technique:
  21.     Create a pipe, let's call it p.
  22.     Close file descriptor 2.
  23.     Use fcntl(p[1], F_DUPFD, 2) to re-open file descriptor 2 as
  24.     as duplicate of the writable side of your pipe.
  25.     Now, when somebody writes to stderr (or directly to file
  26.     descriptor 2), the data goes into your pipe and thus is
  27.     available for reading from p[0].  If you have asynch i/o
  28.     capability you can set up a signal handler to be called
  29.     whenever data is available to be read, otherwise you will
  30.     need to poll the pipe.
  31. Apologies for posting a UNIX-oriented reply in a C-oriented group.
  32.  
  33. --
  34. Art Dyer    ahd@apollo.sunquest.com    Voice:  (602) 570-2602
  35.