import openbsd z* scripts and manuals.

This commit is contained in:
mrg 2003-12-28 12:31:55 +00:00
parent a92ed9fb69
commit 5ddb436023
10 changed files with 933 additions and 0 deletions

177
usr.bin/gzip/gzexe Executable file
View File

@ -0,0 +1,177 @@
#!/bin/sh -
#
# $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 () {
typeset prog tmp
# 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

71
usr.bin/gzip/gzexe.1 Normal file
View File

@ -0,0 +1,71 @@
.\" $OpenBSD: gzexe.1,v 1.1 2003/07/31 07:32:47 otto 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.
.\"
.Dd July 30, 2003
.Dt GZEXE 1
.Os
.Sh NAME
.Nm gzexe
.Nd create auto-decompressing executables
.Sh SYNOPSIS
.Nm gzexe
.Op Fl d
.Ar
.Sh DESCRIPTION
The
.Nm
utility uses
.Xr gzip 1
to compress executables, producing executables that decompress on-the-fly
when executed.
This saves disk space, at the cost of slower execution times.
The original executables are saved by copying each of them to a file with
the same name with a
.Sq ~
suffix appended.
After verifying that the compressed executables work as expected, the backup
files can be removed.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl d
Decompress executables previously compressed by
.Nm gzexe .
.El
.Pp
The
.Nm
program refuses to compress non-regular or non-executable files,
files with a setuid or setgid bit set, files that are already
compressed using
.Nm
or programs it needs to perform on-the-fly decompression:
.Xr sh 1 ,
.Xr mktemp 1 ,
.Xr rm 1 ,
.Xr echo 1 ,
.Xr tail 1 ,
.Xr gzip 1
and
.Xr chmod 1 .
.Sh SEE ALSO
.Xr gzip 1
.Sh CAVEATS
The
.Nm
utility replaces files by overwriting them with the generated
compressed executable.
To be able to do this, it is required that the original files are writable.

108
usr.bin/gzip/zdiff Executable file
View File

@ -0,0 +1,108 @@
#!/bin/sh -
#
# $OpenBSD: zdiff,v 1.2 2003/07/29 07:42:44 otto Exp $
#
# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
#
# 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.
#
# Sponsored in part by the Defense Advanced Research Projects
# Agency (DARPA) and Air Force Research Laboratory, Air Force
# Materiel Command, USAF, under agreement number F39502-99-1-0512.
#
# Set $prog based on $0
case $0 in
*cmp) prog=cmp
;;
*) prog=diff
;;
esac
USAGE="usage: z$prog [options] file1 [file2]"
# Pull out any command line flags so we can pass them to diff/cmp
# XXX - assumes there is no optarg
flags=
while test $# -ne 0; do
case "$1" in
--)
shift
break
;;
-*)
flags="$flags $1"
shift
;;
*)
break
;;
esac
done
if [ $# -eq 1 ]; then
# One file given, compare compressed to uncompressed
files="$1"
case "$1" in
*[._-][Zz])
files="${1%??}"
;;
*[._-]gz)
files="${1%???}"
;;
*.t[ag]z)
files="${1%??}"ar
;;
*) echo "z$prog: unknown suffix" 1>&2
exit 1
esac
compress -cdfq "$1" | $prog $flags - "$files"
status=$?
elif [ $# -eq 2 ]; then
# Two files given, compare the two uncompressing as needed
case "$1" in
*[._-][Zz]|*[._-]gz|*.t[ag]z)
files=-
filt="compress -cdfq $1"
;;
*)
files="$1"
;;
esac
case "$2" in
*[._-][Zz]|*[._-]gz|*.t[ag]z)
if [ "$files" = "-" ]; then
tmp=`mktemp -t z$prog.XXXXXXXXXX` || exit 1
trap "rm -f $tmp" 0 1 2 3 13 15
compress -cdfq "$2" > $tmp
files="$files $tmp"
else
files="$files -"
filt="compress -cdfq $2"
fi
;;
*)
files="$files $2"
;;
esac
if [ -n "$filt" ]; then
$filt | $prog $flags $files
else
$prog $flags $files
fi
status=$?
else
echo "$USAGE" 1>&2
exit 1
fi
exit $status

106
usr.bin/gzip/zdiff.1 Normal file
View File

@ -0,0 +1,106 @@
.\" $OpenBSD: zdiff.1,v 1.2 2003/07/13 17:39:14 millert Exp $
.\"
.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
.\" 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.
.\"
.\" Sponsored in part by the Defense Advanced Research Projects
.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
.\"
.Dd June 23, 2003
.Dt ZDIFF 1
.Os
.Sh NAME
.Nm zcmp ,
.Nm zdiff
.Nd compare compressed files
.Sh SYNOPSIS
.Nm zcmp
.Op Ar options
.Ar file
.Op Ar file2
.Nm zdiff
.Op Ar options
.Ar file
.Op Ar file2
.Sh DESCRIPTION
.Nm zcmp
and
.Nm zdiff
are filters that invoke
.Xr cmp 1
or
.Xr diff 1
respectively to compare compressed files.
Such files generally have a
.Dq Z
or
.Dq gz
extension (both the
.Xr compress 1
and
.Xr gzip 1
formats are supported).
Any
.Ar options
that are specified are passed to
.Xr cmp 1
or
.Xr diff 1 .
.Pp
If only
.Ar file1
is specified, it is compared against a file with the same name, but
with the extension removed.
When both
.Ar file1
or
.Ar file2
are specified, either file may be compressed.
.Sh ENVIRONMENT
.Bl -tag -width "TMPDIR"
.It Ev TMPDIR
Directory in which to place temporary files.
If unset,
.Pa /tmp
is used.
.El
.Sh FILES
.Bl -tag -width "/tmp/zdiff.XXXXXXXXXX" -compact
.It Pa /tmp/zcmp.XXXXXXXXXX
Temporary file for
.Nm zcmp .
.It Pa /tmp/zdiff.XXXXXXXXXX
Temporary file for
.Nm zdiff .
.El
.Sh SEE ALSO
.Xr cmp 1 ,
.Xr compress 1 ,
.Xr diff 1
.Sh CAVEATS
.Nm zcmp
and
.Nm zdiff
rely solely on the file extension to determine what is, or is not,
a compressed file.
Consequently, the following are not supported as arguments:
.Bl -dash
.It
directories
.It
device special files
.It
filenames indicating the standard input ("-")
.El

52
usr.bin/gzip/zforce Executable file
View File

@ -0,0 +1,52 @@
#!/bin/sh -
#
# $OpenBSD: zforce,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.
#
prog=`basename $0`
USAGE="usage: $prog file ..."
if test $# -eq 0; then
echo $USAGE
exit 1
fi
ret=0
while test $# -ne 0; do
case "$1" in
*[._-]gz)
shift
;;
*.t[ag]z)
shift
;;
*)
if file "$1" |
grep -q "gzip compressed data" 2> /dev/null
then
n="$1".gz
if mv "$1" "$n" 2> /dev/null; then
echo "$1" -- renamed to "$n"
else
ret=1
echo $prog: cannot rename "$1" to "$n"
fi
fi
shift
;;
esac
done
exit $ret

51
usr.bin/gzip/zforce.1 Normal file
View File

@ -0,0 +1,51 @@
.\" $OpenBSD: zforce.1,v 1.1 2003/07/29 11:50:09 otto 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.
.\"
.Dd July 29, 2003
.Dt ZFORCE 1
.Os
.Sh NAME
.Nm zforce
.Nd force gzip files to have a .gz suffix
.Sh SYNOPSIS
.Nm zforce
.Ar
.Sh DESCRIPTION
The
.Nm
utility renames
.Xr gzip 1
files to have a
.Sq .gz
suffix, so that
.Xr gzip 1
will not compress them twice.
This can be useful if file names were truncated during a file transfer.
Files that have an existing
.Sq .gz ,
.Sq -gz ,
.Sq _gz ,
.Sq .tgz
or
.Sq .taz
suffix, or that have not been compressed by
.Xr gzip 1 ,
are ignored.
.Sh SEE ALSO
.Xr gzip 1
.Sh CAVEATS
.Nm
overwrites existing files without warning.

72
usr.bin/gzip/zmore Executable file
View File

@ -0,0 +1,72 @@
#!/bin/sh -
#
# $OpenBSD: zmore,v 1.4 2003/07/29 07:42:45 otto Exp $
#
# Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
#
# 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.
#
# Sponsored in part by the Defense Advanced Research Projects
# Agency (DARPA) and Air Force Research Laboratory, Air Force
# Materiel Command, USAF, under agreement number F39502-99-1-0512.
#
# Pull out any command line flags so we can pass them to more/less
flags=
while test $# -ne 0; do
case "$1" in
--)
shift
break
;;
-*)
flags="$flags $1"
shift
;;
*)
break
;;
esac
done
# No files means read from stdin
if [ $# -eq 0 ]; then
compress -cdfq 2>&1 | ${PAGER-more} $flags
exit 0
fi
oterm=`stty -g 2>/dev/null`
while test $# -ne 0; do
compress -cdfq "$1" 2>&1 | ${PAGER-more} $flags
prev="$1"
shift
if tty -s && test -n "$oterm" -a $# -gt 0; then
#echo -n "--More--(Next file: $1)"
echo -n "$prev (END) - Next: $1 "
trap "stty $oterm 2>/dev/null" 0 1 2 3 13 15
stty cbreak -echo 2>/dev/null
REPLY=`dd bs=1 count=1 2>/dev/null`
stty $oterm 2>/dev/null
trap - 0 1 2 3 13 15
echo
case "$REPLY" in
s)
shift
;;
e|q)
break
;;
esac
fi
done
exit 0

92
usr.bin/gzip/zmore.1 Normal file
View File

@ -0,0 +1,92 @@
.\" $OpenBSD: zmore.1,v 1.3 2003/06/23 21:00:48 deraadt Exp $
.\"
.\" Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
.\"
.\" 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.
.\"
.\" Sponsored in part by the Defense Advanced Research Projects
.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
.\"
.Dd June 23, 2003
.Dt ZMORE 1
.Os
.Sh NAME
.Nm zmore
.Nd view compressed files on a crt
.Sh SYNOPSIS
.Nm zmore
.Op Ar flags
.Op Ar file ...
.Sh DESCRIPTION
.Nm
is a filter that allows the viewing of files compressed with Lempel-Ziv
encoding.
Such files generally have a
.Dq Z
or
.Dq gz
extension (both the
.Xr compress 1
and
.Xr gzip 1
formats are supported).
Any
.Ar flags
that are specified are passed to the user's preferred
.Ev PAGER
(which is
.Pa /usr/bin/more
by default).
.Pp
When multiple files are specified,
.Nm
will pause at the end of each file and present the following prompt to the user:
.Bd -literal -offset indent
prev_file (END) - Next: next_file
.Ed
.Pp
Where
.Sy prev_file
is the file that was just displayed and
.Sy next_file
is the next file to be displayed.
The following keys are recognized at the prompt:
.Bl -tag -width "e or q" -offset indent
.It Ic e No or Ic q
quit
.Nm zmore .
.It Ic s
skip the next file (or exit if the next file is the last).
.El
.Pp
If no files are specified,
.Nm
will read from the standard input.
In this mode
.Nm
will assume
.Xr gzip 1
style compression since there is no suffix on which to make a decision.
.Sh ENVIRONMENT
.Bl -tag -width "PAGER"
.It Ev PAGER
Program used to display files.
If unset,
.Pa /usr/bin/more
is used.
.El
.Sh SEE ALSO
.Xr compress 1 ,
.Xr less 1 ,
.Xr more 1

135
usr.bin/gzip/znew Executable file
View File

@ -0,0 +1,135 @@
#!/bin/ksh -
#
# $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.
#
# 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 $gzipflags -o "$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

69
usr.bin/gzip/znew.1 Normal file
View File

@ -0,0 +1,69 @@
.\" $OpenBSD: znew.1,v 1.1 2003/08/02 20:52:50 otto 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.
.\"
.Dd August 1, 2003
.Dt ZNEW 1
.Os
.Sh NAME
.Nm znew
.Nd convert compressed files to gzipped files
.Sh SYNOPSIS
.Nm
.Op Fl ftv9K
.Ar
.Sh DESCRIPTION
The
.Nm
utility uncompresses files compressed by
.Xr compress 1
and recompresses them with
.Xr gzip 1 .
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl f
Overwrite existing
.Sq .gz
files.
Unless this option is specified,
.Nm
refuses to overwrite existing files.
.It Fl t
Test integrity of the gzipped file before deleting the original file.
If the integrity check fails, the original
.Sq .Z
file is not removed.
.It Fl v
Print a report specifying the achieved compression ratios.
.It Fl 9
Use the -9 mode of
.Xr gzip 1 ,
achieving better compression at the cost of slower execution.
.It Fl K
Keep the original
.Sq .Z
file if it uses less disk blocks than the gzipped one.
A disk block is 1024 bytes.
.El
.Sh SEE ALSO
.Xr gzip 1
.Sh CAVEATS
The
.Nm
utility tries to maintain the file mode of the original file.
If the original file is not writable, it is not able to do that and
.Nm
will print a warning.