11.1Smrg#!/bin/sh -
21.1Smrg#
31.3Swiz# $NetBSD: gzexe,v 1.3 2004/05/01 08:22:41 wiz Exp $
41.2Swiz#
51.1Smrg# $OpenBSD: gzexe,v 1.3 2003/08/05 18:22:17 deraadt Exp $
61.1Smrg#
71.1Smrg#  Copyright (c) 2003 Otto Moerbeek <otto@drijf.net>
81.1Smrg#
91.1Smrg#  Permission to use, copy, modify, and distribute this software for any
101.1Smrg#  purpose with or without fee is hereby granted, provided that the above
111.1Smrg#  copyright notice and this permission notice appear in all copies.
121.1Smrg#
131.1Smrg#  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
141.1Smrg#  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
151.1Smrg#  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
161.1Smrg#  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
171.1Smrg#  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
181.1Smrg#  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
191.1Smrg#  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
201.1Smrg#
211.1Smrg
221.1Smrg# The number of lines plus one in the on-the-fly decompression script
231.1Smrglines=19
241.1Smrg
251.1Smrg# A simple string to recognize already compressed files
261.1Smrgmagic="# compressed by gzexe"
271.1Smrg
281.1Smrg# Write the decompression script to stdout
291.1Smrgheader () {
301.1Smrg	# first section needs variable expansion, second not
311.1Smrg	cat <<- EOF
321.1Smrg		#!/bin/sh -
331.1Smrg		$magic
341.1Smrg		lines=$lines
351.1Smrg	EOF
361.1Smrg	cat <<- 'EOF'
371.1Smrg		prog=`/usr/bin/basename "$0"`
381.1Smrg		tmp=`/usr/bin/mktemp -d /tmp/gzexeXXXXXXXXXX` || {
391.1Smrg			/bin/echo "$prog: cannot create tmp dir"; exit 1
401.1Smrg		}
411.1Smrg		trap '/bin/rm -rf "$tmp"' 0
421.1Smrg		if /usr/bin/tail +$lines "$0" |
431.1Smrg		    /usr/bin/gzip -dc > "$tmp/$prog" 2> /dev/null; then
441.1Smrg			/bin/chmod u+x "$tmp/$prog"
451.1Smrg			"$tmp/$prog" ${1+"$@"}
461.1Smrg			ret=$?
471.1Smrg		else
481.1Smrg			/bin/echo "$prog: cannot decompress $0"
491.1Smrg			ret=1
501.1Smrg		fi
511.1Smrg		exit $ret
521.1Smrg	EOF
531.1Smrg}
541.1Smrg
551.1Smrg# Test if a file is compressed by checking the magic line
561.1Smrgcompressed () {
571.1Smrg	test "X`sed -n 2p "$1" 2> /dev/null`" = "X$magic"
581.1Smrg}
591.1Smrg
601.1Smrg# Decompress a file
611.1Smrgdecompress () {
621.1Smrg	tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || {
631.1Smrg		echo "$prog: cannot create tmp file"
641.1Smrg		return 1
651.1Smrg	}
661.1Smrg	if ! cp "$1" "$tmp"; then
671.1Smrg		echo "$prog: cannot copy $1 to $tmp"
681.1Smrg		rm -f "$tmp"
691.1Smrg		return 1
701.1Smrg	fi
711.1Smrg	if ! tail +$lines "$tmp" | gzip -vdc > "$1"; then
721.1Smrg		echo "$prog: cannot decompress $1"
731.1Smrg		cp "$tmp" "$1"
741.1Smrg		rm -f "$tmp"
751.1Smrg		return 1
761.1Smrg	fi
771.1Smrg}
781.1Smrg
791.1Smrg# Perform some sanity checks on the file
801.1Smrgcheck () {
811.1Smrg	if test ! -e "$1"; then
821.1Smrg		echo "$prog: cannot compress non-existing file $1"
831.1Smrg		return 1
841.1Smrg	fi
851.1Smrg
861.1Smrg	if test ! -f "$1"; then
871.1Smrg		echo "$prog: cannot compress non-regular file $1"
881.1Smrg		return 1
891.1Smrg	fi
901.1Smrg
911.1Smrg	case `basename "$1"` in
921.1Smrg		sh | mktemp | rm | echo | tail | gzip | chmod)
931.1Smrg			echo "$prog: cannot compress $1, I depend on it"
941.1Smrg			return 1
951.1Smrg	esac
961.1Smrg
971.1Smrg	if test ! -x "$1"; then
981.1Smrg		echo "$prog: cannot compress $1, it is not executable"
991.1Smrg		return 1
1001.1Smrg	fi
1011.1Smrg
1021.1Smrg	if test -u "$1" -o -g "$1"; then
1031.1Smrg		echo "$prog: cannot compress $1, it has an s bit set"
1041.1Smrg		return 1
1051.1Smrg	fi
1061.1Smrg}
1071.1Smrg
1081.1Smrg# Compress a file
1091.1Smrgcompress () {
1101.1Smrg	tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || {
1111.1Smrg		echo "$prog: cannot create tmp file"
1121.1Smrg		return 1
1131.1Smrg	}
1141.1Smrg	if ! cp "$1" "$tmp"; then
1151.1Smrg		echo "$prog: cannot copy $1 to $tmp"
1161.1Smrg		rm -f "$tmp"
1171.1Smrg		return 1
1181.1Smrg	fi
1191.1Smrg	if ! cp "$1" "$1"~; then
1201.1Smrg		echo "$prog: cannot create backup copy $1~"
1211.1Smrg		rm -f "$1"~ "$tmp"
1221.1Smrg		return 1
1231.1Smrg	fi
1241.1Smrg
1251.1Smrg	# Use cp to overwrite the existing file preserving mode and owner
1261.1Smrg	# if possible. If the file is not writable, this will produce an
1271.1Smrg	# error.
1281.1Smrg
1291.1Smrg	if header "$1" > "$tmp" && gzip -vc "$1" >> "$tmp"; then
1301.1Smrg		if ! cp "$tmp" "$1"; then
1311.1Smrg			echo "$prog: cannot copy $tmp to $1"
1321.1Smrg			rm -f "$tmp"
1331.1Smrg			return 1
1341.1Smrg		fi
1351.1Smrg	else
1361.1Smrg		echo "$prog: cannot compress $1"
1371.1Smrg		rm -f "$1"~ "$tmp"
1381.1Smrg		return 1
1391.1Smrg	fi
1401.1Smrg}
1411.1Smrg
1421.1Smrg# Is the -d flag specified?
1431.1Smrgdflag=
1441.1Smrg
1451.1Smrg# Return value
1461.1Smrgrc=0
1471.1Smrg
1481.1Smrgif test "X$1" = X-d; then
1491.1Smrg	dflag=1
1501.1Smrg	shift
1511.1Smrgfi
1521.1Smrg
1531.1Smrgprog=`basename "$0"`
1541.1SmrgUSAGE="usage: $prog [-d] file ..."
1551.1Smrgif test $# -eq 0; then
1561.1Smrg	echo $USAGE
1571.1Smrg	exit 1
1581.1Smrgfi
1591.1Smrg
1601.1Smrgwhile test $# -ne 0; do
1611.1Smrg	if test $dflag; then
1621.1Smrg		if ! compressed "$1"; then
1631.1Smrg			echo "$prog: $1 is not compressed"
1641.1Smrg			rc=1;
1651.1Smrg		elif ! decompress "$1"; then
1661.1Smrg			rc=$?
1671.1Smrg		fi
1681.1Smrg	else
1691.1Smrg		if compressed "$1"; then
1701.1Smrg			echo "$prog: $1 is already compressed"
1711.1Smrg			rc=1;
1721.1Smrg		elif ! check "$1" || ! compress "$1"; then
1731.1Smrg			rc=$?
1741.1Smrg		fi
1751.1Smrg	fi
1761.1Smrg	shift
1771.1Smrgdone
1781.1Smrgexit $rc
179