3 ms·
On a related note, how about C source code that you can chmod +x and execute directly, even with execve: https://gist.github.com/jdarpinian/84a28a1ed8a36313a4e0
by modeless 9y ago
On a related note, how about C source code that you can chmod +x and execute directly, even with execve: https://gist.github.com/jdarpinian/84a28a1ed8a36313a4e0cad8b3cd347f https://gist.github.com/jdarpinian/84a28a1ed8a36313a4e0cad8b...
- khc 9y agoit's actually much easier than that: https://github.com/kahing/bin/blob/master/cleancache.c https://github.com/kahing/bin/blob/master/cleancache.c
- modeless 9y agoTrue, but there are a few gotchas with that version. It won't work with execve, it doesn't cache the binary, it won't work if called with "source", it doesn't set argv[0] properly when the binary is called, and a few other things. It is nice and terse though.
- khc 9y agoexecve and binary caching are relatively easy to implement, as is argv[0]. Do people actually care about making "source" work?
- modeless 9y agoexecve compatibility is not easy at all, since it requires a shebang line which isn't valid C. However thanks to emmelaich's // idea I just figured out a way to do it using fewer lines than your #if 0 solution. https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83a7aa4e2 https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83...
- MaxBarraclough 9y agoI can't decide if that's cute, or eye-bleedingly criminal. Neat though, and I haven't seen it before.
- emmelaich 9y agoNice. Mine is short and sweet -- just one line. Dunno whether it is better. I've had to put it in gdocs because HN fouls up the asters. https://docs.google.com/document/d/1sBzur57FeLqzgcoGMz8jwOgMQmt5TxolACgTMyM6qkg/edit?usp=sharing https://docs.google.com/document/d/1sBzur57FeLqzgcoGMz8jwOgM...
- modeless 9y agoHa, I'd seen the 3 line #if 0 version but not this! If you don't care about the fact that execve won't work, then this is pretty great.
- emmelaich 9y ago(execve)... Challenge accepted. :-) Might be just a matter of a shebang at the beginning and an 'exec' before the final $p.
- modeless 9y agoYeah, a shebang is required, and unfortunately it isn't valid C so you can no longer feed the file directly to the compiler. I just figured out how to whittle it down to 2 lines though! Thanks for the // idea. Here it is in both shebang (2 line) and non-shebang (1 line) versions: https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83a7aa4e2 https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83...
- emmelaich 9y agoThanks, good stuff. dolmen/Olivier's followup is good too. I now remember why I used the complicated expr command and make; it's to ensure it works with C and C++. And any language that happens to accept // as a comment and is supported by "make". (note that no actual Makefile is required)
- modeless 9y agoThat is a cool feature! It's a bit annoying to have separate versions for C and C++.
- riking 9y agoWhy go to all that trouble when you can just #!/usr/bin/tcc -run #include <stdio.h> int main() { puts("Hello, World!"); }
- modeless 9y agoBecause most people don't have tcc installed, and the convenience is ruined if you have to install things for this to work. I really like that tcc has a flag for this; GCC and Clang really should copy it.
- ogdoad 9y agoWhen I read the source and started on the comments, I thought: won't be long until someone drops in TCC. But yes, TCC does limit you in this regard a bit.
- v_lisivka 9y agoIt's much simpler: $ cat >test.c //usr/bin/gcc "$0" -o /tmp/out.exe || exit; exec /tmp/out.exe "$@" #include <stdio.h> void main(int argc, char **argv) { printf("Hello, world!"); } ^D $ chmod a+x test.c $ ./test.c Hello, world!
- modeless 9y agoEven shorter, works unmodified with both C and C++, and doesn't recompile unless necessary: //usr/bin/make -s "${0%.*}"&&exec "${0%.*}" "$@";exit