]> git.cworth.org Git - gzip/blob - gzexe.in
Imported Debian patch 1.3.5-10
[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 set -C
46 tmp=gz$$
47 trap "rm -f $tmp; exit 1" HUP INT QUIT TRAP USR1 PIPE TERM
48 : > $tmp || exit 1
49
50 decomp=0
51 res=0
52 test "$x" = "ungzexe" && decomp=1
53 if test "x$1" = "x-d"; then
54   decomp=1
55   shift
56 fi
57
58 echo hi > zfoo1$$ || exit 1
59 echo hi > zfoo2$$ || exit 1
60 if test -z "`(${CPMOD-cpmod} zfoo1$$ zfoo2$$) 2>&1`"; then
61   cpmod=${CPMOD-cpmod}
62 fi
63 rm -f zfoo[12]$$
64
65 tail=""
66 IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
67 for dir in $PATH; do
68   test -z "$dir" && dir=.
69   if test -f $dir/tail; then
70     tail="$dir/tail"
71     break
72   fi
73 done
74 IFS="$saveifs"
75 if test -z "$tail"; then
76   echo cannot find tail
77   exit 1
78 fi
79 case `echo foo | $tail -n +1 2>/dev/null` in
80 foo) tail="$tail -n";;
81 esac
82
83 for i do
84   if test ! -f "$i" ; then
85     echo ${x}: $i not a file
86     res=1
87     continue
88   fi
89   if test $decomp -eq 0; then
90     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
91       echo "${x}: $i is already gzexe'd"
92       continue
93     fi
94   fi
95   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
96     echo "${x}: $i has setuid permission, unchanged"
97     continue
98   fi
99   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
100     echo "${x}: $i has setgid permission, unchanged"
101     continue
102   fi
103   case "`basename $i`" in
104   gzip | tail | sed | chmod | ln | sleep | rm)
105         echo "${x}: $i would depend on itself"; continue ;;
106   esac
107   if test -z "$cpmod"; then
108     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
109     if test -w $tmp 2>/dev/null; then
110       writable=1
111     else
112       writable=0
113       chmod u+w $tmp 2>/dev/null
114     fi
115     : >| $tmp  # truncate the file, ignoring set -C
116   fi
117   if test $decomp -eq 0; then
118     sed 1q $0 >> $tmp
119     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
120 skip=23
121 set -C
122 umask=`umask`
123 umask 77
124 tmpfile=`tempfile -p gztmp -d /tmp` || exit 1
125 if tail +$skip "$0" | /bin/gzip -cd >> $tmpfile; then
126   umask $umask
127   /bin/chmod 700 $tmpfile
128   prog="`echo $0 | /bin/sed 's|^.*/||'`"
129   if /bin/ln $tmpfile "/tmp/$prog" 2>/dev/null; then
130     trap '/bin/rm -f $tmpfile "/tmp/$prog"; exit $res' 0
131     (/bin/sleep 5; /bin/rm -f $tmpfile "/tmp/$prog") 2>/dev/null &
132     /tmp/"$prog" ${1+"$@"}; res=$?
133   else
134     trap '/bin/rm -f $tmpfile; exit $res' 0
135     (/bin/sleep 5; /bin/rm -f $tmpfile) 2>/dev/null &
136     $tmpfile ${1+"$@"}; res=$?
137   fi
138 else
139   echo Cannot decompress $0; exit 1
140 fi; exit $res
141 EOF
142     gzip -cv9 "$i" >> $tmp || {
143       /bin/rm -f $tmp
144       echo ${x}: compression not possible for $i, file unchanged.
145       res=1
146       continue
147     }
148
149   else
150     # decompression
151     skip=23
152     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
153       eval `sed -e 1d -e 2q "$i"`
154     fi
155     if tail +$skip "$i" | gzip -cd > $tmp; then
156       :
157     else
158       echo ${x}: $i probably not in gzexe format, file unchanged.
159       res=1
160       continue
161     fi
162   fi
163   rm -f "$i~"
164   mv "$i" "$i~" || {
165     echo ${x}: cannot backup $i as $i~
166     rm -f $tmp
167     res=1
168     continue
169   }
170   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
171     echo ${x}: cannot create $i
172     rm -f $tmp
173     res=1
174     continue
175   }
176   rm -f $tmp
177   if test -n "$cpmod"; then
178     $cpmod "$i~" "$i" 2>/dev/null
179   elif test $writable -eq 0; then
180     chmod u-w $i 2>/dev/null
181   fi
182 done
183 exit $res