Stupid C

Greg,

I'm trying to replace stdin with an open handle the currently-running
binary itself, so that the Ruby interpreter can read the script file
I've appended to the application directly, without an intermediate
file being created. Basically, my code does the following:

···

--
int this_fh = open(argv[0], O_RDONLY);
lseek(this_fh, SCRIPT_OFFSET, SEEK_SET);
fclose(stdin);
dup2(this_fh, fileno(stdin));
--

The idea being tha, after all this, the Ruby interpreter will fire up
and start reading from STDIN, which will be the application binary
itself, from the first character of the embedded Ruby script I've
appended to it.

--
Lennon
rcoder.net

Okay, I'll answer my own question, just in case anyone's curious. This
started working as soon as I changed the following lines:
fclose(stdin);
dup2(this_fh, fileno(stdin));

to these:
close(0);
dup2(this_fh, 0);

Go figure. Now I just have to make in work in Windows.

Fun, fun.

···

--
Lennon
rcoder.net

flclose (stdin) is defined to only close the FILE *stdin, it is an
implementation detail that it happend to have an fd underneath. perhaps try generating a tmpfile and

freopen (tmp, 'r', stdin);

-a

···

On Tue, 24 Aug 2004, Lennon Day-Reynolds wrote:

Okay, I'll answer my own question, just in case anyone's curious. This
started working as soon as I changed the following lines:
fclose(stdin);
dup2(this_fh, fileno(stdin));

to these:
close(0);
dup2(this_fh, 0);

Go figure. Now I just have to make in work in Windows.

Fun, fun.

--
Lennon
rcoder.net

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

I've got it working now, using the integer file ids (0 for stdin, 1
for stdout, etc.) directly. I'm hoping to avoid using a tempfile, as
this wrapper needs to be capable of distributing quite large
(50-150MB) binaries with the Ruby code, and I don't want to copy them
any more times than necessary.

Now I'm working on adding the script's dependencies; my current
prototype does some hackish things with the DATA filehandle and
Marshal, but it seems to be working for trivial examples, at least.

Thanks all,

Lennon
rcoder.net