3 # gzexe: compressor for Unix executables.
4 # Use this only for binaries that you do not use frequently.
6 # The compressed version is a shell script which decompresses itself after
7 # skipping $skip lines of shell commands. We try invoking the compressed
8 # executable with the original name (for programs looking at their name).
9 # We also try to retain the original file permissions on the compressed file.
10 # For safety reasons, gzexe will not create setuid or setgid shell scripts.
12 # WARNING: the first line of this file must be either : or #!/bin/sh
13 # The : is required for some old versions of csh.
14 # On Ultrix, /bin/sh is too buggy, change the first line to: #!/bin/sh5
18 echo compress executables. original file foo is renamed to foo~
19 echo usage: ${x} [-d] files...
20 echo " -d decompress the executables"
25 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
29 test "$x" = "ungzexe" && decomp=1
30 if test "x$1" = "x-d"; then
37 if test -z "`(${CPMOD-cpmod} zfoo1$$ zfoo2$$) 2>&1`"; then
43 IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:"
45 test -z "$dir" && dir=.
46 if test -f $dir/tail; then
52 if test -z "$tail"; then
58 if test ! -f "$i" ; then
59 echo ${x}: $i not a file
63 if test $decomp -eq 0; then
64 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
65 echo "${x}: $i is already gzexe'd"
69 if ls -l "$i" | grep '^...[sS]' > /dev/null; then
70 echo "${x}: $i has setuid permission, unchanged"
73 if ls -l "$i" | grep '^......[sS]' > /dev/null; then
74 echo "${x}: $i has setgid permission, unchanged"
77 case "`basename $i`" in
78 gzip | tail | chmod | ln | sleep | rm)
79 echo "${x}: $i would depend on itself"; continue ;;
81 if test -z "$cpmod"; then
82 cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
83 if test -w $tmp 2>/dev/null; then
87 chmod u+w $tmp 2>/dev/null
90 if test $decomp -eq 0; then
92 sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
97 if tail +$skip $0 | "BINDIR"/gzip -cd > /tmp/gztmp$$; then
99 /bin/chmod 700 /tmp/gztmp$$
100 prog="`echo $0 | /bin/sed 's|^.*/||'`"
101 if /bin/ln /tmp/gztmp$$ "/tmp/$prog" 2>/dev/null; then
102 trap '/bin/rm -f /tmp/gztmp$$ "/tmp/$prog"; exit $res' 0
103 (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$ "/tmp/$prog") 2>/dev/null &
104 /tmp/"$prog" ${1+"$@"}; res=$?
106 trap '/bin/rm -f /tmp/gztmp$$; exit $res' 0
107 (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$) 2>/dev/null &
108 /tmp/gztmp$$ ${1+"$@"}; res=$?
111 echo Cannot decompress $0; exit 1
114 gzip -cv9 "$i" >> $tmp || {
116 echo ${x}: compression not possible for $i, file unchanged.
124 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
125 eval `sed -e 1d -e 2q "$i"`
127 if tail +$skip "$i" | gzip -cd > $tmp; then
130 echo ${x}: $i probably not in gzexe format, file unchanged.
137 echo ${x}: cannot backup $i as $i~
142 mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
143 echo ${x}: cannot create $i
149 if test -n "$cpmod"; then
150 $cpmod "$i~" "$i" 2>/dev/null
151 elif test $writable -eq 0; then
152 chmod u-w $i 2>/dev/null