Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.development.apps > #295
| From | Josef Moellers <josef.moellers@ts.fujitsu.com> |
|---|---|
| Newsgroups | comp.os.linux.development.apps |
| Subject | Re: How to dup a FILE* |
| Date | 2011-12-06 10:41 +0100 |
| Organization | Fujitsu Technology Solutions |
| Message-ID | <jbknvn$l8b$1@nntp.ts.fujitsu.com> (permalink) |
| References | <jbkl2v$a75$1@nntp.ts.fujitsu.com> <87k46am55c.fsf@araminta.anjou.terraraq.org.uk> |
Am 6.12.2011 schrub Richard Kettlewell:
> Josef Moellers <josef.moellers@ts.fujitsu.com> writes:
>
>> 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.)
>
> You'd surely be best off doing it solely at the level of file
> descriptors. Synchronize with stdio in the usual way, i.e. ensure that
> there's nothing in the output buffer by doing it before you've written
> anything or fflushing first (and maybe worrying about threads).
>
If I understand correctly: just fiddle with the (integer)
file-descriptors "underneath" the FILE?
Like:
fflush(stderr);
oldfd2 = dup(STDERR_FILENO);
close(STDERR_FILENO);
open("...", O_APPEND); /* Assuming this opens to STDERR_FILENO (+) */
:
:
fflush(stderr);
dup2(oldfd2, STDERR_FILENO);
close(oldfd2);
(+) ... or maybe
if ((fd = open("...", O_APPEND)) != STDERR_FILENO)
{
dup2(fd, STDERR_FILENO);
close(fd);
}
Sounds reasonable ...
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 | Next — Previous in thread | Next in thread | Find similar
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