This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / gnu / usr.bin / gzip / znew
CommitLineData
3013fe88
NW
1#!/bin/sh
2
caed0dfe 3PATH="/usr/local/bin:$PATH"; export PATH
3013fe88
NW
4check=0
5pipe=0
6opt=
7files=
8keep=0
9res=0
10old=0
11new=0
12block=1024
13# block is the disk block size (best guess, need not be exact)
14
15warn="(does not preserve modes and timestamp)"
16tmp=/tmp/zfoo.$$
17echo hi > $tmp.1
18echo hi > $tmp.2
19if test -z "`(${CPMOD-cpmod} $tmp.1 $tmp.2) 2>&1`"; then
20 cpmod=${CPMOD-cpmod}
21 warn=""
22fi
23
24if 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)"
28fi
caed0dfe
NW
29
30# check if GZIP env. variable uses -S or --suffix
31gzip -q $tmp.1
32ext=`echo $tmp.1* | sed "s|$tmp.1||"`
33rm -f $tmp.[12]*
34if test -z "$ext"; then
35 echo znew: error determining gzip extension
36 exit 1
37fi
38if test "$ext" = ".Z"; then
39 echo znew: cannot use .Z as gzip extension.
40 exit 1
41fi
3013fe88
NW
42
43A=
44fileno=0
45
46for arg
47do
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
55done
56
57if test $fileno -eq 0; then
caed0dfe
NW
58 echo "recompress .Z files into $ext (gzip) files"
59 echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
3013fe88
NW
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)"
caed0dfe 63 echo " -K keep a .Z file when it is smaller than the $ext file"
3013fe88
NW
64 echo " -P use pipes for the conversion $warn"
65 exit 1
66fi
67
68eval set "$A" # the files are now in $1, $2, ...
69
70opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
71case "$opt" in
72 *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
73esac
74case "$opt" in
75 *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
76esac
77case "$opt" in
78 *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
79esac
80if test -n "$opt"; then
81 opt="-$opt"
82fi
83
84for 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
caed0dfe 92 if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
3013fe88 93 # Copy file attributes from old file to new one, if possible.
caed0dfe 94 test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
3013fe88
NW
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
caed0dfe 128 test $keep -eq 1 && new=`wc -c < "$n$ext"`
3013fe88
NW
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
caed0dfe 132 rm -f "$n$ext"
3013fe88 133 elif test $check -eq 1; then
caed0dfe 134 mv "$n.$$" "$n.Z" && rm -f "$n$ext"
3013fe88 135 else
caed0dfe 136 gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
3013fe88 137 fi
caed0dfe 138 echo "$n.Z smaller than $n$ext -- unchanged"
3013fe88
NW
139
140 elif test $check -eq 1; then
caed0dfe 141 if gzip -t "$n$ext" ; then
3013fe88
NW
142 rm -f "$n.$$" "$n.Z"
143 else
144 test $pipe -eq 0 && mv "$n.$$" "$n.Z"
caed0dfe
NW
145 rm -f "$n$ext"
146 echo error while testing $n$ext, $n.Z unchanged
3013fe88
NW
147 res=1; continue
148 fi
149 elif test $pipe -eq 1; then
150 rm -f "$n.Z"
151 fi
152done
153exit $res