1176133Syar#!/bin/sh - 2166255Sdelphij# 3180136Sdelphij# $NetBSD: znew,v 1.3 2008/04/27 09:07:13 nakayama Exp $ 4166255Sdelphij# $OpenBSD: znew,v 1.2 2003/08/05 18:22:17 deraadt Exp $ 5166255Sdelphij# 6166255Sdelphij#- 7166255Sdelphij# Copyright (c) 2003 Otto Moerbeek <otto@drijf.net> 8166255Sdelphij# 9166255Sdelphij# Permission to use, copy, modify, and distribute this software for any 10166255Sdelphij# purpose with or without fee is hereby granted, provided that the above 11166255Sdelphij# copyright notice and this permission notice appear in all copies. 12166255Sdelphij# 13166255Sdelphij# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14166255Sdelphij# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15166255Sdelphij# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16166255Sdelphij# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17166255Sdelphij# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18166255Sdelphij# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19166255Sdelphij# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20166255Sdelphij# 21166255Sdelphij# $FreeBSD$ 22166255Sdelphij 23166255Sdelphij# Return 0 if the first arg file size is smaller than the second, 1 otherwise. 24166255Sdelphijsmaller () { 25166255Sdelphij a=`du -k "$1" | awk '{ print $1 }'` 26166255Sdelphij b=`du -k "$2" | awk '{ print $1 }'` 27166255Sdelphij test $a -lt $b 28166255Sdelphij} 29166255Sdelphij 30166255Sdelphij# Check gzip integrity if the -t flag is specified 31166255Sdelphijcheckfile () { 32166255Sdelphij if test $tflag -eq 1; then 33166255Sdelphij gzip -qt < "$1" 34166255Sdelphij fi 35166255Sdelphij} 36166255Sdelphij 37166255Sdelphij# Decompress a file and then gzip it 38166255Sdelphijprocess () { 39166255Sdelphij prefix="${1%.Z}" 40166255Sdelphij filez="$prefix".Z 41166255Sdelphij filegz="$prefix".gz 42166255Sdelphij 43166255Sdelphij if test ! -e "$filez"; then 44166255Sdelphij echo "$prog: $filez does not exist" 45166255Sdelphij return 1 46166255Sdelphij fi 47166255Sdelphij if test ! -f "$filez"; then 48166255Sdelphij echo "$prog: $filez is not a regular file" 49166255Sdelphij return 1 50166255Sdelphij fi 51166255Sdelphij if test -e "$filegz" -a $fflag -eq 0; then 52166255Sdelphij echo "$prog: $filegz already exists" 53166255Sdelphij return 1 54166255Sdelphij fi 55166255Sdelphij 56166255Sdelphij tmp=`mktemp /tmp/znewXXXXXXXXXX` || { 57166255Sdelphij echo "$prog: cannot create tmp file" 58166255Sdelphij return 1 59166255Sdelphij } 60166255Sdelphij trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM 61166255Sdelphij 62166255Sdelphij # Do the actual work, producing a file "$tmp" 63176133Syar if uncompress -f -c < "$filez" | gzip -f -c $gzipflags > "$tmp"; then 64166255Sdelphij if test $kflag -eq 1 && smaller "$filez" "$tmp"; then 65166255Sdelphij echo -n "$prog: $filez is smaller than $filegz" 66166255Sdelphij echo "; keeping it" 67166255Sdelphij rm -f "$tmp" 68166255Sdelphij return 0 69166255Sdelphij fi 70166255Sdelphij if ! checkfile "$tmp"; then 71166255Sdelphij echo "$prog: integrity check of $tmp failed" 72166255Sdelphij rm -f "$tmp" 73166255Sdelphij return 1; 74166255Sdelphij fi 75166255Sdelphij 76166255Sdelphij # Try to keep the mode of the original file 77166255Sdelphij if ! cp -fp "$filez" "$filegz"; then 78166255Sdelphij echo "$prog: warning: could not keep mode of $filez" 79166255Sdelphij fi 80166255Sdelphij if ! cp "$tmp" "$filegz" 2> /dev/null; then 81166255Sdelphij echo "$prog: warning: could not keep mode of $filez" 82166255Sdelphij if ! cp -f "$tmp" "$filegz" 2> /dev/null; then 83166255Sdelphij echo "$prog: could not copy $tmp to $filegz" 84166255Sdelphij rm -f "$filegz" "$tmp" 85166255Sdelphij return 1 86166255Sdelphij fi 87166255Sdelphij fi 88166255Sdelphij if ! touch -fr "$filez" "$filegz"; then 89166255Sdelphij echo -n "$prog: warning: could not keep timestamp of " 90166255Sdelphij echo "$filez" 91166255Sdelphij fi 92166255Sdelphij rm -f "$filez" "$tmp" 93166255Sdelphij else 94166255Sdelphij echo "$prog: failed to process $filez" 95166255Sdelphij rm -f "$tmp" 96166255Sdelphij return 1 97166255Sdelphij fi 98166255Sdelphij} 99166255Sdelphij 100166255Sdelphijprog=`basename "$0"` 101166255Sdelphijusage="usage: $prog [-ftv9K] file ..." 102166255Sdelphij 103166255Sdelphijfflag=0 104166255Sdelphijtflag=0 105166255Sdelphijkflag=0 106166255Sdelphijgzipflags= 107166255Sdelphij 108166255Sdelphij# -P flag is recognized to maintain compatibility, but ignored. Pipe mode is 109166255Sdelphij# always used 110166255Sdelphijwhile getopts :ftv9PK i; do 111166255Sdelphij case $i in 112166255Sdelphij f) fflag=1;; 113166255Sdelphij t) tflag=1;; 114166255Sdelphij v) gzipflags="-v $gzipflags";; 115166255Sdelphij 9) gzipflags="-9 $gzipflags";; 116166255Sdelphij P) ;; 117166255Sdelphij K) kflag=1;; 118166255Sdelphij \?) echo "$usage"; exit 1;; 119166255Sdelphij esac 120166255Sdelphijdone 121166255Sdelphij 122176133Syarshift $((OPTIND - 1)) 123166255Sdelphij 124166255Sdelphijif test $# -eq 0; then 125166255Sdelphij echo "$usage" 126166255Sdelphij exit 1 127166255Sdelphijfi 128166255Sdelphij 129166255Sdelphijrc=0 130166255Sdelphij 131166255Sdelphijwhile test $# -ne 0; do 132166255Sdelphij if ! process "$1"; then 133166255Sdelphij rc=$? 134166255Sdelphij fi 135166255Sdelphij shift 136166255Sdelphijdone 137166255Sdelphijexit $rc 138