gzexe revision 1.2
11.1Smrg#!/bin/sh - 21.1Smrg# 31.2Swiz# $NetBSD: gzexe,v 1.2 2003/12/28 12:43:43 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 typeset prog tmp 311.1Smrg # first section needs variable expansion, second not 321.1Smrg cat <<- EOF 331.1Smrg #!/bin/sh - 341.1Smrg $magic 351.1Smrg lines=$lines 361.1Smrg EOF 371.1Smrg cat <<- 'EOF' 381.1Smrg prog=`/usr/bin/basename "$0"` 391.1Smrg tmp=`/usr/bin/mktemp -d /tmp/gzexeXXXXXXXXXX` || { 401.1Smrg /bin/echo "$prog: cannot create tmp dir"; exit 1 411.1Smrg } 421.1Smrg trap '/bin/rm -rf "$tmp"' 0 431.1Smrg if /usr/bin/tail +$lines "$0" | 441.1Smrg /usr/bin/gzip -dc > "$tmp/$prog" 2> /dev/null; then 451.1Smrg /bin/chmod u+x "$tmp/$prog" 461.1Smrg "$tmp/$prog" ${1+"$@"} 471.1Smrg ret=$? 481.1Smrg else 491.1Smrg /bin/echo "$prog: cannot decompress $0" 501.1Smrg ret=1 511.1Smrg fi 521.1Smrg exit $ret 531.1Smrg EOF 541.1Smrg} 551.1Smrg 561.1Smrg# Test if a file is compressed by checking the magic line 571.1Smrgcompressed () { 581.1Smrg test "X`sed -n 2p "$1" 2> /dev/null`" = "X$magic" 591.1Smrg} 601.1Smrg 611.1Smrg# Decompress a file 621.1Smrgdecompress () { 631.1Smrg tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || { 641.1Smrg echo "$prog: cannot create tmp file" 651.1Smrg return 1 661.1Smrg } 671.1Smrg if ! cp "$1" "$tmp"; then 681.1Smrg echo "$prog: cannot copy $1 to $tmp" 691.1Smrg rm -f "$tmp" 701.1Smrg return 1 711.1Smrg fi 721.1Smrg if ! tail +$lines "$tmp" | gzip -vdc > "$1"; then 731.1Smrg echo "$prog: cannot decompress $1" 741.1Smrg cp "$tmp" "$1" 751.1Smrg rm -f "$tmp" 761.1Smrg return 1 771.1Smrg fi 781.1Smrg} 791.1Smrg 801.1Smrg# Perform some sanity checks on the file 811.1Smrgcheck () { 821.1Smrg if test ! -e "$1"; then 831.1Smrg echo "$prog: cannot compress non-existing file $1" 841.1Smrg return 1 851.1Smrg fi 861.1Smrg 871.1Smrg if test ! -f "$1"; then 881.1Smrg echo "$prog: cannot compress non-regular file $1" 891.1Smrg return 1 901.1Smrg fi 911.1Smrg 921.1Smrg case `basename "$1"` in 931.1Smrg sh | mktemp | rm | echo | tail | gzip | chmod) 941.1Smrg echo "$prog: cannot compress $1, I depend on it" 951.1Smrg return 1 961.1Smrg esac 971.1Smrg 981.1Smrg if test ! -x "$1"; then 991.1Smrg echo "$prog: cannot compress $1, it is not executable" 1001.1Smrg return 1 1011.1Smrg fi 1021.1Smrg 1031.1Smrg if test -u "$1" -o -g "$1"; then 1041.1Smrg echo "$prog: cannot compress $1, it has an s bit set" 1051.1Smrg return 1 1061.1Smrg fi 1071.1Smrg} 1081.1Smrg 1091.1Smrg# Compress a file 1101.1Smrgcompress () { 1111.1Smrg tmp=`mktemp /tmp/gzexeXXXXXXXXXX` || { 1121.1Smrg echo "$prog: cannot create tmp file" 1131.1Smrg return 1 1141.1Smrg } 1151.1Smrg if ! cp "$1" "$tmp"; then 1161.1Smrg echo "$prog: cannot copy $1 to $tmp" 1171.1Smrg rm -f "$tmp" 1181.1Smrg return 1 1191.1Smrg fi 1201.1Smrg if ! cp "$1" "$1"~; then 1211.1Smrg echo "$prog: cannot create backup copy $1~" 1221.1Smrg rm -f "$1"~ "$tmp" 1231.1Smrg return 1 1241.1Smrg fi 1251.1Smrg 1261.1Smrg # Use cp to overwrite the existing file preserving mode and owner 1271.1Smrg # if possible. If the file is not writable, this will produce an 1281.1Smrg # error. 1291.1Smrg 1301.1Smrg if header "$1" > "$tmp" && gzip -vc "$1" >> "$tmp"; then 1311.1Smrg if ! cp "$tmp" "$1"; then 1321.1Smrg echo "$prog: cannot copy $tmp to $1" 1331.1Smrg rm -f "$tmp" 1341.1Smrg return 1 1351.1Smrg fi 1361.1Smrg else 1371.1Smrg echo "$prog: cannot compress $1" 1381.1Smrg rm -f "$1"~ "$tmp" 1391.1Smrg return 1 1401.1Smrg fi 1411.1Smrg} 1421.1Smrg 1431.1Smrg# Is the -d flag specified? 1441.1Smrgdflag= 1451.1Smrg 1461.1Smrg# Return value 1471.1Smrgrc=0 1481.1Smrg 1491.1Smrgif test "X$1" = X-d; then 1501.1Smrg dflag=1 1511.1Smrg shift 1521.1Smrgfi 1531.1Smrg 1541.1Smrgprog=`basename "$0"` 1551.1SmrgUSAGE="usage: $prog [-d] file ..." 1561.1Smrgif test $# -eq 0; then 1571.1Smrg echo $USAGE 1581.1Smrg exit 1 1591.1Smrgfi 1601.1Smrg 1611.1Smrgwhile test $# -ne 0; do 1621.1Smrg if test $dflag; then 1631.1Smrg if ! compressed "$1"; then 1641.1Smrg echo "$prog: $1 is not compressed" 1651.1Smrg rc=1; 1661.1Smrg elif ! decompress "$1"; then 1671.1Smrg rc=$? 1681.1Smrg fi 1691.1Smrg else 1701.1Smrg if compressed "$1"; then 1711.1Smrg echo "$prog: $1 is already compressed" 1721.1Smrg rc=1; 1731.1Smrg elif ! check "$1" || ! compress "$1"; then 1741.1Smrg rc=$? 1751.1Smrg fi 1761.1Smrg fi 1771.1Smrg shift 1781.1Smrgdone 1791.1Smrgexit $rc 180