* NEWS: Describe gzexe changes noted below.
[debian/gzip] / gzexe.in
1 #!/bin/sh
2 # gzexe: compressor for Unix executables.
3 # Use this only for binaries that you do not use frequently.
4 #
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.
10
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
14
15
16 # Copyright (C) 1998, 2002, 2004, 2006 Free Software Foundation
17 # Copyright (C) 1993 Jean-loup Gailly
18
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 of the License, or
22 # (at your option) any later version.
23
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.
28
29 # You should have received a copy of the GNU General Public License along
30 # with this program; if not, write to the Free Software Foundation, Inc.,
31 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32
33 tab='   '
34 nl='
35 '
36 IFS=" $tab$nl"
37
38 version='gzexe (gzip) @VERSION@
39 Copyright (C) 2006 Free Software Foundation, Inc.
40 This is free software.  You may redistribute copies of it under the terms of
41 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
42 There is NO WARRANTY, to the extent permitted by law.
43
44 Written by Jean-loup Gailly.'
45
46 usage='Usage: gzexe [OPTION] FILE...
47 Rename each FILE with a compressed version of itself, renaming FILE to FILE~.
48
49   -d             Decompress each FILE instead of compressing it.
50       --help     display this help and exit
51       --version  output version information and exit
52
53 Report bugs to <bug-gzip@gnu.org>.'
54
55
56 PATH="BINDIR:$PATH"
57
58 decomp=0
59 res=0
60 while :; do
61   case $1 in
62   -d) decomp=1; shift;;
63   --h*) exec echo "$usage";;
64   --v*) exec echo "$version";;
65   --) shift; break;;
66   *) break;;
67   esac
68 done
69
70 if test $# -eq 0; then
71   echo >&2 "$0: missing operand
72 Try \`$0 --help' for more information."
73   exit 1
74 fi
75
76 tmp=
77 trap '
78   res=$?
79   test -n "$tmp" && rm -f "$tmp"
80   (exit $res); exit $res
81 ' 0 1 2 3 5 10 13 15
82
83 for i do
84   case $i in
85   -*) file=./$i;;
86   *)  file=$i;;
87   esac
88   if test ! -f "$file" || test ! -r "$file"; then
89     res=$?
90     echo >&2 "$0: $i is not a readable regular file"
91     continue
92   fi
93   if test $decomp -eq 0; then
94     if sed -e 1d -e 2q "$file" | grep "^skip=[0-9][0-9]*$" >/dev/null; then
95       echo >&2 "$0: $i is already gzexe'd"
96       continue
97     fi
98   fi
99   if test -u "$file"; then
100     echo >&2 "$0: $i has setuid permission, unchanged"
101     continue
102   fi
103   if test -g "$file"; then
104     echo >&2 "$0: $i has setgid permission, unchanged"
105     continue
106   fi
107   case /$file in
108   */basename | */cat | */chmod | */cp | \
109   */dirname | */echo | */expr | */gzip | \
110   */ln | */mkdir | */mktemp | */mv | */rm | */rmdir | \
111   */sed | */sh | */sleep | */test | */tail)
112     echo >&2 "$0: $i would depend on itself"; continue;;
113   esac
114
115   dir=`dirname "$file"` || dir=$TMPDIR
116   test -d "$dir" && test -w "$dir" && test -x "$dir" || dir=/tmp
117   test -n "$tmp" && rm -f "$tmp"
118   tmp=`TMPDIR=$dir mktemp -t gzexeXXXXXX` || tmp=$dir/gzexe$$
119   cp -p "$file" "$tmp" 2>/dev/null || cp "$file" "$tmp" || {
120     res=$?
121     echo >&2 "$0: cannot copy $file"
122     continue
123   }
124   if test -w "$tmp"; then
125     writable=1
126   else
127     writable=0
128     chmod u+w "$tmp" || {
129       res=$?
130       echo >&2 "$0: cannot chmod $tmp"
131       continue
132     }
133   fi
134   if test $decomp -eq 0; then
135     (cat <<'EOF' &&
136 #!/bin/sh
137 skip=43
138
139 tab='   '
140 nl='
141 '
142 IFS=" $tab$nl"
143
144 umask=`umask`
145 umask 77
146
147 gztmpdir=
148 trap '
149   res=$?
150   test -n "$gztmpdir" && rm -fr "$gztmpdir"
151   (exit $res); exit $res
152 ' 0 1 2 3 5 10 13 15
153
154 gztmpdir=`(mktemp -dt) 2>/dev/null` ||
155   { gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir; } ||
156   { (exit 127); exit 127; }
157
158 gztmp=$gztmpdir/$0
159 case $0 in
160 -* | */*'
161 ') mkdir -p "$gztmp" && rmdir "$gztmp";;
162 */*) gztmp=$gztmpdir/`basename "$0"`;;
163 esac || { (exit 127); exit 127; }
164
165 case `echo X | tail -n +1 2>/dev/null` in
166 X) tail_n=-n;;
167 *) tail_n=;;
168 esac
169 if tail $tail_n +$skip <"$0" | gzip -cd > "$gztmp"; then
170   umask $umask
171   chmod 700 "$gztmp"
172   (sleep 5; rm -fr "$gztmpdir") 2>/dev/null &
173   "$gztmp" ${1+"$@"}; res=$?
174 else
175   echo >&2 "Cannot decompress $0"
176   (exit 127); res=127
177 fi; exit $res
178 EOF
179     gzip -cv9 "$file") > "$tmp" || {
180       res=$?
181       echo >&2 "$0: compression not possible for $i, file unchanged."
182       continue
183     }
184
185   else
186     # decompression
187     skip=43
188     skip_line=`sed -e 1d -e 2q "$file"`
189     case $skip_line in
190     skip=[0-9] | skip=[0-9][0-9] | skip=[0-9][0-9][0-9])
191       eval "$skip_line";;
192     esac
193     case `echo X | tail -n +1 2>/dev/null` in
194     X) tail_n=-n;;
195     *) tail_n=;;
196     esac
197     tail $tail_n +$skip "$file" | gzip -cd > "$tmp" || {
198       res=$?
199       echo >&2 "$0: $i probably not in gzexe format, file unchanged."
200       continue
201     }
202   fi
203   test $writable -eq 1 || chmod u-w "$tmp" || {
204     res=$?
205     echo >&2 "$0: $tmp: cannot chmod"
206     continue
207   }
208   ln -f "$file" "$file~" || {
209     res=$?
210     echo >&2 "$0: cannot backup $i as $i~"
211     continue
212   }
213   mv -f "$tmp" "$file" || {
214     res=$?
215     echo >&2 "$0: cannot rename $tmp to $i"
216     continue
217   }
218   tmp=
219 done
220 (exit $res); exit $res