2 # gzexe: compressor for Unix executables.
3 # Use this only for binaries that you do not use frequently.
5 # The compressed version is a shell script which decompresses itself after
6 # skipping $skip lines of shell commands. We try invoking the compressed
7 # executable with the original name (for programs looking at their name).
8 # We also try to retain the original file permissions on the compressed file.
9 # For safety reasons, gzexe will not create setuid or setgid shell scripts.
11 # WARNING: the first line of this file must be either : or #!/bin/sh
12 # The : is required for some old versions of csh.
13 # On Ultrix, /bin/sh is too buggy, change the first line to: #!/bin/sh5
15 # $FreeBSD: src/gnu/usr.bin/gzip/gzexe,v 1.7.2.1 2001/02/18 02:19:22 kris Exp $
19 echo compress executables. original file foo is renamed to foo~
20 echo usage: ${x} [-d] files...
21 echo " -d decompress the executables"
25 tmp=`/usr/bin/mktemp -t gz` || exit 1
26 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
30 test "$x" = "ungzexe" && decomp=1
31 if test "x$1" = "x-d"; then
36 zfoo1=`/usr/bin/mktemp -t zfoo1` || exit 1
37 zfoo2=`/usr/bin/mktemp -t zfoo2` || exit 1
40 if test -z "`(${CPMOD-cpmod} $zfoo1 $zfoo2) 2>&1`"; then
46 IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:"
48 test -z "$dir" && dir=.
49 if test -f $dir/tail; then
55 if test -z "$tail"; then
61 if test ! -f "$i" ; then
62 echo ${x}: $i not a file
66 if test $decomp -eq 0; then
67 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
68 echo "${x}: $i is already gzexe'd"
72 if ls -l "$i" | grep '^...[sS]' > /dev/null; then
73 echo "${x}: $i has setuid permission, unchanged"
76 if ls -l "$i" | grep '^......[sS]' > /dev/null; then
77 echo "${x}: $i has setgid permission, unchanged"
80 case "`basename $i`" in
81 sh | gzip | tail | chmod | ln | sleep | rm | mktemp)
82 echo "${x}: $i would depend on itself"; continue ;;
84 if test -z "$cpmod"; then
85 cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
86 if test -w $tmp 2>/dev/null; then
90 chmod u+w $tmp 2>/dev/null
93 if test $decomp -eq 0; then
95 sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
97 gztmp=`/usr/bin/mktemp -t gztmp` || exit 1
98 if tail +$skip $0 | gzip -cd > $gztmp; then
100 prog="`echo $0 | sed 's|^.*/||'`"
101 progtmp=`/usr/bin/mktemp -t ${prog}` || exit 1
102 if /bin/ln $gztmp $progtmp 2>/dev/null; then
103 trap '/bin/rm -f $gztmp $progtmp; exit $res' 0
104 (/bin/sleep 5; /bin/rm -f $gztmp $progtmp) 2>/dev/null &
105 /tmp/"$prog" ${1+"$@"}; res=$?
107 trap '/bin/rm -f $progtmp $gztmp exit $res' 0
108 (/bin/sleep 5; /bin/rm -f $progtmp $gztmp) 2>/dev/null &
109 $gztmp ${1+"$@"}; res=$?
112 echo Cannot decompress $0
117 gzip -cv9 "$i" >> $tmp || {
119 echo ${x}: compression not possible for $i, file unchanged.
127 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
128 eval `sed -e 1d -e 2q "$i"`
130 if tail +$skip "$i" | gzip -cd > $tmp; then
133 echo ${x}: $i probably not in gzexe format, file unchanged.
140 echo ${x}: cannot backup $i as $i~
145 mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
146 echo ${x}: cannot create $i
152 if test -n "$cpmod"; then
153 $cpmod "$i~" "$i" 2>/dev/null
154 elif test $writable -eq 0; then
155 chmod u-w $i 2>/dev/null