Initial import from FreeBSD RELENG_4:
[games.git] / gnu / usr.bin / gzip / gzexe
1 #!/bin/sh
2 # gzexe: compressor for Unix executables.
3 # Use this only for binaries that you do not use frequently.
4 #
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.
10
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
14 #
15 # $FreeBSD: src/gnu/usr.bin/gzip/gzexe,v 1.7.2.1 2001/02/18 02:19:22 kris Exp $
16
17 x=`basename $0`
18 if test $# = 0; then
19   echo compress executables. original file foo is renamed to foo~
20   echo usage: ${x} [-d] files...
21   echo   "   -d  decompress the executables"
22   exit 1
23 fi
24
25 tmp=`/usr/bin/mktemp -t gz` || exit 1
26 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
27
28 decomp=0
29 res=0
30 test "$x" = "ungzexe" && decomp=1
31 if test "x$1" = "x-d"; then
32   decomp=1
33   shift
34 fi
35
36 zfoo1=`/usr/bin/mktemp -t zfoo1` || exit 1
37 zfoo2=`/usr/bin/mktemp -t zfoo2` || exit 1
38 echo hi > $zfoo1
39 echo hi > $zfoo2
40 if test -z "`(${CPMOD-cpmod} $zfoo1 $zfoo2) 2>&1`"; then
41   cpmod=${CPMOD-cpmod}
42 fi
43 rm -f $zfoo1 $zfoo2
44
45 tail=""
46 IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
47 for dir in $PATH; do
48   test -z "$dir" && dir=.
49   if test -f $dir/tail; then
50     tail="$dir/tail"
51     break
52   fi
53 done
54 IFS="$saveifs"
55 if test -z "$tail"; then
56   echo cannot find tail
57   exit 1
58 fi
59
60 for i do
61   if test ! -f "$i" ; then
62     echo ${x}: $i not a file
63     res=1
64     continue
65   fi
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"
69       continue
70     fi
71   fi
72   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
73     echo "${x}: $i has setuid permission, unchanged"
74     continue
75   fi
76   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
77     echo "${x}: $i has setgid permission, unchanged"
78     continue
79   fi
80   case "`basename $i`" in
81   sh | gzip | tail | chmod | ln | sleep | rm | mktemp)
82         echo "${x}: $i would depend on itself"; continue ;;
83   esac
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
87       writable=1
88     else
89       writable=0
90       chmod u+w $tmp 2>/dev/null
91     fi
92   fi
93   if test $decomp -eq 0; then
94     sed 1q $0 > $tmp
95     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
96 skip=22
97 gztmp=`/usr/bin/mktemp -t gztmp` || exit 1
98 if tail +$skip $0 | gzip -cd > $gztmp; then
99   chmod 700 $gztmp
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=$?
106   else
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=$?
110   fi
111 else
112   echo Cannot decompress $0
113   rm -f $gztmp
114   exit 1
115 fi; exit $res
116 EOF
117     gzip -cv9 "$i" >> $tmp || {
118       /bin/rm -f $tmp
119       echo ${x}: compression not possible for $i, file unchanged.
120       res=1
121       continue
122     }
123
124   else
125     # decompression
126     skip=18
127     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
128       eval `sed -e 1d -e 2q "$i"`
129     fi
130     if tail +$skip "$i" | gzip -cd > $tmp; then
131       :
132     else
133       echo ${x}: $i probably not in gzexe format, file unchanged.
134       res=1
135       continue
136     fi
137   fi
138   rm -f "$i~"
139   mv "$i" "$i~" || {
140     echo ${x}: cannot backup $i as $i~
141     rm -f $tmp
142     res=1
143     continue
144   }
145   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
146     echo ${x}: cannot create $i
147     rm -f $tmp
148     res=1
149     continue
150   }
151   rm -f $tmp
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
156   fi
157 done
158 exit $res