Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active September 16, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/7bc1a49b90adabed45f5775b15139c5f to your computer and use it in GitHub Desktop.
Save Hermann-SW/7bc1a49b90adabed45f5775b15139c5f to your computer and use it in GitHub Desktop.
C script runs C code without temp space (tcc -run) and script stdin available to C
#!/bin/bash
echo "foo"
tcc -run <(sed -n "/^\/\*\*$/,\$p" $0) 42
exit
/**
*/
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("bar %s\n", argv[1]);
for(int ch=getchar(); EOF!=ch; ch=getchar())
putchar(ch);
return 0;
}
@Hermann-SW
Copy link
Author

Hermann-SW commented Sep 16, 2021

Demos passing "42" as argv[1], and keeping C code stdin the same as C script stdin:

$ route -n | ./run_from_memory_stdin.c 
foo
bar 42
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.29.58.98    0.0.0.0         UG    306    0        0 wlan1
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U     303    0        0 wlan0
172.29.58.96    0.0.0.0         255.255.255.252 U     306    0        0 wlan1
$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment