top | item 23612451

Show HN: Runc – Compile and run C in one command

41 points| alcover | 5 years ago |github.com | reply

47 comments

order
[+] koolba|5 years ago|reply
This looks fun. I can't imagine it being directly useful but the idea itself is fun.

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/

[+] tomjakubowski|5 years ago|reply
re: Bash "strict mode", the shell options are fine but resetting IFS blows the weirdness budget for a default.

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.
[+] alcover|5 years ago|reply
Done, done and done. Thank you !

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'.

[+] brudgers|5 years ago|reply
This is not a complaint about the name. Just that as a command from the command line, "runc" may clash with the runc container running utility due to potential overlap in the likely users.

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.

https://github.com/opencontainers/runc

[+] alcover|5 years ago|reply
Oh. Especially if one puts my script into ~/bin and calls '$ runc ..'

I admit I didn't research the name 'runc'.

Should I rename it ? I like this one because it's short and explicit.

[+] skolskoly|5 years ago|reply
Quick and ugly alternative is chmod+x and add something like this to the top of your file:

  //$(which gcc) $0 -o a.out; ./a.out $@; rm a.out; exit
[+] alcover|5 years ago|reply
Clever polyglot file!

Does 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);
[+] solvorn|5 years ago|reply
This. My first thought was there is a UNIXy way to do this already.
[+] fish45|5 years ago|reply
This is definitely helpful for testing.

Also related is the tiny C compiler and its `tcc -run` command.

[+] jonathonf|5 years ago|reply
I was just about to reply about the same thing:

  #!/usr/bin/tcc -run
  
  #include <stdio.h>
  
  int main() {
    printf("Hello\n");
    return 0;
  }
[+] crazypython|5 years ago|reply
D has the `rdmd` command for running D code in one command.
[+] gitgud|5 years ago|reply
Reminds me of [1] "c" a utility which allows you to execute C code as if it were a script. It's a shame the name is almost unsearchable in Google though...

[1] https://github.com/ryanmjacobs/c

[+] glouwbug|5 years ago|reply
why not just do

    gcc main.c && ./a.out
[+] smabie|5 years ago|reply
That doesn't include the headers. It also leaves a.out on your system.
[+] verroq|5 years ago|reply
Why is this submission submitted a day ago showing up as new again? @dang