xref: /freebsd/usr.bin/gzip/znew (revision 2008043f386721d58158e37e0d7e50df8095942d)
1#!/bin/sh -
2#
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 $
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
22# Return 0 if the first arg file size is smaller than the second, 1 otherwise.
23smaller () {
24	a=`du -k "$1" | awk '{ print $1 }'`
25	b=`du -k "$2" | awk '{ print $1 }'`
26	test $a -lt $b
27}
28
29# Check gzip integrity if the -t flag is specified
30checkfile () {
31	if test $tflag -eq 1; then
32		gzip -qt < "$1"
33	fi
34}
35
36# Decompress a file and then gzip it
37process () {
38	prefix="${1%.Z}"
39	filez="$prefix".Z
40	filegz="$prefix".gz
41
42	if test ! -e "$filez"; then
43		echo "$prog: $filez does not exist"
44		return 1
45	fi
46	if test ! -f "$filez"; then
47		echo "$prog: $filez is not a regular file"
48		return 1
49	fi
50	if test -e "$filegz" -a $fflag -eq 0; then
51		echo "$prog: $filegz already exists"
52		return 1
53	fi
54
55	tmp=`mktemp /tmp/znewXXXXXXXXXX` || {
56		echo "$prog: cannot create tmp file"
57		return 1
58	}
59	trap 'rm -f "$tmp"; exit 1' HUP INT QUIT PIPE TERM
60
61	# Do the actual work, producing a file "$tmp"
62	if uncompress -f -c < "$filez" | gzip -f -c $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
68		fi
69		if ! checkfile "$tmp"; then
70			echo "$prog: integrity check of $tmp failed"
71			rm -f "$tmp"
72			return 1;
73		fi
74
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"
78		fi
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
85			fi
86		fi
87		if ! touch -fr "$filez" "$filegz"; then
88			echo -n "$prog: warning: could not keep timestamp of "
89			echo "$filez"
90		fi
91		rm -f "$filez" "$tmp"
92	else
93		echo "$prog: failed to process $filez"
94		rm -f "$tmp"
95		return 1
96	fi
97}
98
99prog=`basename "$0"`
100usage="usage: $prog [-ftv9K] file ..."
101
102fflag=0
103tflag=0
104kflag=0
105gzipflags=
106
107# -P flag is recognized to maintain compatibility, but ignored. Pipe mode is
108# always used
109while 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
119done
120
121shift $((OPTIND - 1))
122
123if test $# -eq 0; then
124	echo "$usage"
125	exit 1
126fi
127
128rc=0
129
130while test $# -ne 0; do
131	if ! process "$1"; then
132		rc=$?
133	fi
134	shift
135done
136exit $rc
137