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