Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@Hermann-SW 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