summaryrefslogtreecommitdiffstats
path: root/usr.bin/gzip/znew
blob: 27bed94274a626f92e17696e1faa6e7fca467ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh -
#
# $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.
#
# $FreeBSD$

# 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 -c $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
OpenPOWER on IntegriCloud