Imported Upstream version 1.3.5
[debian/gzip] / gzexe.in
1 :
2 #!/bin/sh
3 # gzexe: compressor for Unix executables.
4 # Use this only for binaries that you do not use frequently.
5 #
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.
11
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
15
16
17 # Copyright (C) 1998, 2002 Free Software Foundation
18 # Copyright (C) 1993 Jean-loup Gailly
19
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2, or (at your option)
23 # any later version.
24
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write to the Free Software
32 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 # 02111-1307, USA.
34
35
36 PATH="BINDIR:$PATH"
37 x=`basename $0`
38 if test $# = 0; then
39   echo compress executables. original file foo is renamed to foo~
40   echo usage: ${x} [-d] files...
41   echo   "   -d  decompress the executables"
42   exit 1
43 fi
44
45 tmp=gz$$
46 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
47
48 decomp=0
49 res=0
50 test "$x" = "ungzexe" && decomp=1
51 if test "x$1" = "x-d"; then
52   decomp=1
53   shift
54 fi
55
56 echo hi > zfoo1$$
57 echo hi > zfoo2$$
58 if test -z "`(${CPMOD-cpmod} zfoo1$$ zfoo2$$) 2>&1`"; then
59   cpmod=${CPMOD-cpmod}
60 fi
61 rm -f zfoo[12]$$
62
63 tail=""
64 IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
65 for dir in $PATH; do
66   test -z "$dir" && dir=.
67   if test -f $dir/tail; then
68     tail="$dir/tail"
69     break
70   fi
71 done
72 IFS="$saveifs"
73 if test -z "$tail"; then
74   echo cannot find tail
75   exit 1
76 fi
77 case `echo foo | $tail -n +1 2>/dev/null` in
78 foo) tail="$tail -n";;
79 esac
80
81 for i do
82   if test ! -f "$i" ; then
83     echo ${x}: $i not a file
84     res=1
85     continue
86   fi
87   if test $decomp -eq 0; then
88     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
89       echo "${x}: $i is already gzexe'd"
90       continue
91     fi
92   fi
93   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
94     echo "${x}: $i has setuid permission, unchanged"
95     continue
96   fi
97   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
98     echo "${x}: $i has setgid permission, unchanged"
99     continue
100   fi
101   case "`basename $i`" in
102   gzip | tail | sed | chmod | ln | sleep | rm)
103         echo "${x}: $i would depend on itself"; continue ;;
104   esac
105   if test -z "$cpmod"; then
106     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
107     if test -w $tmp 2>/dev/null; then
108       writable=1
109     else
110       writable=0
111       chmod u+w $tmp 2>/dev/null
112     fi
113   fi
114   if test $decomp -eq 0; then
115     sed 1q $0 > $tmp
116     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
117 skip=22
118 set -C
119 umask=`umask`
120 umask 77
121 if tail +$skip "$0" | "BINDIR"/gzip -cd > /tmp/gztmp$$; then
122   umask $umask
123   /bin/chmod 700 /tmp/gztmp$$
124   prog="`echo $0 | /bin/sed 's|^.*/||'`"
125   if /bin/ln /tmp/gztmp$$ "/tmp/$prog" 2>/dev/null; then
126     trap '/bin/rm -f /tmp/gztmp$$ "/tmp/$prog"; exit $res' 0
127     (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$ "/tmp/$prog") 2>/dev/null &
128     /tmp/"$prog" ${1+"$@"}; res=$?
129   else
130     trap '/bin/rm -f /tmp/gztmp$$; exit $res' 0
131     (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$) 2>/dev/null &
132     /tmp/gztmp$$ ${1+"$@"}; res=$?
133   fi
134 else
135   echo Cannot decompress $0; exit 1
136 fi; exit $res
137 EOF
138     gzip -cv9 "$i" >> $tmp || {
139       /bin/rm -f $tmp
140       echo ${x}: compression not possible for $i, file unchanged.
141       res=1
142       continue
143     }
144
145   else
146     # decompression
147     skip=22
148     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9][0-9]*$" >/dev/null; then
149       eval `sed -e 1d -e 2q "$i"`
150     fi
151     if tail +$skip "$i" | gzip -cd > $tmp; then
152       :
153     else
154       echo ${x}: $i probably not in gzexe format, file unchanged.
155       res=1
156       continue
157     fi
158   fi
159   rm -f "$i~"
160   mv "$i" "$i~" || {
161     echo ${x}: cannot backup $i as $i~
162     rm -f $tmp
163     res=1
164     continue
165   }
166   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
167     echo ${x}: cannot create $i
168     rm -f $tmp
169     res=1
170     continue
171   }
172   rm -f $tmp
173   if test -n "$cpmod"; then
174     $cpmod "$i~" "$i" 2>/dev/null
175   elif test $writable -eq 0; then
176     chmod u-w $i 2>/dev/null
177   fi
178 done
179 exit $res