Use just one temporary, not two.
[debian/gzip] / znew.in
1 :
2 #!/bin/sh
3
4 # Copyright (C) 1998, 2002, 2004 Free Software Foundation
5 # Copyright (C) 1993 Jean-loup Gailly
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 # 02111-1307, USA.
21
22 PATH="BINDIR:$PATH"; export PATH
23 check=0
24 pipe=0
25 opt=
26 files=
27 keep=0
28 res=0
29 old=0
30 new=0
31 block=1024
32 # block is the disk block size (best guess, need not be exact)
33
34 warn="(does not preserve modes and timestamp)"
35 tmp=/tmp/zfoo.$$
36 set -C
37 echo hi > $tmp || exit
38 if test -z "`(${CPMOD-cpmod} $tmp $tmp) 2>&1`"; then
39   cpmod=${CPMOD-cpmod}
40   warn=""
41 fi
42
43 if test -z "$cpmod" && ${TOUCH-touch} -r $tmp $tmp 2>/dev/null; then
44   cpmod="${TOUCH-touch}"
45   cpmodarg="-r"
46   warn="(does not preserve file modes)"
47 fi
48
49 # check if GZIP env. variable uses -S or --suffix
50 gzip -q $tmp
51 ext=`echo $tmp* | sed "s|$tmp||"`
52 rm -f $tmp*
53 if test -z "$ext"; then
54   echo znew: error determining gzip extension
55   exit 1
56 fi
57 if test "$ext" = ".Z"; then
58   echo znew: cannot use .Z as gzip extension.
59   exit 1
60 fi
61
62 for arg
63 do
64   case "$arg" in
65   -*)     opt="$opt $arg"; shift;;
66    *)     break;;
67   esac
68 done
69
70 if test $# -eq 0; then
71   echo "recompress .Z files into $ext (gzip) files"
72   echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
73   echo "  -t tests the new files before deleting originals"
74   echo "  -v be verbose"
75   echo "  -9 use the slowest compression method (optimal compression)"
76   echo "  -K keep a .Z file when it is smaller than the $ext file"
77   echo "  -P use pipes for the conversion $warn"
78   exit 1
79 fi
80
81 opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
82 case "$opt" in
83   *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
84 esac
85 case "$opt" in
86   *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
87 esac
88 case "$opt" in
89   *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
90 esac
91 if test -n "$opt"; then
92   opt="-$opt"
93 fi
94
95 for i do
96   n=`echo $i | sed 's/.Z$//'`
97   if test ! -f "$n.Z" ; then
98     echo $n.Z not found
99     res=1; continue
100   fi
101   test $keep -eq 1 && old=`wc -c < "$n.Z"`
102   if test $pipe -eq 1; then
103     if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
104       # Copy file attributes from old file to new one, if possible.
105       test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
106     else
107       echo error while recompressing $n.Z
108       res=1; continue
109     fi
110   else
111     if test $check -eq 1; then
112       if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then
113         :
114       else
115         echo cannot backup "$n.Z"
116         res=1; continue
117       fi
118     fi
119     if gzip -d "$n.Z"; then
120       :
121     else
122       test $check -eq 1 && mv "$n.$$" "$n.Z"
123       echo error while uncompressing $n.Z
124       res=1; continue
125     fi
126     if gzip $opt "$n"; then
127       :
128     else
129       if test $check -eq 1; then
130         mv "$n.$$" "$n.Z" && rm -f "$n"
131         echo error while recompressing $n
132       else
133         # compress $n  (might be dangerous if disk full)
134         echo error while recompressing $n, left uncompressed
135       fi
136       res=1; continue
137     fi
138   fi
139   test $keep -eq 1 && new=`wc -c < "$n$ext"`
140   if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
141                               `expr \( $new + $block - 1 \) / $block`; then
142     if test $pipe -eq 1; then
143       rm -f "$n$ext"
144     elif test $check -eq 1; then
145       mv "$n.$$" "$n.Z" && rm -f "$n$ext"
146     else
147       gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
148     fi
149     echo "$n.Z smaller than $n$ext -- unchanged"
150
151   elif test $check -eq 1; then
152     if gzip -t "$n$ext" ; then
153       rm -f "$n.$$" "$n.Z"
154     else
155       test $pipe -eq 0 && mv "$n.$$" "$n.Z"
156       rm -f "$n$ext"
157       echo error while testing $n$ext, $n.Z unchanged
158       res=1; continue
159     fi
160   elif test $pipe -eq 1; then
161     rm -f "$n.Z"
162   fi
163 done
164 exit $res