Improved sztest argument parsing.
[utils.git] / sztest
... / ...
CommitLineData
1#!/bin/sh
2
3usage() {
4 echo "usage: sztest [-h] [-H SYS-HEADER] [-L LOCAL-HEADER] TYPE [CCFLAGS...]"
5}
6
7sheaders=
8lheaders=
9while [ "${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
26done
27if [ $# -lt 1 ]; then
28 usage >&2
29 exit 1
30fi
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
41for header in $sheaders; do
42 echo "#include <$header>" >>"$file.c"
43done
44for header in $lheaders; do
45 echo "#include \"$header\"" >>"$file.c"
46done
47cat >>"$file.c" <<EOF
48int main(int argc, char **argv)
49{
50 printf("%zi\n", sizeof($type));
51 return(0);
52}
53EOF
54
55if ! gcc "$@" -iquote. -g -Wall -o "$file" "$file.c"; then
56 rm -f "$file" "$file.c"
57 exit 1
58fi
59"$file"
60rm "$file" "$file.c"