[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: freopen -- the easy way (was: Re: LYNX-DEV 4DOS)
From: |
Laura Eaves |
Subject: |
Re: freopen -- the easy way (was: Re: LYNX-DEV 4DOS) |
Date: |
Thu, 8 May 1997 13:30:41 -0400 (EDT) |
> Date: Thu, 8 May 1997 10:23:47 -0400
> From: address@hidden (Larry W. Virden, x2487)
>...
> To handle the case of the freopen failing, you might be able to use
> something like
> int oldstderrfd;
> FILE *newstderr;
>
> if ((oldstderrfd = dup(fileno(stderr))) != -1 ) {
> /* Do your code to do the freopen of stderr, etc.) */
> }
>
> /* Now, when it's time to go back ... */
> if ( (stderr = fdopen(oldstderr,"w+") ) != NULL ) {
> /* stderr should be back to where it was */
> }
Is fileno defined on non-unix systems?
Also, "stderr = fdopen(..." won't compile as stderr isn't an lvalue.
How about something like the following (I knew I'd blow my lunch hour...
but this seems to work):
#include <stdio.h>
int oldstderrfd;
void restore_stderr(closed)
int closed;
{
if ( oldstderrfd != -1 ) {
FILE* newstderr;
if ( (newstderr = fdopen(oldstderrfd,"w+")) != NULL ) {
if ( !closed ) fclose(stderr);
*stderr = *newstderr;
oldstderrfd = -1;
/* stderr should be back to where it was */
/* EXCEPT that the file descriptor might not be 2 */
}
}
}
main(argc,argv)
int argc; char **argv;
{
if ( argc < 2 ) return 0;
fprintf(stderr,"before freopen: %s, fileno==%d\n",argv[1],fileno(stderr));
if ((oldstderrfd = dup(fileno(stderr))) != -1 ) {
/* Do your code to do the freopen of stderr, etc.) */
if ( freopen(argv[1],"w+",stderr) == NULL )
restore_stderr(1);
}
fprintf(stderr,"after freopen: %s, fileno==%d\n",argv[1],fileno(stderr));
/* Now, when it's time to go back ... */
restore_stderr(0);
fprintf(stderr,"after restoring stderr: %s,
fileno==%d\n",argv[1],fileno(stderr));
}
;
; To UNSUBSCRIBE: Send a mail message to address@hidden
; with "unsubscribe lynx-dev" (without the
; quotation marks) on a line by itself.
;