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