Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / gnu / usr.bin / gzip / gzexe
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 # $FreeBSD: src/gnu/usr.bin/gzip/gzexe,v 1.7.2.1 2001/02/18 02:19:22 kris Exp $
16 # $DragonFly: src/gnu/usr.bin/gzip/Attic/gzexe,v 1.2 2003/06/17 04:25:46 dillon Exp $
17
18 x=`basename $0`
19 if test $# = 0; then
20   echo compress executables. original file foo is renamed to foo~
21   echo usage: ${x} [-d] files...
22   echo   "   -d  decompress the executables"
23   exit 1
24 fi
25
26 tmp=`/usr/bin/mktemp -t gz` || exit 1
27 trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
28
29 decomp=0
30 res=0
31 test "$x" = "ungzexe" && decomp=1
32 if test "x$1" = "x-d"; then
33   decomp=1
34   shift
35 fi
36
37 zfoo1=`/usr/bin/mktemp -t zfoo1` || exit 1
38 zfoo2=`/usr/bin/mktemp -t zfoo2` || exit 1
39 echo hi > $zfoo1
40 echo hi > $zfoo2
41 if test -z "`(${CPMOD-cpmod} $zfoo1 $zfoo2) 2>&1`"; then
42   cpmod=${CPMOD-cpmod}
43 fi
44 rm -f $zfoo1 $zfoo2
45
46 tail=""
47 IFS="${IFS=     }"; saveifs="$IFS"; IFS="${IFS}:"
48 for dir in $PATH; do
49   test -z "$dir" && dir=.
50   if test -f $dir/tail; then
51     tail="$dir/tail"
52     break
53   fi
54 done
55 IFS="$saveifs"
56 if test -z "$tail"; then
57   echo cannot find tail
58   exit 1
59 fi
60
61 for i do
62   if test ! -f "$i" ; then
63     echo ${x}: $i not a file
64     res=1
65     continue
66   fi
67   if test $decomp -eq 0; then
68     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
69       echo "${x}: $i is already gzexe'd"
70       continue
71     fi
72   fi
73   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
74     echo "${x}: $i has setuid permission, unchanged"
75     continue
76   fi
77   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
78     echo "${x}: $i has setgid permission, unchanged"
79     continue
80   fi
81   case "`basename $i`" in
82   sh | gzip | tail | chmod | ln | sleep | rm | mktemp)
83         echo "${x}: $i would depend on itself"; continue ;;
84   esac
85   if test -z "$cpmod"; then
86     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
87     if test -w $tmp 2>/dev/null; then
88       writable=1
89     else
90       writable=0
91       chmod u+w $tmp 2>/dev/null
92     fi
93   fi
94   if test $decomp -eq 0; then
95     sed 1q $0 > $tmp
96     sed "s|^if tail|if $tail|" >> $tmp <<'EOF'
97 skip=22
98 gztmp=`/usr/bin/mktemp -t gztmp` || exit 1
99 if tail +$skip $0 | gzip -cd > $gztmp; then
100   chmod 700 $gztmp
101   prog="`echo $0 | sed 's|^.*/||'`"
102   progtmp=`/usr/bin/mktemp -t ${prog}` || exit 1
103   if /bin/ln $gztmp $progtmp 2>/dev/null; then
104     trap '/bin/rm -f $gztmp $progtmp; exit $res' 0
105     (/bin/sleep 5; /bin/rm -f $gztmp $progtmp) 2>/dev/null &
106     /tmp/"$prog" ${1+"$@"}; res=$?
107   else
108     trap '/bin/rm -f $progtmp $gztmp exit $res' 0
109     (/bin/sleep 5; /bin/rm -f $progtmp $gztmp) 2>/dev/null &
110     $gztmp ${1+"$@"}; res=$?
111   fi
112 else
113   echo Cannot decompress $0
114   rm -f $gztmp
115   exit 1
116 fi; exit $res
117 EOF
118     gzip -cv9 "$i" >> $tmp || {
119       /bin/rm -f $tmp
120       echo ${x}: compression not possible for $i, file unchanged.
121       res=1
122       continue
123     }
124
125   else
126     # decompression
127     skip=18
128     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
129       eval `sed -e 1d -e 2q "$i"`
130     fi
131     if tail +$skip "$i" | gzip -cd > $tmp; then
132       :
133     else
134       echo ${x}: $i probably not in gzexe format, file unchanged.
135       res=1
136       continue
137     fi
138   fi
139   rm -f "$i~"
140   mv "$i" "$i~" || {
141     echo ${x}: cannot backup $i as $i~
142     rm -f $tmp
143     res=1
144     continue
145   }
146   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
147     echo ${x}: cannot create $i
148     rm -f $tmp
149     res=1
150     continue
151   }
152   rm -f $tmp
153   if test -n "$cpmod"; then
154     $cpmod "$i~" "$i" 2>/dev/null
155   elif test $writable -eq 0; then
156     chmod u-w $i 2>/dev/null
157   fi
158 done
159 exit $res