sztest: Use bash specifically.
[utils.git] / sztest
1 #!/bin/bash
2
3 if [ $# -lt 1 ]; then
4     echo "usage: sztest [-H SYS-HEADER] [-L LOCAL-HEADER] TYPE [CCFLAGS...]" >&2
5     exit 1
6 fi
7
8 sheaders=
9 lheaders=
10 while [ "${1:0:1}" = "-" ]; do
11     opt="$1"
12     shift
13     case "$opt" in
14         "-H")
15             sheaders="$sheaders $1"
16             shift
17             ;;
18         "-L")
19             lheaders="$lheaders $1"
20             shift
21             ;;
22     esac
23 done
24 type="$1"
25 shift
26 file="$(mktemp /tmp/sztestXXXXXX)"
27 cat >"$file.c" <<EOF
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 EOF
34 for header in $sheaders; do
35     echo "#include <$header>" >>"$file.c"
36 done
37 for header in $lheaders; do
38     echo "#include \"$header\"" >>"$file.c"
39 done
40 cat >>"$file.c" <<EOF
41 int main(int argc, char **argv)
42 {
43     printf("%zi\n", sizeof($type));
44     return(0);
45 }
46 EOF
47
48 if ! gcc "$@" -iquote. -g -Wall -o "$file" "$file.c"; then
49     rm -f "$file" "$file.c"
50     exit 1
51 fi
52 "$file"
53 rm "$file" "$file.c"