Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!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 Keywords: stderr Message-ID: <1991Mar15.133707.9556@athena.mit.edu> Date: 15 Mar 91 13:37:07 GMT References: <574@dprmpt.UUCP> <1991Mar14.220650.11736@athena.mit.edu> <1991Mar14.225058.13507@athena.mit.edu> Sender: news@athena.mit.edu (News system) Distribution: na Organization: Massachusetts Institute of Technology Lines: 50 Another thing to mention about this question occurred to me in the shower this morning (really! :-). Why do you need to redirect stderr temporarily? It seems to me that there are two possible explanations for this: 1) You want the error messages your program prints to go somewhere else temporarily. 2) You want to capture the error messages printed by a process invoked by your program. If the reason is (1), then a cleaner way to do what you're trying to do would be to keep a global FILE * variable in your namespace to which your program prints errors, and then change that variable when you want to redirect errors elsewhere. In other words, the routine that replaces stderr with a file would do something like this (you add the error checking): extern FILE *error_file; extern char *error_file_name; error_file = fopen(error_file_name, "w"); and the routing that puts stderr back would simply do this: error_file = stderr; and then all of your routines that print errors would print to error_file instead of printed to stderr explicitly. If the reason is (2), then once again there is a cleaner way to do this. Obviously, you have to fork in order to run a new process if you expect control to return to your process (and if you don't expect control to return to your process, there's no reason for you to have to put stderr back, so the question is moot). After the fork, simply freopen the error file on top of stderr in the child that runs the subprocess. The parent will get to keep the original stderr, and the child's errors will go to the file. If you're using popen() or system() or something right now, then you'll either have to write your own version of popen() or system() that takes a file name to put errors in and does the freopen after the fork, or you can put a wrapper around the standard functions to be a bit clever about doing things so that you don't have to roll your own. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710