lsvfs(1): Sync a bit with FreeBSD, as applicable.
[dragonfly.git] / usr.bin / gzip / znew
blob8a820bfcf9f001571be8ed6c09b28aee5809f9b3
1 #!/bin/sh -
3 # $NetBSD: znew,v 1.3 2008/04/27 09:07:13 nakayama Exp $
4 # $OpenBSD: znew,v 1.2 2003/08/05 18:22:17 deraadt Exp $
6 # Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>
8 # Permission to use, copy, modify, and distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
12 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 # Return 0 if the first arg file size is smaller than the second, 1 otherwise.
22 smaller () {
23 a=`du -k "$1" | awk '{ print $1 }'`
24 b=`du -k "$2" | awk '{ print $1 }'`
25 test $a -lt $b
28 # Check gzip integrity if the -t flag is specified
29 checkfile () {
30 if test $tflag -eq 1; then
31 gzip -qt < "$1"
35 # Decompress a file and then gzip it
36 process () {
37 prefix="${1%.Z}"
38 filez="$prefix".Z
39 filegz="$prefix".gz
41 if test ! -e "$filez"; then
42 echo "$prog: $filez does not exist"
43 return 1
45 if test ! -f "$filez"; then
46 echo "$prog: $filez is not a regular file"
47 return 1
49 if test -e "$filegz" -a $fflag -eq 0; then
50 echo "$prog: $filegz already exists"
51 return 1
54 tmp=`mktemp /tmp/znewXXXXXXXXXX` || {
55 echo "$prog: cannot create tmp file"
56 return 1
58 trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM
60 # Do the actual work, producing a file "$tmp"
61 if uncompress -f -c < "$filez" | gzip -f $gzipflags > "$tmp"; then
63 if test $kflag -eq 1 && smaller "$filez" "$tmp"; then
64 echo -n "$prog: $filez is smaller than $filegz"
65 echo "; keeping it"
66 rm -f "$tmp"
67 return 0
69 if ! checkfile "$tmp"; then
70 echo "$prog: integrity check of $tmp failed"
71 rm -f "$tmp"
72 return 1;
75 # Try to keep the mode of the original file
76 if ! cp -fp "$filez" "$filegz"; then
77 echo "$prog: warning: could not keep mode of $filez"
79 if ! cp "$tmp" "$filegz" 2> /dev/null; then
80 echo "$prog: warning: could not keep mode of $filez"
81 if ! cp -f "$tmp" "$filegz" 2> /dev/null; then
82 echo "$prog: could not copy $tmp to $filegz"
83 rm -f "$filegz" "$tmp"
84 return 1
87 if ! touch -fr "$filez" "$filegz"; then
88 echo -n "$prog: warning: could not keep timestamp of "
89 echo "$filez"
91 rm -f "$filez" "$tmp"
92 else
93 echo "$prog: failed to process $filez"
94 rm -f "$tmp"
95 return 1
99 prog=`basename "$0"`
100 usage="usage: $prog [-9fKtv] file ..."
102 fflag=0
103 tflag=0
104 kflag=0
105 gzipflags=
107 # -P flag is recognized to maintain compatibility, but ignored. Pipe mode is
108 # always used
109 while getopts :ftv9PK i; do
110 case $i in
111 f) fflag=1;;
112 t) tflag=1;;
113 v) gzipflags="-v $gzipflags";;
114 9) gzipflags="-9 $gzipflags";;
115 P) ;;
116 K) kflag=1;;
117 \?) echo "$usage"; exit 1;;
118 esac
119 done
121 shift $((OPTIND - 1))
123 if test $# -eq 0; then
124 echo "$usage"
125 exit 1
128 rc=0
130 while test $# -ne 0; do
131 if ! process "$1"; then
132 rc=$?
134 shift
135 done
136 exit $rc