HAMMER Utilities: MFC all changes as of 20080924 - through 'hammer cleanup'.
[dragonfly.git] / usr.bin / gzip / gzexe
blob9b7bda6873c6cd13dd4abd577f5cf721b8da2ace
1 #!/bin/sh -
3 # $NetBSD: gzexe,v 1.2 2003/12/28 12:43:43 wiz Exp $
4 # $DragonFly: src/usr.bin/gzip/gzexe,v 1.1 2004/10/26 11:19:31 joerg Exp $
5 # $OpenBSD: gzexe,v 1.3 2003/08/05 18:22:17 deraadt Exp $
7 # Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>
9 # Permission to use, copy, modify, and distribute this software for any
10 # purpose with or without fee is hereby granted, provided that the above
11 # copyright notice and this permission notice appear in all copies.
13 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 # The number of lines plus one in the on-the-fly decompression script
23 lines=19
25 # A simple string to recognize already compressed files
26 magic="# compressed by gzexe"
28 # Write the decompression script to stdout
29 header () {
30 # first section needs variable expansion, second not
31 cat <<- EOF
32 #!/bin/sh -
33 $magic
34 lines=$lines
35 EOF
36 cat <<- 'EOF'
37 prog=`/usr/bin/basename "$0"`
38 tmp=`/usr/bin/mktemp -d /tmp/gzexeXXXXXXXXXX` || {
39 /bin/echo "$prog: cannot create tmp dir"; exit 1
41 trap '/bin/rm -rf "$tmp"' 0
42 if /usr/bin/tail +$lines "$0" |
43 /usr/bin/gzip -dc > "$tmp/$prog" 2> /dev/null; then
44 /bin/chmod u+x "$tmp/$prog"
45 "$tmp/$prog" ${1+"$@"}
46 ret=$?
47 else
48 /bin/echo "$prog: cannot decompress $0"
49 ret=1
51 exit $ret
52 EOF
55 # Test if a file is compressed by checking the magic line
56 compressed () {
57 test "X`sed -n 2p "$1" 2> /dev/null`" = "X$magic"
60 # Decompress a file
61 decompress () {
62 tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || {
63 echo "$prog: cannot create tmp file"
64 return 1
66 if ! cp "$1" "$tmp"; then
67 echo "$prog: cannot copy $1 to $tmp"
68 rm -f "$tmp"
69 return 1
71 if ! tail +$lines "$tmp" | gzip -vdc > "$1"; then
72 echo "$prog: cannot decompress $1"
73 cp "$tmp" "$1"
74 rm -f "$tmp"
75 return 1
79 # Perform some sanity checks on the file
80 check () {
81 if test ! -e "$1"; then
82 echo "$prog: cannot compress non-existing file $1"
83 return 1
86 if test ! -f "$1"; then
87 echo "$prog: cannot compress non-regular file $1"
88 return 1
91 case `basename "$1"` in
92 sh | mktemp | rm | echo | tail | gzip | chmod)
93 echo "$prog: cannot compress $1, I depend on it"
94 return 1
95 esac
97 if test ! -x "$1"; then
98 echo "$prog: cannot compress $1, it is not executable"
99 return 1
102 if test -u "$1" -o -g "$1"; then
103 echo "$prog: cannot compress $1, it has an s bit set"
104 return 1
108 # Compress a file
109 compress () {
110 tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || {
111 echo "$prog: cannot create tmp file"
112 return 1
114 if ! cp "$1" "$tmp"; then
115 echo "$prog: cannot copy $1 to $tmp"
116 rm -f "$tmp"
117 return 1
119 if ! cp "$1" "$1"~; then
120 echo "$prog: cannot create backup copy $1~"
121 rm -f "$1"~ "$tmp"
122 return 1
125 # Use cp to overwrite the existing file preserving mode and owner
126 # if possible. If the file is not writable, this will produce an
127 # error.
129 if header "$1" > "$tmp" && gzip -vc "$1" >> "$tmp"; then
130 if ! cp "$tmp" "$1"; then
131 echo "$prog: cannot copy $tmp to $1"
132 rm -f "$tmp"
133 return 1
135 else
136 echo "$prog: cannot compress $1"
137 rm -f "$1"~ "$tmp"
138 return 1
142 # Is the -d flag specified?
143 dflag=
145 # Return value
146 rc=0
148 if test "X$1" = X-d; then
149 dflag=1
150 shift
153 prog=`basename "$0"`
154 USAGE="usage: $prog [-d] file ..."
155 if test $# -eq 0; then
156 echo $USAGE
157 exit 1
160 while test $# -ne 0; do
161 if test $dflag; then
162 if ! compressed "$1"; then
163 echo "$prog: $1 is not compressed"
164 rc=1;
165 elif ! decompress "$1"; then
166 rc=$?
168 else
169 if compressed "$1"; then
170 echo "$prog: $1 is already compressed"
171 rc=1;
172 elif ! check "$1" || ! compress "$1"; then
173 rc=$?
176 shift
177 done
178 exit $rc