3 # $FreeBSD: src/gnu/usr.bin/gzip/znew,v 1.5.2.1 2002/01/28 01:21:12 nectar Exp $
14 # block is the disk block size (best guess, need not be exact)
16 warn="(does not preserve modes and timestamp)"
17 tmp=`mktemp -d -t znew`
18 if test -z "$tmp"; then
19 echo znew: could not create temporary directory
24 if test -z "`(${CPMOD-cpmod} $tmp/1 $tmp/2) 2>&1`"; then
29 if test -z "$cpmod" && ${TOUCH-touch} -r $tmp/1 $tmp/2 2>/dev/null; then
30 cpmod="${TOUCH-touch}"
32 warn="(does not preserve file modes)"
35 # check if GZIP env. variable uses -S or --suffix
37 ext=`echo $tmp/1* | sed "s|$tmp/1||"`
39 if test -z "$ext"; then
40 echo znew: error determining gzip extension
43 if test "$ext" = ".Z"; then
44 echo znew: cannot use .Z as gzip extension.
51 -*) opt="$opt $arg"; shift;;
56 if test $# -eq 0; then
57 echo "recompress .Z files into $ext (gzip) files"
58 echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
59 echo " -t tests the new files before deleting originals"
61 echo " -9 use the slowest compression method (optimal compression)"
62 echo " -K keep a .Z file when it is smaller than the $ext file"
63 echo " -P use pipes for the conversion $warn"
67 opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
69 *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
72 *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
75 *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
77 if test -n "$opt"; then
82 n=`echo $i | sed 's/.Z$//'`
83 if test ! -f "$n.Z" ; then
87 test $keep -eq 1 && old=`wc -c < "$n.Z"`
88 if test $pipe -eq 1; then
89 if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
90 # Copy file attributes from old file to new one, if possible.
91 test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
93 echo error while recompressing $n.Z
97 if test $check -eq 1; then
98 if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then
101 echo cannot backup "$n.Z"
105 if gzip -d "$n.Z"; then
108 test $check -eq 1 && mv "$n.$$" "$n.Z"
109 echo error while uncompressing $n.Z
112 if gzip $opt "$n"; then
115 if test $check -eq 1; then
116 mv "$n.$$" "$n.Z" && rm -f "$n"
117 echo error while recompressing $n
119 # compress $n (might be dangerous if disk full)
120 echo error while recompressing $n, left uncompressed
125 test $keep -eq 1 && new=`wc -c < "$n$ext"`
126 if test $keep -eq 1 -a `expr \( $old + $block - 1 \) / $block` -lt \
127 `expr \( $new + $block - 1 \) / $block`; then
128 if test $pipe -eq 1; then
130 elif test $check -eq 1; then
131 mv "$n.$$" "$n.Z" && rm -f "$n$ext"
133 gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
135 echo "$n.Z smaller than $n$ext -- unchanged"
137 elif test $check -eq 1; then
138 if gzip -t "$n$ext" ; then
141 test $pipe -eq 0 && mv "$n.$$" "$n.Z"
143 echo error while testing $n$ext, $n.Z unchanged
146 elif test $pipe -eq 1; then