]> git.cworth.org Git - gzip/blob - gzexe.in
Imported Debian patch 1.3.2-3woody4
[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 x=`basename "$0"`
17 if test $# = 0; then
18   echo compress executables. original file foo is renamed to foo~
19   echo usage: ${x} [-d] files...
20   echo   "   -d  decompress the executables"
21   exit 1
22 fi
23
24 decomp=0
25 res=0
26 test "$x" = "ungzexe" && decomp=1
27 if test "x$1" = "x-d"; then
28   decomp=1
29   shift
30 fi
31
32 cpmod=
33 if type ${CPMOD:-cpmod} 2>/dev/null; then
34   cpmod=${CPMOD:-cpmod}
35 fi
36
37 tail=""
38 IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
39 for dir in $PATH; do
40   test -z "$dir" && dir=.
41   if test -f $dir/tail; then
42     tail="$dir/tail"
43     break
44   fi
45 done
46 IFS="$saveifs"
47 if test -z "$tail"; then
48   echo cannot find tail
49   exit 1
50 fi
51
52 for i do
53   if test ! -f "$i" ; then
54     echo ${x}: $i not a file
55     res=1
56     continue
57   fi
58   if test $decomp -eq 0; then
59     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
60       echo "${x}: $i is already gzexe'd"
61       continue
62     fi
63   fi
64   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
65     echo "${x}: $i has setuid permission, unchanged"
66     continue
67   fi
68   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
69     echo "${x}: $i has setgid permission, unchanged"
70     continue
71   fi
72   case "`basename $i`" in
73   bash | chmod | gzip | ln | mktemp | rm | sed | sh | tail)
74         echo "${x}: $i would depend on itself"; continue ;;
75   esac
76
77   tmp=`/bin/mktemp -t gzexe.XXXXXXXXXX` || exit 1
78   trap "rm -f $tmp; exit 1" HUP INT QUIT PIPE TERM
79   trap "rm -f $tmp; exit 0" EXIT
80
81   if test -z "$cpmod"; then
82     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
83     if test -w $tmp 2>/dev/null; then
84       writable=1
85     else
86       writable=0
87       chmod u+w $tmp 2>/dev/null
88     fi
89   fi
90   if test $decomp -eq 0; then
91     sed 1q $0 > $tmp
92     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
93 skip=23
94 set -C
95 umask=`umask`
96 umask 77
97 tmpfile=$(tempfile -p gztmp -d /tmp)
98 if tail +$skip $0 | /bin/gzip -cd >> $tmpfile; then
99   umask $umask
100   /bin/chmod 700 $tmpfile
101   prog="`echo $0 | /bin/sed 's|^.*/||'`"
102   if /bin/ln $tmpfile "/tmp/$prog" 2>/dev/null; then
103     trap '/bin/rm -f $tmpfile "/tmp/$prog"; exit $res' 0
104     (/bin/sleep 5; /bin/rm -f $tmpfile "/tmp/$prog") 2>/dev/null &
105     /tmp/"$prog" ${1+"$@"}; res=$?
106   else
107     trap '/bin/rm -f $tmpfile; exit $res' 0
108     (/bin/sleep 5; /bin/rm -f $tmpfile) 2>/dev/null &
109     $tmpfile ${1+"$@"}; res=$?
110   fi
111 else
112   echo Cannot decompress $0; exit 1
113 fi; exit $res
114 EOF
115     gzip -cv9 "$i" >> $tmp || {
116       /bin/rm -f $tmp
117       echo ${x}: compression not possible for $i, file unchanged.
118       res=1
119       continue
120     }
121
122   else
123     # decompression
124     skip=22
125     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
126       eval `sed -e 1d -e 2q "$i"`
127     fi
128     if tail +$skip "$i" | gzip -cd > $tmp; then
129       :
130     else
131       echo ${x}: $i probably not in gzexe format, file unchanged.
132       rm -f $tmp
133       res=1
134       continue
135     fi
136   fi
137   rm -f "$i~"
138   mv "$i" "$i~" || {
139     echo ${x}: cannot backup $i as $i~
140     rm -f $tmp
141     res=1
142     continue
143   }
144   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
145     echo ${x}: cannot create $i
146     rm -f $tmp
147     res=1
148     continue
149   }
150   rm -f $tmp
151   if test -n "$cpmod"; then
152     $cpmod "$i~" "$i" 2>/dev/null
153   elif test $writable -eq 0; then
154     chmod u-w $i 2>/dev/null
155   fi
156 done
157 exit $res