- Here is "proof" perl (or perlxs) is stuffing stderr into a buffer
#!/usr/bin/perl --
use Inline C => Config =>
BUILD_NOISY => 1,
#~ FORCE_BUILD => 1,
CLEAN_AFTER_BUILD => 0,
; # Inline
use Inline C => <<'__C__';
void test_err()
{
PerlIO_printf( PerlIO_stderr(), "Inline::C PerlIO_printf => PerlIO
+_stderr()(%p) is PerlIO_fileno()(%d)\n", PerlIO_stderr(), PerlIO_file
+no(PerlIO_stderr()) );
fprintf (stderr, "Inline::C fprintf => PerlIO_stderr()(%p) is Perl
+IO_fileno()(%d)\n", PerlIO_stderr(), PerlIO_fileno(PerlIO_stderr()) )
+;
fprintf (stderr, "Inline::C fprintf => stderr(%p) is fileno(%d)\n"
+, stderr, fileno(stderr) );
// above output gets eaten if you flush
// fflush (stderr );
fprintf (stderr, "will somebody please eat my stderr buffer \n");
}
__C__
Main( @ARGV );
print "main is over\n";
exit( 0 );
END { print "perl is over\n"; }
sub Main {
use FileHandle;
STDERR->autoflush(1);
STDOUT->autoflush(1);
use File::Temp qw' tempfile ';
open my $olderr, '>&STDERR'; # save STDERR
my ($fh, $fn) = tempfile();
open STDERR, '>&', $fh;
test_err();
open STDERR, '>&', $olderr; # reset STDERR
close $fh;
open my $e, '<', $fn;
my $err = <$e>;
print "(((($err))))\n";
}
__END__
without fflush
((((Inline::C PerlIO_printf => PerlIO_stderr()(99417c) is PerlIO_filen
+o()(2)
))))
main is over
perl is over
Inline::C fprintf => PerlIO_stderr()(0099417C) is PerlIO_fileno()(2)
Inline::C fprintf => stderr(77C5FCC0) is fileno(2)
will somebody please eat my stderr buffer
with fflush
((((Inline::C PerlIO_printf => PerlIO_stderr()(99417c) is PerlIO_filen
+o()(2)
))))
main is over
perl is over
will somebody please eat my stderr buffer
And doing the same thing WITHOUT perl , code from _dup, _dup2 adapted for mingw-gcc
// crt_dup.c
// This program uses the variable old to save
// the original stdout. It then opens a new file named
// DataFile and forces stdout to refer to it. Finally, it
// restores stdout to its original state.
//
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
int old;
FILE *DataFile;
old = _dup( 1 ); // "old" now refers to "stdout"
// Note: file descriptor 1 == "stdout"
if( old == -1 )
{
perror( "_dup( 1 ) failure" );
exit( 1 );
}
_write( old, "This goes to stdout first\n", 26 );
if( fopen_s( &DataFile, "data", "w" ) != 0 )
{
puts( "Can't open file 'data'\n" );
exit( 1 );
}
// stdout now refers to file "data"
if( -1 == _dup2( _fileno( DataFile ), 1 ) )
{
perror( "Can't _dup2 stdout" );
exit( 1 );
}
puts( "This goes to file 'data'\n" );
system( "echo echo echo echo echo echo echo" );
system( "echo echo this goes to 'data' " );
system( "perl -le print(666)" ); // goes to data
// Flush stdout stream buffer so it goes to correct file
fflush( stdout );
fclose( DataFile );
// Restore original stdout
_dup2( old, 1 );
puts( "This goes to stdout also\n" );
puts( "The file 'data' contains:" );
_flushall();
system( "type data" );
}
//~ EUREKA http://v8.googlecode.com/svn/trunk/src/platform-win32.cc
int fopen_s(FILE** pFile, const char* filename, const char* mode) {
*pFile = fopen(filename, mode);
return *pFile != NULL ? 0 : 1;
}
as you can see it works
This goes to stdout first
This goes to stdout also
The file 'data' contains:
echo echo echo echo echo echo
echo this goes to 'data'
666
This goes to file 'data'
If I knew more about gcc and debuggers, I might try to figure out WHERE perl fudges things , but I don't :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|