Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #293

How to dup a FILE*

From Josef Moellers <josef.moellers@ts.fujitsu.com>
Newsgroups comp.os.linux.development.apps
Subject How to dup a FILE*
Date 2011-12-06 09:51 +0100
Organization Fujitsu Technology Solutions
Message-ID <jbkl2v$a75$1@nntp.ts.fujitsu.com> (permalink)

Show all headers | View raw


Hi,

I need to juggle with stderr: redirecting it from one file to another
and back. To be precise:
I have a main process which spawns off child processes to handle hard
disk drives.
At one point, the main process starts working in the context of one of
the disk drives, so from that point on, I'd like to write all stderr
output to a drive-related file. Then the main process spawns off the
child process. Since the child is drive-specific, I need not concern
with stderr in the child.
However, I'd like to revert the main process back to the original stderr.
Also, since I use other external programs/libraries like flex and bison,
I cannot use a custom function to write to whatever FILE pointer that
may be available.

So, in essence, I'd like to do the FILE* equivalent of

oldfd2 = dup(STDERR_FILENO);
close(STDERR_FILENO);
open("...", O_APPEND);	/* if 0 and 1 are open, this will open() fd 2 */

and later

close(STDERR_FILENO);
dup2(oldfd2, STDERR_FILENO);
close(oldfd2);

I'm pretty confident that

fflush(stderr);
oldfd2 = dup(fileno(stderr));
freopen("...", "a", stderr);

will switch to the new file, but will

fflush(stderr);
fclose(stderr);
dup2(oldfd2, fileno(stderr));	/* this may not work, see below */
fdopen(fileno(stderr), "a");

(Maybe I will have to use "STDERR_FILENO" rather than fileno(stderr)
because when stderr is closed, fileno(stderr) probably be invalid.)

Anyway: does anyone have a bullet-proof way of switching back and forth
between FILE*s?

Thanks,

Josef
???
-- 
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html

Back to comp.os.linux.development.apps | Previous | NextNext in thread | Find similar


Thread

How to dup a FILE* Josef Moellers <josef.moellers@ts.fujitsu.com> - 2011-12-06 09:51 +0100
  Re: How to dup a FILE* Richard Kettlewell <rjk@greenend.org.uk> - 2011-12-06 09:16 +0000
    Re: How to dup a FILE* Josef Moellers <josef.moellers@ts.fujitsu.com> - 2011-12-06 10:41 +0100
  Re: How to dup a FILE* Jasen Betts <jasen@xnet.co.nz> - 2011-12-06 13:28 +0000
    Re: How to dup a FILE* Josef Moellers <josef.moellers@ts.fujitsu.com> - 2011-12-06 16:57 +0100
  Re: How to dup a FILE* David W Noon <dwnoon@spamtrap.ntlworld.com> - 2011-12-06 19:53 +0000
    Re: How to dup a FILE* Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-12-06 20:38 +0000
      Re: How to dup a FILE* Richard Kettlewell <rjk@greenend.org.uk> - 2011-12-06 22:03 +0000
      Re: How to dup a FILE* David W Noon <dwnoon@spamtrap.ntlworld.com> - 2011-12-06 21:46 +0000
        Re: How to dup a FILE* Rainer Weikusat <rweikusat@mssgmbh.com> - 2011-12-07 17:52 +0000

csiph-web