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

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

Revision 1.3, Sat May 1 08:22:41 2004 UTC (10 years, 4 months ago) by wiz
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, 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, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, 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-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-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, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, netbsd-3-base, netbsd-3-1-RELEASE, netbsd-3-1-RC4, netbsd-3-1-RC3, netbsd-3-1-RC2, netbsd-3-1-RC1, netbsd-3-1-1-RELEASE, netbsd-3-1, netbsd-3-0-RELEASE, netbsd-3-0-RC6, netbsd-3-0-RC5, netbsd-3-0-RC4, netbsd-3-0-RC3, netbsd-3-0-RC2, netbsd-3-0-RC1, netbsd-3-0-3-RELEASE, netbsd-3-0-2-RELEASE, netbsd-3-0-1-RELEASE, netbsd-3-0, netbsd-3, mjf-devfs2-base, mjf-devfs2, 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, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, keiichi-mipv6-nbase, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, chap-midi-nbase, chap-midi-base, chap-midi, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, abandoned-netbsd-4-base, abandoned-netbsd-4, HEAD
Changes since 1.2: +1 -2 lines

Remove typeset line, since sh(1) does not support it.
Closes PR 25403 by Jukka Salmi.

#!/bin/sh -
#
# $NetBSD: gzexe,v 1.3 2004/05/01 08:22:41 wiz Exp $
#
# $OpenBSD: gzexe,v 1.3 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.
#

# The number of lines plus one in the on-the-fly decompression script
lines=19

# A simple string to recognize already compressed files
magic="# compressed by gzexe"

# Write the decompression script to stdout
header () {
	# first section needs variable expansion, second not
	cat <<- EOF
		#!/bin/sh -
		$magic
		lines=$lines
	EOF
	cat <<- 'EOF'
		prog=`/usr/bin/basename "$0"`
		tmp=`/usr/bin/mktemp -d /tmp/gzexeXXXXXXXXXX` || {
			/bin/echo "$prog: cannot create tmp dir"; exit 1
		}
		trap '/bin/rm -rf "$tmp"' 0
		if /usr/bin/tail +$lines "$0" |
		    /usr/bin/gzip -dc > "$tmp/$prog" 2> /dev/null; then
			/bin/chmod u+x "$tmp/$prog"
			"$tmp/$prog" ${1+"$@"}
			ret=$?
		else
			/bin/echo "$prog: cannot decompress $0"
			ret=1
		fi
		exit $ret
	EOF
}

# Test if a file is compressed by checking the magic line
compressed () {
	test "X`sed -n 2p "$1" 2> /dev/null`" = "X$magic"
}

# Decompress a file
decompress () {
	tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || {
		echo "$prog: cannot create tmp file"
		return 1
	}
	if ! cp "$1" "$tmp"; then
		echo "$prog: cannot copy $1 to $tmp"
		rm -f "$tmp"
		return 1
	fi
	if ! tail +$lines "$tmp" | gzip -vdc > "$1"; then
		echo "$prog: cannot decompress $1"
		cp "$tmp" "$1"
		rm -f "$tmp"
		return 1
	fi
}

# Perform some sanity checks on the file
check () {
	if test ! -e "$1"; then
		echo "$prog: cannot compress non-existing file $1"
		return 1
	fi

	if test ! -f "$1"; then
		echo "$prog: cannot compress non-regular file $1"
		return 1
	fi

	case `basename "$1"` in
		sh | mktemp | rm | echo | tail | gzip | chmod)
			echo "$prog: cannot compress $1, I depend on it"
			return 1
	esac

	if test ! -x "$1"; then
		echo "$prog: cannot compress $1, it is not executable"
		return 1
	fi

	if test -u "$1" -o -g "$1"; then
		echo "$prog: cannot compress $1, it has an s bit set"
		return 1
	fi
}

# Compress a file
compress () {
	tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || {
		echo "$prog: cannot create tmp file"
		return 1
	}
	if ! cp "$1" "$tmp"; then
		echo "$prog: cannot copy $1 to $tmp"
		rm -f "$tmp"
		return 1
	fi
	if ! cp "$1" "$1"~; then
		echo "$prog: cannot create backup copy $1~"
		rm -f "$1"~ "$tmp"
		return 1
	fi

	# Use cp to overwrite the existing file preserving mode and owner
	# if possible. If the file is not writable, this will produce an
	# error.

	if header "$1" > "$tmp" && gzip -vc "$1" >> "$tmp"; then
		if ! cp "$tmp" "$1"; then
			echo "$prog: cannot copy $tmp to $1"
			rm -f "$tmp"
			return 1
		fi
	else
		echo "$prog: cannot compress $1"
		rm -f "$1"~ "$tmp"
		return 1
	fi
}

# Is the -d flag specified?
dflag=

# Return value
rc=0

if test "X$1" = X-d; then
	dflag=1
	shift
fi

prog=`basename "$0"`
USAGE="usage: $prog [-d] file ..."
if test $# -eq 0; then
	echo $USAGE
	exit 1
fi

while test $# -ne 0; do
	if test $dflag; then
		if ! compressed "$1"; then
			echo "$prog: $1 is not compressed"
			rc=1;
		elif ! decompress "$1"; then
			rc=$?
		fi
	else
		if compressed "$1"; then
			echo "$prog: $1 is already compressed"
			rc=1;
		elif ! check "$1" || ! compress "$1"; then
			rc=$?
		fi
	fi
	shift
done
exit $rc