coverity appeasement
[minix.git] / commands / unstack / unstack.sh
blobbb781c624ab8e3d8f0d6158e264e3b1c2142b354
1 #!/bin/sh
3 # Look at /usr/pkg/bin first in case there is an old nm in /usr/bin
4 PATH=/usr/pkg/bin:$PATH:/usr/gnu/bin
6 # Check usage
7 if [ $# -lt 1 ]
8 then echo "Usage: unstack <executable> [0x... [0x... ] ]"
9 echo " datasizes <executable>"
10 exit 1
13 # Check invocation mode
14 case "`basename $0`" in
15 datasizes)
16 mode=data
18 unstack)
19 mode=stack
22 echo "Invoked as $0?"
23 exit 1
25 esac
27 # Get executable name
28 executable=$1
29 shift
31 # gnu nm can be gnm or nm
32 if which gnm >/dev/null 2>&1
33 then GNM=gnm
34 else GNM=nm
37 # Invoke gnu nm or ack nm?
38 if file $executable | grep NSYM >/dev/null 2>&1
39 then NM="$GNM --radix=d"
40 elif file $executable | grep ELF >/dev/null 2>&1
41 then NM="$GNM --radix=d"
42 else NM="acknm -d"
45 # Invoked as unstack?
46 if [ $mode = stack ]
47 then
48 while [ $# -gt 0 ]
49 do dec="`printf %d $1`"
50 $NM -n $executable | grep ' [Tt] [^.]' | awk '
51 { if($1 > '$dec') { printf "%s+0x%x\n", name, '$dec'-offset; exit }
52 name=$3; offset=$1
54 shift
55 done
57 exit 0
60 # Invoked as datasizes?
61 if [ $mode = data ]
62 then
63 $NM -n $executable |
64 grep ' [bBdD] [^.]' | awk '{ if (lastpos) printf "%10ld kB %s\n", ($1-lastpos)/1024, lastname; lastpos=$1; lastname=$3 }' | sort -n
66 exit 0
69 # Can't happen.
70 echo "Impossible invocation."
72 exit 1