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