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