]> sipb.mit.edu Git - snippets/.git/blob - programming/gccrun
55e6b680b23ac29dedf57b08346769f907379d21
[snippets/.git] / programming / gccrun
1 #!/bin/sh
2
3 if [ "$#" = 0 ]; then
4     echo "usage: $0 [-w wrapper] <C code...>" >&2
5     exit 1
6 fi
7
8 if [ "$1" = -w ]; then
9     wrapper="$2"
10     shift 2
11 fi
12
13 f=$(mktemp -d -t gccrun.XXXXXXXX) || exit 1
14 cat > "$f/command.c" << EOF
15 #define _GNU_SOURCE
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19 #include <sys/ptrace.h>
20 #include <sys/syscall.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 int
29 main(int argc, char *argv[], char *envp[])
30 {
31         $@;
32         return 0;
33 }
34 EOF
35 if ! gcc -o "$f/command" "$f/command.c"; then
36     exit 1
37 fi
38 if [ -n "$wrapper" ]; then
39     "$wrapper" "$f/command"
40 else
41     "$f/command"
42 fi
43 r=$?
44 rm -r "$f"
45 exit $r