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