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 $
16 # $DragonFly: src/gnu/usr.bin/gzip/Attic/gzexe,v 1.2 2003/06/17 04:25:46 dillon Exp $
20 echo compress executables. original file foo is renamed to foo~
21 echo usage: ${x} [-d] files...
22 echo " -d decompress the executables"
26 tmp=`/usr/bin/mktemp -t gz` || exit 1
27 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
31 test "$x" = "ungzexe" && decomp=1
32 if test "x$1" = "x-d"; then
37 zfoo1=`/usr/bin/mktemp -t zfoo1` || exit 1
38 zfoo2=`/usr/bin/mktemp -t zfoo2` || exit 1
41 if test -z "`(${CPMOD-cpmod} $zfoo1 $zfoo2) 2>&1`"; then
47 IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:"
49 test -z "$dir" && dir=.
50 if test -f $dir/tail; then
56 if test -z "$tail"; then
62 if test ! -f "$i" ; then
63 echo ${x}: $i not a file
67 if test $decomp -eq 0; then
68 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
69 echo "${x}: $i is already gzexe'd"
73 if ls -l "$i" | grep '^...[sS]' > /dev/null; then
74 echo "${x}: $i has setuid permission, unchanged"
77 if ls -l "$i" | grep '^......[sS]' > /dev/null; then
78 echo "${x}: $i has setgid permission, unchanged"
81 case "`basename $i`" in
82 sh | gzip | tail | chmod | ln | sleep | rm | mktemp)
83 echo "${x}: $i would depend on itself"; continue ;;
85 if test -z "$cpmod"; then
86 cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
87 if test -w $tmp 2>/dev/null; then
91 chmod u+w $tmp 2>/dev/null
94 if test $decomp -eq 0; then
96 sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
98 gztmp=`/usr/bin/mktemp -t gztmp` || exit 1
99 if tail +$skip $0 | gzip -cd > $gztmp; then
101 prog="`echo $0 | sed 's|^.*/||'`"
102 progtmp=`/usr/bin/mktemp -t ${prog}` || exit 1
103 if /bin/ln $gztmp $progtmp 2>/dev/null; then
104 trap '/bin/rm -f $gztmp $progtmp; exit $res' 0
105 (/bin/sleep 5; /bin/rm -f $gztmp $progtmp) 2>/dev/null &
106 /tmp/"$prog" ${1+"$@"}; res=$?
108 trap '/bin/rm -f $progtmp $gztmp exit $res' 0
109 (/bin/sleep 5; /bin/rm -f $progtmp $gztmp) 2>/dev/null &
110 $gztmp ${1+"$@"}; res=$?
113 echo Cannot decompress $0
118 gzip -cv9 "$i" >> $tmp || {
120 echo ${x}: compression not possible for $i, file unchanged.
128 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
129 eval `sed -e 1d -e 2q "$i"`
131 if tail +$skip "$i" | gzip -cd > $tmp; then
134 echo ${x}: $i probably not in gzexe format, file unchanged.
141 echo ${x}: cannot backup $i as $i~
146 mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
147 echo ${x}: cannot create $i
153 if test -n "$cpmod"; then
154 $cpmod "$i~" "$i" 2>/dev/null
155 elif test $writable -eq 0; then
156 chmod u-w $i 2>/dev/null