Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / gzip / znew
blobd433c792f1a970ce78a5290226f13aa370f8e576
1 #!/bin/ksh -
3 # $NetBSD: znew,v 1.2 2003/12/28 12:43:43 wiz Exp $
5 # $OpenBSD: znew,v 1.2 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 # Return 0 if the first arg file size is smaller than the second, 1 otherwise.
23 smaller () {
24 a=`du -k "$1" | awk '{ print $1 }'`
25 b=`du -k "$2" | awk '{ print $1 }'`
26 test $a -lt $b
29 # Check gzip integrity if the -t flag is specified
30 checkfile () {
31 if test $tflag -eq 1; then
32 gzip -qt < "$1"
36 # Decompress a file and then gzip it
37 process () {
38 prefix="${1%.Z}"
39 filez="$prefix".Z
40 filegz="$prefix".gz
42 if test ! -e "$filez"; then
43 echo "$prog: $filez does not exist"
44 return 1
46 if test ! -f "$filez"; then
47 echo "$prog: $filez is not a regular file"
48 return 1
50 if test -e "$filegz" -a $fflag -eq 0; then
51 echo "$prog: $filegz already exists"
52 return 1
55 tmp=`mktemp /tmp/znewXXXXXXXXXX` || {
56 echo "$prog: cannot create tmp file"
57 return 1
59 trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM
61 # Do the actual work, producing a file "$tmp"
62 if uncompress -f -c < "$filez" | gzip -f $gzipflags > "$tmp"; then
64 if test $kflag -eq 1 && smaller "$filez" "$tmp"; then
65 echo -n "$prog: $filez is smaller than $filegz"
66 echo "; keeping it"
67 rm -f "$tmp"
68 return 0
70 if ! checkfile "$tmp"; then
71 echo "$prog: integrity check of $tmp failed"
72 rm -f "$tmp"
73 return 1;
76 # Try to keep the mode of the original file
77 if ! cp -fp "$filez" "$filegz"; then
78 echo "$prog: warning: could not keep mode of $filez"
80 if ! cp "$tmp" "$filegz" 2> /dev/null; then
81 echo "$prog: warning: could not keep mode of $filez"
82 if ! cp -f "$tmp" "$filegz" 2> /dev/null; then
83 echo "$prog: could not copy $tmp to $filegz"
84 rm -f "$filegz" "$tmp"
85 return 1
88 if ! touch -fr "$filez" "$filegz"; then
89 echo -n "$prog: warning: could not keep timestamp of "
90 echo "$filez"
92 rm -f "$filez" "$tmp"
93 else
94 echo "$prog: failed to process $filez"
95 rm -f "$tmp"
96 return 1
100 prog=`basename "$0"`
101 usage="usage: $prog [-ftv9K] file ..."
103 fflag=0
104 tflag=0
105 kflag=0
106 gzipflags=
108 # -P flag is recognized to maintain compatibility, but ignored. Pipe mode is
109 # always used
110 while getopts :ftv9PK i; do
111 case $i in
112 f) fflag=1;;
113 t) tflag=1;;
114 v) gzipflags="-v $gzipflags";;
115 9) gzipflags="-9 $gzipflags";;
116 P) ;;
117 K) kflag=1;;
118 \?) echo "$usage"; exit 1;;
119 esac
120 done
122 shift OPTIND-1
124 if test $# -eq 0; then
125 echo "$usage"
126 exit 1
129 rc=0
131 while test $# -ne 0; do
132 if ! process "$1"; then
133 rc=$?
135 shift
136 done
137 exit $rc