[BACK]Return to znew CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / gzip

File: [cvs.NetBSD.org] / src / usr.bin / gzip / znew (download)

Revision 1.3, Sun Apr 27 09:07:13 2008 UTC (6 years, 5 months ago) by nakayama
Branch: MAIN
CVS Tags: yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-7-base, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, HEAD
Changes since 1.2: +2 -2 lines

Our gzip does not have "-o" option.
So replace -o with >, then znew can now work again.

#!/bin/ksh -
#
# $NetBSD: znew,v 1.3 2008/04/27 09:07:13 nakayama Exp $
#
# $OpenBSD: znew,v 1.2 2003/08/05 18:22:17 deraadt Exp $
#
# Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# Return 0 if the first arg file size is smaller than the second, 1 otherwise.
smaller () {
	a=`du -k "$1" | awk '{ print $1 }'`
	b=`du -k "$2" | awk '{ print $1 }'`
	test $a -lt $b
}

# Check gzip integrity if the -t flag is specified
checkfile () {
	if test $tflag -eq 1; then
		gzip -qt < "$1"
	fi
}

# Decompress a file and then gzip it
process () {
	prefix="${1%.Z}"
	filez="$prefix".Z
	filegz="$prefix".gz

	if test ! -e "$filez"; then
		echo "$prog: $filez does not exist"
		return 1
	fi
	if test ! -f "$filez"; then
		echo "$prog: $filez is not a regular file"
		return 1
	fi
	if test -e "$filegz" -a $fflag -eq 0; then
		echo "$prog: $filegz already exists"
		return 1
	fi

	tmp=`mktemp /tmp/znewXXXXXXXXXX` || {
		echo "$prog: cannot create tmp file"
		return 1
	}
	trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM

	# Do the actual work, producing a file "$tmp"
	if uncompress -f -c < "$filez" | gzip -f $gzipflags > "$tmp"; then

		if test $kflag -eq 1 && smaller "$filez" "$tmp"; then
			echo -n "$prog: $filez is smaller than $filegz"
			echo "; keeping it"
			rm -f "$tmp"
			return 0
		fi
		if ! checkfile "$tmp"; then
			echo "$prog: integrity check of $tmp failed"
			rm -f "$tmp"
			return 1;
		fi

		# Try to keep the mode of the original file
		if ! cp -fp "$filez" "$filegz"; then
			echo "$prog: warning: could not keep mode of $filez"
		fi
		if  ! cp "$tmp" "$filegz" 2> /dev/null; then
			echo "$prog: warning: could not keep mode of $filez"
			if ! cp -f "$tmp" "$filegz" 2> /dev/null; then
				echo "$prog: could not copy $tmp to $filegz"
				rm -f "$filegz" "$tmp"
				return 1
			fi
		fi
		if ! touch -fr "$filez" "$filegz"; then
			echo -n "$prog: warning: could not keep timestamp of "
			echo "$filez"
		fi
		rm -f "$filez" "$tmp"
	else
		echo "$prog: failed to process $filez"
		rm -f "$tmp"
		return 1
	fi
}

prog=`basename "$0"`
usage="usage: $prog [-ftv9K] file ..."

fflag=0
tflag=0
kflag=0
gzipflags=

# -P flag is recognized to maintain compatibility, but ignored. Pipe mode is
# always used
while getopts :ftv9PK i; do
	case $i in
		f) fflag=1;;
		t) tflag=1;;
		v) gzipflags="-v $gzipflags";;
		9) gzipflags="-9 $gzipflags";;
		P) ;;
		K) kflag=1;;
		\?) echo "$usage"; exit 1;;
	esac
done

shift OPTIND-1

if test $# -eq 0; then
	echo "$usage"
	exit 1
fi

rc=0

while test $# -ne 0; do
	if ! process "$1"; then
		rc=$?
	fi
	shift
done
exit $rc