3 # gzexe: compressor for Unix executables.
4 # Use this only for binaries that you do not use frequently.
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.
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
17 # Copyright (C) 1998, 2002, 2004 Free Software Foundation
18 # Copyright (C) 1993 Jean-loup Gailly
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 of the License, or
23 # (at your option) any later version.
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.
30 # You should have received a copy of the GNU General Public License along
31 # with this program; if not, write to the Free Software Foundation, Inc.,
32 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 version='gzexe (gzip) @VERSION@
35 Copyright (C) 2006 Free Software Foundation, Inc.
36 This is free software. You may redistribute copies of it under the terms of
37 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
38 There is NO WARRANTY, to the extent permitted by law.
40 Written by Jean-loup Gailly.'
42 usage='Usage: gzexe [OPTION] FILE...
43 Rename each FILE with a compressed version of itself, renaming FILE to FILE~.
45 -d Decompress each FILE instead of compressing it.
46 --help display this help and exit
47 --version output version information and exit
49 Report bugs to <bug-gzip@gnu.org>.'
63 --h*) exec echo "$usage";;
64 --v*) exec echo "$version";;
71 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
74 echo hi > $tmp || exit
75 if test -z "`(${CPMOD-cpmod} $tmp $tmp) 2>&1`"; then
80 IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:"
82 test -z "$dir" && dir=.
83 if test -f $dir/tail; then
89 if test -z "$tail"; then
93 case `echo foo | $tail -n +1 2>/dev/null` in
94 foo) tail="$tail -n";;
98 if test ! -f "$i" ; then
99 echo ${x}: $i not a file
103 if test $decomp -eq 0; then
104 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
105 echo "${x}: $i is already gzexe'd"
109 if ls -l "$i" | grep '^...[sS]' > /dev/null; then
110 echo "${x}: $i has setuid permission, unchanged"
113 if ls -l "$i" | grep '^......[sS]' > /dev/null; then
114 echo "${x}: $i has setgid permission, unchanged"
118 */gzip | */tail | */sed | */chmod | */ln | */sleep | */rm)
119 echo "${x}: $i would depend on itself"; continue ;;
121 if test -z "$cpmod"; then
122 cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
123 if test -w $tmp 2>/dev/null; then
127 chmod u+w $tmp 2>/dev/null
131 if test $decomp -eq 0; then
133 sed "s|^if tail|if $tail|" <<'EOF' &&
138 if (tempfile --version) >/dev/null 2>&1
139 then gztmp=`tempfile -p gztmp` || exit
140 else gztmp=/tmp/gztmp$$
142 if tail +$skip "$0" | "BINDIR"/gzip -cd > "$gztmp"; then
144 /bin/chmod 700 "$gztmp"
145 prog=`echo "$gztmp" | /bin/sed 's|[^/]*$||'; echo $0 | /bin/sed 's|.*/||'`
146 if /bin/ln "$gztmp" "$prog" 2>/dev/null; then
147 trap '/bin/rm -f "$gztmp" "$prog"; exit $res' 0
148 (/bin/sleep 5; /bin/rm -f "$gztmp" "$prog") 2>/dev/null &
149 "$prog" ${1+"$@"}; res=$?
151 trap '/bin/rm -f "$gztmp"; exit $res' 0
152 (/bin/sleep 5; /bin/rm -f "$gztmp") 2>/dev/null &
153 "$gztmp" ${1+"$@"}; res=$?
156 echo Cannot decompress $0; exit 1
159 gzip -cv9 "$i") > $tmp || {
161 echo ${x}: compression not possible for $i, file unchanged.
169 if sed -e 1d -e 2q "$i" | grep "^skip=[0-9][0-9]*$" >/dev/null; then
170 eval `sed -e 1d -e 2q "$i"`
172 if $tail +$skip "$i" | gzip -cd > $tmp; then
175 echo ${x}: $i probably not in gzexe format, file unchanged.
182 echo ${x}: cannot backup $i as $i~
187 mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
188 echo ${x}: cannot create $i
194 if test -n "$cpmod"; then
195 $cpmod "$i~" "$i" 2>/dev/null
196 elif test $writable -eq 0; then
197 chmod u-w $i 2>/dev/null