Introduction
Introduction Statistics Contact Development Disclaimer Help
_______ __ _______
| | |.---.-..----.| |--..-----..----. | | |.-----..--.--.--..-----.
| || _ || __|| < | -__|| _| | || -__|| | | ||__ --|
|___|___||___._||____||__|__||_____||__| |__|____||_____||________||_____|
on Gopher (inofficial)
Visit Hacker News on the Web
COMMENT PAGE FOR:
Binfmtc – binfmt_misc C scripting interface
codr7 wrote 7 hours 15 min ago:
I did a simple hack for doing the same thing from inside a C program
for my book:
[1]: https://github.com/codr7/hacktical-c/tree/main/dynamic
kazinator wrote 8 hours 5 min ago:
This is doable entirely without a Linux-specific binfmt-misc hack.
[1]: https://rosettacode.org/wiki/Multiline_shebang#C
enriquto wrote 5 hours 20 min ago:
This is a neat hack, but the whole file is not a valid C program.
monocasa wrote 9 hours 1 min ago:
There's also tcc, which has a shebang compatible extension to allow it
to be used by scripts.
rwmj wrote 9 hours 29 min ago:
You can already do this without using binfmt ...
#if 0
gcc "$0" -o "$@".out && exec ./"$@".out
#endif
#include
int main () { printf ("hello, world\n"); return 0; }
Usage:
$ chmod +x print.c
$ ./print.c
hello, world
(Could be better to use a temporary file though.)
There's a similar cute trick for compiled OCaml scripts that we use
with nbdkit:
[1]: https://libguestfs.org/nbdkit-cc-plugin.3.html#Using-this-plug...
suprjami wrote 1 hour 28 min ago:
Is there a way to do this and have the shell remove the temporary
file after exec?
teo_zero wrote 3 hours 52 min ago:
The use of $@ doesn't look right to me.
In the trivial case exposed here where there are no additional
arguments to pass to the .c program, the shell executes
gcc "print.c" -o .out && exec ./.out
and it works "by chance".
In a more complex scenario where print.c expects some parameters, it
won't work. For example,
./print.c a b c
will result in the shell trying to invoke
gcc "print.c" -o "a" "b" "c".out && exec ./"a" "b" "c".out
which makes no sense.
Are you sure you didn't intend $0 instead of $@ ?
rwmj wrote 3 hours 43 min ago:
It's true, that's a mistake!
OTOH we're trying to write self-compiling executable C scripts, so
the safety, correctness and good sense ships sailed a while back.
ckastner wrote 4 hours 43 min ago:
Oh this is neat. Took me a bit.
The shell treats the first line as a comment. It executes the second
line, which eventually exec's the binary so the rest of the file do
not matter to the shell.
And the compiler treats the first line as a preprocessor directive,
so it ignores the second line.
I initially misread/mistook the first line for a shebang.
AlotOfReading wrote 7 hours 24 min ago:
You can also #embed the compiler binary, and execve it to much the
same effect as binfmtc. I explored that trick for an IOCC entry that
was never submitted because it ended up far too readable.
mananaysiempre wrote 8 hours 38 min ago:
Compiler errors won’t cause as many funny consequences with
gcc "$0" -o "$@".out && exec ./"$@".out || exit $? # I'd use
${0%.c} not $@
Love this trick too, but the difference, as far as I understand, is
that it only works with a Bourne(-compatible) shell, whereas shebangs
or binfmt_misc also work with exec().
enriquto wrote 9 hours 34 min ago:
was surprised that "sudo apt install binfmtc" works out of the box on
my box (linux mint) and i can do the magic just as described here
radiospiel wrote 10 hours 8 min ago:
Amazing; when I tried something similar I used a "#!" line pointing to
a C compiler + runner of sorts ( [1] ). [2] is also following that
approach.
What is the benefit of registering an extension via binfmt_misc?
[1]: https://github.com/radiospiel/jit
[2]: https://git.zx2c4.com/cscript/about/
lmz wrote 9 hours 46 min ago:
It's still valid C source code? Is the #! sequence valid C?
JSR_FDED wrote 10 hours 49 min ago:
C is still my first love. You can hold the whole language in your head,
and it’s fast. Yes there are footguns but it’s a libertarian
programming language - you’re responsible for what you build. No hand
holding.
ykonstant wrote 8 hours 7 min ago:
I like that too, but the problem is that C doesn't keep its end of
the deal. No hand holding, but make what you are doing transparent.
It used to be the case back in the 80s, but not anymore. Not with
our optimizing compilers and oodles of UB and spec subtleties and
implicit actions.
elitepleb wrote 11 hours 24 min ago:
similar to
[1]: http://www.iq0.com/duffgram/com.html
Surac wrote 11 hours 39 min ago:
So lovely
zx2c4 wrote 11 hours 43 min ago:
Similar project of mine from a long while ago:
[1]: https://git.zx2c4.com/cscript/about/
<- back to front page
You are viewing proxied material from codevoid.de. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.