* NEWS, configure.ac (AC_INIT):
[debian/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
17 # Copyright (C) 1998, 2002, 2004 Free Software Foundation
18 # Copyright (C) 1993 Jean-loup Gailly
19
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.
24
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.
29
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.
33
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.
39
40 Written by Jean-loup Gailly.'
41
42 usage='Usage: gzexe [OPTION] FILE...
43 Rename each FILE with a compressed version of itself, renaming FILE to FILE~.
44
45   -d             Decompress each FILE instead of compressing it.
46       --help     display this help and exit
47       --version  output version information and exit
48
49 Report bugs to <bug-gzip@gnu.org>.'
50
51
52 PATH="BINDIR:$PATH"
53 if test $# = 0; then
54   echo "$usage"
55   exit 1
56 fi
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 tmp=gz$$
71 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
72
73 set -C
74 echo hi > $tmp || exit
75 if test -z "`(${CPMOD-cpmod} $tmp $tmp) 2>&1`"; then
76   cpmod=${CPMOD-cpmod}
77 fi
78
79 tail=""
80 IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
81 for dir in $PATH; do
82   test -z "$dir" && dir=.
83   if test -f $dir/tail; then
84     tail="$dir/tail"
85     break
86   fi
87 done
88 IFS="$saveifs"
89 if test -z "$tail"; then
90   echo cannot find tail
91   exit 1
92 fi
93 case `echo foo | $tail -n +1 2>/dev/null` in
94 foo) tail="$tail -n";;
95 esac
96
97 for i do
98   if test ! -f "$i" ; then
99     echo ${x}: $i not a file
100     res=1
101     continue
102   fi
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"
106       continue
107     fi
108   fi
109   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
110     echo "${x}: $i has setuid permission, unchanged"
111     continue
112   fi
113   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
114     echo "${x}: $i has setgid permission, unchanged"
115     continue
116   fi
117   case "$i" in
118   */gzip | */tail | */sed | */chmod | */ln | */sleep | */rm)
119         echo "${x}: $i would depend on itself"; continue ;;
120   esac
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
124       writable=1
125     else
126       writable=0
127       chmod u+w $tmp 2>/dev/null
128     fi
129     : >| $tmp
130   fi
131   if test $decomp -eq 0; then
132     (sed 1q $0 &&
133      sed "s|^if tail|if $tail|" <<'EOF' &&
134 skip=26
135 set -C
136 umask=`umask`
137 umask 77
138 if (tempfile --version) >/dev/null 2>&1
139 then gztmp=`tempfile -p gztmp` || exit
140 else gztmp=/tmp/gztmp$$
141 fi
142 if tail +$skip "$0" | "BINDIR"/gzip -cd > "$gztmp"; then
143   umask $umask
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=$?
150   else
151     trap '/bin/rm -f "$gztmp"; exit $res' 0
152     (/bin/sleep 5; /bin/rm -f "$gztmp") 2>/dev/null &
153     "$gztmp" ${1+"$@"}; res=$?
154   fi
155 else
156   echo Cannot decompress $0; exit 1
157 fi; exit $res
158 EOF
159      gzip -cv9 "$i") > $tmp || {
160       /bin/rm -f $tmp
161       echo ${x}: compression not possible for $i, file unchanged.
162       res=1
163       continue
164     }
165
166   else
167     # decompression
168     skip=26
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"`
171     fi
172     if $tail +$skip "$i" | gzip -cd > $tmp; then
173       :
174     else
175       echo ${x}: $i probably not in gzexe format, file unchanged.
176       res=1
177       continue
178     fi
179   fi
180   rm -f "$i~"
181   mv "$i" "$i~" || {
182     echo ${x}: cannot backup $i as $i~
183     rm -f $tmp
184     res=1
185     continue
186   }
187   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
188     echo ${x}: cannot create $i
189     rm -f $tmp
190     res=1
191     continue
192   }
193   rm -f $tmp
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
198   fi
199 done
200 exit $res