Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!think.com!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: comp.unix.questions Subject: Re: Redirection of stderr Message-ID: <1991Mar18.045101.23238@athena.mit.edu> Date: 18 Mar 91 04:51:01 GMT References: <574@dprmpt.UUCP> <1991Mar14.220650.11736@athena.mit.edu> Sender: news@athena.mit.edu (News system) Distribution: na Organization: Massachusetts Institute of Technology Lines: 51 In article , tb@sequent.sequent.com (Tony Booker) writes: |> In article <1991Mar14.225058.13507@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes: |> However, if you do things this way, I can't see any way to put the |> original stderr back in place -- there is no "fdreopen". That's |> unfortunate. |> |> I beleive the return value of freopen() is the original file for you |> to salt away as necessary. From freopen(3): Freopen substitutes the named file in place of the open stream. It returns the original value of stream. The ori- ginal stream is closed. In other words, yes, it returns the original stream, but it's closed and replaced by the new file, so you can't do anything useful with it. To verify this, compile and run the following program. If what you say works, then the second fprintf should cause output to go to the tty; if not, the second fprintf should appear in the testfile. On my system, it does. #include #define TESTFILE "/tmp/freopen-testfile" main() { FILE *old; old = freopen(TESTFILE, "w", stdout); fprintf(stdout, "This is being written to stdout, now %s.\n", TESTFILE); fprintf(old, "This is being written to the old stdout.\n"); fclose(stdout); fclose(old); exit(0); } -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710 P.S. It's usually a good idea to actually try suggestions like this out before posting them to the net. Yes, I know, I'm not one to talk, considering that I posted about assigning to stderr when that won't work on most systems anyway. But I thought I'd point it out anyway. :-)