Commit | Line | Data |
---|---|---|
3013fe88 NW |
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 | ||
caed0dfe | 12 | # WARNING: the first line of this file must be either : or #!/bin/sh |
3013fe88 | 13 | # The : is required for some old versions of csh. |
caed0dfe | 14 | # On Ultrix, /bin/sh is too buggy, change the first line to: #!/bin/sh5 |
3013fe88 NW |
15 | |
16 | x=`basename $0` | |
17 | if test $# = 0; then | |
18 | echo compress executables. original file foo is renamed to foo~ | |
19 | echo usage: ${x} [-d] files... | |
20 | echo " -d decompress the executables" | |
21 | exit 1 | |
22 | fi | |
23 | ||
24 | tmp=gz$$ | |
25 | trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15 | |
26 | ||
27 | decomp=0 | |
28 | res=0 | |
29 | test "$x" = "ungzexe" && decomp=1 | |
30 | if test "x$1" = "x-d"; then | |
31 | decomp=1 | |
32 | shift | |
33 | fi | |
34 | ||
35 | echo hi > zfoo1$$ | |
36 | echo hi > zfoo2$$ | |
37 | if test -z "`(${CPMOD-cpmod} zfoo1$$ zfoo2$$) 2>&1`"; then | |
38 | cpmod=${CPMOD-cpmod} | |
39 | fi | |
40 | rm -f zfoo[12]$$ | |
41 | ||
caed0dfe NW |
42 | tail="" |
43 | IFS="${IFS= }"; saveifs="$IFS"; IFS="${IFS}:" | |
44 | for dir in $PATH; do | |
45 | test -z "$dir" && dir=. | |
46 | if test -f $dir/tail; then | |
47 | tail="$dir/tail" | |
48 | break | |
49 | fi | |
50 | done | |
51 | IFS="$saveifs" | |
52 | if test -z "$tail"; then | |
53 | echo cannot find tail | |
54 | exit 1 | |
55 | fi | |
56 | ||
3013fe88 NW |
57 | for i do |
58 | if test ! -f "$i" ; then | |
59 | echo ${x}: $i not a file | |
60 | res=1 | |
61 | continue | |
62 | fi | |
63 | if test $decomp -eq 0; then | |
64 | if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then | |
65 | echo "${x}: $i is already gzexe'd" | |
66 | continue | |
67 | fi | |
68 | fi | |
69 | if ls -l "$i" | grep '^...[sS]' > /dev/null; then | |
70 | echo "${x}: $i has setuid permission, unchanged" | |
71 | continue | |
72 | fi | |
73 | if ls -l "$i" | grep '^......[sS]' > /dev/null; then | |
74 | echo "${x}: $i has setgid permission, unchanged" | |
75 | continue | |
76 | fi | |
caed0dfe NW |
77 | case "`basename $i`" in |
78 | gzip | tail | chmod | ln | sleep | rm) | |
79 | echo "${x}: $i would depend on itself"; continue ;; | |
80 | esac | |
3013fe88 NW |
81 | if test -z "$cpmod"; then |
82 | cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp | |
83 | if test -w $tmp 2>/dev/null; then | |
84 | writable=1 | |
85 | else | |
86 | writable=0 | |
87 | chmod u+w $tmp 2>/dev/null | |
88 | fi | |
89 | fi | |
90 | if test $decomp -eq 0; then | |
91 | sed 1q $0 > $tmp | |
caed0dfe | 92 | sed "s|^if tail|if $tail|" >> $tmp <<'EOF' |
3013fe88 | 93 | skip=18 |
caed0dfe NW |
94 | if tail +$skip $0 | "BINDIR"/gzip -cd > /tmp/gztmp$$; then |
95 | /bin/chmod 700 /tmp/gztmp$$ | |
96 | prog="`echo $0 | /bin/sed 's|^.*/||`" | |
97 | if /bin/ln /tmp/gztmp$$ "/tmp/$prog" 2>/dev/null; then | |
3013fe88 | 98 | trap '/bin/rm -f /tmp/gztmp$$ "/tmp/$prog"; exit $res' 0 |
caed0dfe | 99 | (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$ "/tmp/$prog") 2>/dev/null & |
3013fe88 NW |
100 | /tmp/"$prog" ${1+"$@"}; res=$? |
101 | else | |
102 | trap '/bin/rm -f /tmp/gztmp$$; exit $res' 0 | |
caed0dfe | 103 | (/bin/sleep 5; /bin/rm -f /tmp/gztmp$$) 2>/dev/null & |
3013fe88 NW |
104 | /tmp/gztmp$$ ${1+"$@"}; res=$? |
105 | fi | |
106 | else | |
107 | echo Cannot decompress $0; exit 1 | |
108 | fi; exit $res | |
109 | EOF | |
caed0dfe | 110 | "BINDIR"/gzip -cv9 "$i" >> $tmp || { |
3013fe88 NW |
111 | /bin/rm -f $tmp |
112 | echo ${x}: compression not possible for $i, file unchanged. | |
113 | res=1 | |
114 | continue | |
115 | } | |
116 | ||
117 | else | |
118 | # decompression | |
119 | skip=18 | |
120 | if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then | |
121 | eval `sed -e 1d -e 2q "$i"` | |
122 | fi | |
caed0dfe | 123 | if tail +$skip "$i" | "BINDIR"/gzip -cd > $tmp; then |
3013fe88 NW |
124 | : |
125 | else | |
126 | echo ${x}: $i probably not in gzexe format, file unchanged. | |
127 | res=1 | |
128 | continue | |
129 | fi | |
130 | fi | |
131 | rm -f "$i~" | |
132 | mv "$i" "$i~" || { | |
133 | echo ${x}: cannot backup $i as $i~ | |
134 | rm -f $tmp | |
135 | res=1 | |
136 | continue | |
137 | } | |
138 | mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || { | |
139 | echo ${x}: cannot create $i | |
140 | rm -f $tmp | |
141 | res=1 | |
142 | continue | |
143 | } | |
144 | rm -f $tmp | |
145 | if test -n "$cpmod"; then | |
146 | $cpmod "$i~" "$i" 2>/dev/null | |
147 | elif test $writable -eq 0; then | |
148 | chmod u-w $i 2>/dev/null | |
149 | fi | |
150 | done | |
151 | exit $res |