Stupid C

So, I'm stuck trying to do something clever in C, which feels kind of
like being asked to paint a portrait with ketchup and a spatula -- I'm
sure someone could do a great job with it, but I just can't get over
how awful the tools are. It's only remotely related to Ruby, but I
thought that someone around here may do enough POSIX I/O coding on a
regular basis to help.

Basically, what I'm trying to do is embed a Ruby script (along with
the normal runtime) inside a normal binary. Currently, I have it
appended to the binary itself, and the app opens argv[0], does a dup2
to replace stdin, and then initializes Ruby.

Unfortunately, I get a EINVAL on the dup2 call -- dup2(this_fh,
fileno(stdin)). I'm able to read from the same 'this_fh' file
descriptor earlier in the same code, so I know that it should be fine.
I'm also closing stdin before doing the dup2; otherwise, it appears to
become a no-op, and my binary just behaves like a normal Ruby
interpreter reading from stdin.

Anyway, if people have any suggestions, I'd greatly appreciate it. I
can post slightly longer sections of code if needed, but as I said,
it's all pretty much boilerplate ANSI C file handling.

ยทยทยท

--
Lennon
rcoder.net