| 1 | #!/bin/sh |
| 2 | |
| 3 | PATH="/usr/local/bin:$PATH"; export PATH |
| 4 | check=0 |
| 5 | pipe=0 |
| 6 | opt= |
| 7 | files= |
| 8 | keep=0 |
| 9 | res=0 |
| 10 | old=0 |
| 11 | new=0 |
| 12 | block=1024 |
| 13 | # block is the disk block size (best guess, need not be exact) |
| 14 | |
| 15 | warn="(does not preserve modes and timestamp)" |
| 16 | tmp=/tmp/zfoo.$$ |
| 17 | echo hi > $tmp.1 |
| 18 | echo hi > $tmp.2 |
| 19 | if test -z "`(${CPMOD-cpmod} $tmp.1 $tmp.2) 2>&1`"; then |
| 20 | cpmod=${CPMOD-cpmod} |
| 21 | warn="" |
| 22 | fi |
| 23 | |
| 24 | if test -z "$cpmod" && ${TOUCH-touch} -r $tmp.1 $tmp.2 2>/dev/null; then |
| 25 | cpmod="${TOUCH-touch}" |
| 26 | cpmodarg="-r" |
| 27 | warn="(does not preserve file modes)" |
| 28 | fi |
| 29 | |
| 30 | # check if GZIP env. variable uses -S or --suffix |
| 31 | gzip -q $tmp.1 |
| 32 | ext=`echo $tmp.1* | sed "s|$tmp.1||"` |
| 33 | rm -f $tmp.[12]* |
| 34 | if test -z "$ext"; then |
| 35 | echo znew: error determining gzip extension |
| 36 | exit 1 |
| 37 | fi |
| 38 | if test "$ext" = ".Z"; then |
| 39 | echo znew: cannot use .Z as gzip extension. |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | A= |
| 44 | fileno=0 |
| 45 | |
| 46 | for arg |
| 47 | do |
| 48 | case "$arg" in |
| 49 | -*) opt="$opt $arg";; |
| 50 | *) fileno=`expr $fileno + 1` |
| 51 | eval A$fileno=\$arg |
| 52 | A="$A \"\$A$fileno\"" |
| 53 | ;; |
| 54 | esac |
| 55 | done |
| 56 | |
| 57 | if test $fileno -eq 0; then |
| 58 | echo "recompress .Z files into $ext (gzip) files" |
| 59 | echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z... |
| 60 | echo " -t tests the new files before deleting originals" |
| 61 | echo " -v be verbose" |
| 62 | echo " -9 use the slowest compression method (optimal compression)" |
| 63 | echo " -K keep a .Z file when it is smaller than the $ext file" |
| 64 | echo " -P use pipes for the conversion $warn" |
| 65 | exit 1 |
| 66 | fi |
| 67 | |
| 68 | eval set "$A" # the files are now in $1, $2, ... |
| 69 | |
| 70 | opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'` |
| 71 | case "$opt" in |
| 72 | *t*) check=1; opt=`echo "$opt" | sed 's/t//g'` |
| 73 | esac |
| 74 | case "$opt" in |
| 75 | *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'` |
| 76 | esac |
| 77 | case "$opt" in |
| 78 | *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'` |
| 79 | esac |
| 80 | if test -n "$opt"; then |
| 81 | opt="-$opt" |
| 82 | fi |
| 83 | |
| 84 | for i do |
| 85 | n=`echo $i | sed 's/.Z$//'` |
| 86 | if test ! -f "$n.Z" ; then |
| 87 | echo $n.Z not found |
| 88 | res=1; continue |
| 89 | fi |
| 90 | test $keep -eq 1 && old=`wc -c < "$n.Z"` |
| 91 | if test $pipe -eq 1; then |
| 92 | if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then |
| 93 | # Copy file attributes from old file to new one, if possible. |
| 94 | test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null |
| 95 | else |
| 96 | echo error while recompressing $n.Z |
| 97 | res=1; continue |
| 98 | fi |
| 99 | else |
| 100 | if test $check -eq 1; then |
| 101 | if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then |
| 102 | : |
| 103 | else |
| 104 | echo cannot backup "$n.Z" |
| 105 | res=1; continue |
| 106 | fi |
| 107 | fi |
| 108 | if gzip -d "$n.Z"; then |
| 109 | : |
| 110 | else |
| 111 | test $check -eq 1 && mv "$n.$$" "$n.Z" |
| 112 | echo error while uncompressing $n.Z |
| 113 | res=1; continue |
| 114 | fi |
| 115 | if gzip $opt "$n"; then |
| 116 | : |
| 117 | else |
| 118 | if test $check -eq 1; then |
| 119 | mv "$n.$$" "$n.Z" && rm -f "$n" |
| 120 | echo error while recompressing $n |
| 121 | else |
| 122 | # compress $n (might be dangerous if disk full) |
| 123 | echo error while recompressing $n, left uncompressed |
| 124 | fi |
| 125 | res=1; continue |
| 126 | fi |
| 127 | fi |
| 128 | test $keep -eq 1 && new=`wc -c < "$n$ext"` |
| 129 | if test $keep -eq 1 -a `expr \( $old + $block - 1 \) / $block` -lt \ |
| 130 | `expr \( $new + $block - 1 \) / $block`; then |
| 131 | if test $pipe -eq 1; then |
| 132 | rm -f "$n$ext" |
| 133 | elif test $check -eq 1; then |
| 134 | mv "$n.$$" "$n.Z" && rm -f "$n$ext" |
| 135 | else |
| 136 | gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext" |
| 137 | fi |
| 138 | echo "$n.Z smaller than $n$ext -- unchanged" |
| 139 | |
| 140 | elif test $check -eq 1; then |
| 141 | if gzip -t "$n$ext" ; then |
| 142 | rm -f "$n.$$" "$n.Z" |
| 143 | else |
| 144 | test $pipe -eq 0 && mv "$n.$$" "$n.Z" |
| 145 | rm -f "$n$ext" |
| 146 | echo error while testing $n$ext, $n.Z unchanged |
| 147 | res=1; continue |
| 148 | fi |
| 149 | elif test $pipe -eq 1; then |
| 150 | rm -f "$n.Z" |
| 151 | fi |
| 152 | done |
| 153 | exit $res |