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, 2004, 2006-2007, 2010-2016 Free Software
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 3 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.
39 version='gzexe (gzip) @VERSION@
40 Copyright (C) 2007, 2011-2016 Free Software Foundation, Inc.
41 This is free software. You may redistribute copies of it under the terms of
42 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
43 There is NO WARRANTY, to the extent permitted by law.
45 Written by Jean-loup Gailly.'
47 usage="Usage: $0 [OPTION] FILE...
48 Rename each FILE with a compressed version of itself, renaming FILE to FILE~.
50 -d Decompress each FILE instead of compressing it.
51 --help display this help and exit
52 --version output version information and exit
54 Report bugs to <bug-gzip@gnu.org>."
61 --h*) exec echo "$usage";;
62 --v*) exec echo "$version";;
68 if test $# -eq 0; then
69 echo >&2 "$0: missing operand
70 Try \`$0 --help' for more information."
76 test -n "$tmp" && rm -f "$tmp"
77 (exit $res); exit $res
87 if test ! -f "$file" || test ! -r "$file"; then
89 echo >&2 "$0: $i is not a readable regular file"
92 if test $decomp -eq 0; then
93 if sed -e 1d -e 2q "$file" | grep "^skip=[0-9][0-9]*$" >/dev/null; then
94 echo >&2 "$0: $i is already gzexe'd"
98 if test -u "$file"; then
99 echo >&2 "$0: $i has setuid permission, unchanged"
102 if test -g "$file"; then
103 echo >&2 "$0: $i has setgid permission, unchanged"
107 */basename | */bash | */cat | */chmod | */cp | \
108 */dirname | */echo | */expr | */gzip | \
109 */ln | */mkdir | */mktemp | */mv | */rm | \
110 */sed | */sh | */sleep | */test | */tail)
111 echo >&2 "$0: $i might depend on itself"; continue;;
114 dir=`dirname "$file"` || dir=$TMPDIR
115 test -d "$dir" && test -w "$dir" && test -x "$dir" || dir=/tmp
116 test -n "$tmp" && rm -f "$tmp"
117 if test -z "$mktemp_status"; then
118 type mktemp >/dev/null 2>&1
121 if test $mktemp_status -eq 0; then
122 tmp=`TMPDIR=$dir mktemp -t gzexeXXXXXX`
125 fi && { cp -p "$file" "$tmp" 2>/dev/null || cp "$file" "$tmp"; } || {
127 echo >&2 "$0: cannot copy $file"
130 if test -w "$tmp"; then
134 chmod u+w "$tmp" || {
136 echo >&2 "$0: cannot chmod $tmp"
140 if test $decomp -eq 0; then
155 test -n "$gztmpdir" && rm -fr "$gztmpdir"
156 (exit $res); exit $res
159 if type mktemp >/dev/null 2>&1; then
160 gztmpdir=`mktemp -dt`
162 gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir
163 fi || { (exit 127); exit 127; }
168 ') mkdir -p "$gztmp" && rm -r "$gztmp";;
169 */*) gztmp=$gztmpdir/`basename "$0"`;;
170 esac || { (exit 127); exit 127; }
172 case `echo X | tail -n +1 2>/dev/null` in
176 if tail $tail_n +$skip <"$0" | gzip -cd > "$gztmp"; then
179 (sleep 5; rm -fr "$gztmpdir") 2>/dev/null &
180 "$gztmp" ${1+"$@"}; res=$?
182 echo >&2 "Cannot decompress $0"
186 gzip -cv9 "$file") > "$tmp" || {
188 echo >&2 "$0: compression not possible for $i, file unchanged."
195 skip_line=`sed -e 1d -e 2q "$file"`
197 skip=[0-9] | skip=[0-9][0-9] | skip=[0-9][0-9][0-9])
200 case `echo X | tail -n +1 2>/dev/null` in
204 tail $tail_n +$skip "$file" | gzip -cd > "$tmp" || {
206 echo >&2 "$0: $i probably not in gzexe format, file unchanged."
210 test $writable -eq 1 || chmod u-w "$tmp" || {
212 echo >&2 "$0: $tmp: cannot chmod"
215 ln -f "$file" "$file~" || {
217 echo >&2 "$0: cannot backup $i as $i~"
220 mv -f "$tmp" "$file" || {
222 echo >&2 "$0: cannot rename $tmp to $i"
227 (exit $res); exit $res