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