Show HN: Runc – Compile and run C in one command
github.com
github.com
Couple of immediate issues with the repo:
- Sample output in the README should not be prefixed with a "$". That's only for the shell command prompt.
- Use ``` blocks in your README file. It's easier to format multi-line text.
- Don't use $RANDOM, it's not secure. Use mktemp: https://github.com/alcover/runc/blob/73aaecea5a4cd357f7172dd...
- Don't exit with success if the command failed: https://github.com/alcover/runc/blob/73aaecea5a4cd357f7172dd...
- Use Bash strict mode: http://redsymbol.net/articles/unofficial-bash-strict-mode/
You can get saner splitting for that loop over the array by quoting the array expansion:
for name in "${names[@]}"
Shellcheck lints that, by the way.I need a little time to study your 'strict mode' suggestion. I used 'set -e' before but had problems with it.
Would you know how I could detect that input is cat'ed into the script ? ($ cat snippet.c | runc -i). So that I can suppress 'Type your code then CTRL+D'.
Stack Overflow suggests http://stackoverflow.com/questions/10022323/ddg#10022396
Again, it's not the project name just the potential clash at the OS level. Because over the years, I've done it to myself.
I admit I didn't research the name 'runc'.
Should I rename it ? I like this one because it's short and explicit.
As a bonus, it can output "I don't like C code, I love it" in its help/version docs.
People could #!bin/bash it however they want.
I only noticed the clash because "runc" seemed familiar.
Why should I have to?
See also Cling, an interactive C++ interpreter.
//$(which gcc) $0 -o a.out; ./a.out $@; rm a.out; exitDoes not work in the spirit of my script though, since you would still need to add headers and main():
//$(which gcc) $0 -o a.out; ./a.out $@; rm a.out; exit
int i = 10;
printf ("%d\n", i);
./snip.c:3:9: error: expected declaration specifiers or ‘...’ before string constant
printf ("%d\n", i);Also related is the tiny C compiler and its `tcc -run` command.
#!/usr/bin/tcc -run
#include <stdio.h>
int main() {
printf("Hello\n");
return 0;
} tcc -w -run - <<< 'main() { printf("Hello\n"); return 0; }'
However, runc seems more convenient to pass in just the statements.If you want to make `tcc -w -run` etc. more convenient then:
alias trun='tcc -w -run - <<<'
trun 'main() { printf("Hello\n"); return 0; }'
---I can't edit my parent comment now, but it can be improved with e.g.
//usr/bin/tcc -run $0; exit
main() { printf("Hello\n"); return 0; }I know, but the first example in the link was:
$ runc 'printf("%s\n", "Hello!");'
Hello!
So I figured I'd complement your example use with one that was more analogous to that.> I can't edit my parent comment now, but it can be improved with e.g.
To be honest, I liked your original example more. Using that `//` "shebang" depends on what shell it's invoked from, and it wouldn't work when calling exec from other languages. For example, with your last example using `//`:
$ python -c 'import subprocess; subprocess.run(["./hello-tcc-double-slash-shebang.c"])'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3.8/subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: './hello-tcc-double-slash-shebang.c'
while it would work with the `#!` shebang, since that's handled by the kernel.I wrote this for a hackathon, so please excuse the messy code :)
gcc main.c && ./a.out gcc main.c && ./a.out && rm -f a.out !gcc main.c -fsanitize=address -Og && ./a.out