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