-
Notifications
You must be signed in to change notification settings - Fork 2
/
sizeof
executable file
·75 lines (59 loc) · 1.23 KB
/
sizeof
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
usage()
{
cat >&2 <<__EOF__
Usage: $(basename $0) < type1 > < type2 > ...
Options:
type -- The name of a C type, e.g. size_t.
__EOF__
exit ${1-1}
}
log()
{
echo "$(basename $0): $1" >&2
}
if [ $# -eq 0 ]; then
usage
fi
if which clang >/dev/null 2>&1; then
CC=clang
else
CC=gcc
fi
trap 'rm -f $CFILE $OFILE; exit 0' SIGHUP SIGINT SIGTERM EXIT
CFILE=$(mktemp -t $(basename $0))
OFILE=$(mktemp -t $(basename $0))
if [ -z "$CFILE" -o -z "$OFILE" ]; then
log "Failed to create temporary file. Exiting."
exit 1
fi
# XXX try to dynamically figure out where the defn/typedef for the type is
# by searching through headers in /usr/include and /usr/local/include
cat >> $CFILE <<__EOF__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
__EOF__
while [ $# -gt 0 ]; do
printf '\tprintf("%s: %%zd\\n", sizeof(%s));\n' "$1" "$1" >> $CFILE
shift
done
cat >> $CFILE <<__EOF__
return (0);
}
__EOF__
if ! $CC -x c -o $OFILE $CFILE; then
log "failed to compile test program $CFILE:"
echo '--'
cat $CFILE | sed 's/^/ /'
echo '--'
exit 1
fi
$OFILE