From: Guillaume Marcais Date: 2005-07-27T08:32:28+09:00 Subject: Re: redirecting stdout in an extension On Wed, 2005-07-27 at 08:15 +0900, Mark Probert wrote: > Hi .. > > Any ideas on how to redirect stdout in an extension and then revert it > back to its default behaviour? freopen() redirects, though I am not > sure of returning to normal programming. > > So, > Just a guess. I don't know how well it will play with the Ruby interpreter. Error checking might also be a good idea: > VALUE rb_dump(VALUE self, VALUE o_name) > { > VALUE rval; /* our result */ > VALUE fname; /* holds the filename */ int old_stdout; > > fname = StringValue(o_name); > > /* redirect stdout */ old_stdout = dup(1); > freopen("a.xml", "w+", stdout); > if (foo_dump(STR2CSTR(fname)) == -1) { > rval = Qnil; > } else { > rval = Qtrue; > } > fclose(stdout); /* bad!!! want to go back to fd 1 */ dup2(old_stdout, 1); close(old_stdout); > return rval; > } > > BTW, I have no way of modifying the call to foo_dump() to take a file > handle. It just splurts out to stdout. > > Many thanks, > Guillaume.