* NEWS, configure.ac (AC_INIT):
[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 of the License, or
10 # (at your option) 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 along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 PATH="BINDIR:$PATH"; export PATH
22
23 version="znew (gzip) @VERSION@
24 Copyright (C) 2006 Free Software Foundation, Inc.
25 This is free software.  You may redistribute copies of it under the terms of
26 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
27 There is NO WARRANTY, to the extent permitted by law.
28
29 Written by Jean-loup Gailly."
30
31 usage="Usage: znew [OPTION]... [FILE]...
32 Recompress files from .Z (compress) format to .gz (gzip) format.
33
34 Options:
35
36   -f     Force recompression even if a .gz file already exists.
37   -t     Test the new files before deleting originals.
38   -v     Verbose; display name and statistics for each file compressed.
39   -9     Use the slowest compression method (optimal compression).
40   -P     Use pipes for the conversion to reduce disk space usage.
41   -K     Keep a .Z file when it is smaller than the .gz file.
42       --help     display this help and exit
43       --version  output version information and exit
44
45 Report bugs to <bug-gzip@gnu.org>."
46
47 check=0
48 pipe=0
49 opt=
50 files=
51 keep=0
52 res=0
53 old=0
54 new=0
55 block=1024
56 # block is the disk block size (best guess, need not be exact)
57
58 warn="(does not preserve modes and timestamp)"
59 tmp=/tmp/zfoo.$$
60 set -C
61 echo hi > $tmp || exit
62 if test -z "`(${CPMOD-cpmod} $tmp $tmp) 2>&1`"; then
63   cpmod=${CPMOD-cpmod}
64   warn=""
65 fi
66
67 if test -z "$cpmod" && ${TOUCH-touch} -r $tmp $tmp 2>/dev/null; then
68   cpmod="${TOUCH-touch}"
69   cpmodarg="-r"
70   warn="(does not preserve file modes)"
71 fi
72
73 # check if GZIP env. variable uses -S or --suffix
74 gzip -q $tmp
75 ext=`echo $tmp* | sed "s|$tmp||"`
76 rm -f $tmp*
77 if test -z "$ext"; then
78   echo znew: error determining gzip extension
79   exit 1
80 fi
81 if test "$ext" = ".Z"; then
82   echo znew: cannot use .Z as gzip extension.
83   exit 1
84 fi
85
86 for arg
87 do
88   case "$arg" in
89   --help)      exec echo "$usage";;
90   --version)   exec echo "$version";;
91   -*)     opt="$opt $arg"; shift;;
92    *)     break;;
93   esac
94 done
95
96 if test $# -eq 0; then
97   echo "$usage"
98   exit 1
99 fi
100
101 opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
102 case "$opt" in
103   *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
104 esac
105 case "$opt" in
106   *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
107 esac
108 case "$opt" in
109   *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
110 esac
111 if test -n "$opt"; then
112   opt="-$opt"
113 fi
114
115 for i do
116   n=`echo $i | sed 's/.Z$//'`
117   if test ! -f "$n.Z" ; then
118     echo $n.Z not found
119     res=1; continue
120   fi
121   test $keep -eq 1 && old=`wc -c < "$n.Z"`
122   if test $pipe -eq 1; then
123     if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
124       # Copy file attributes from old file to new one, if possible.
125       test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
126     else
127       echo error while recompressing $n.Z
128       res=1; continue
129     fi
130   else
131     if test $check -eq 1; then
132       if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then
133         :
134       else
135         echo cannot backup "$n.Z"
136         res=1; continue
137       fi
138     fi
139     if gzip -d "$n.Z"; then
140       :
141     else
142       test $check -eq 1 && mv "$n.$$" "$n.Z"
143       echo error while uncompressing $n.Z
144       res=1; continue
145     fi
146     if gzip $opt "$n"; then
147       :
148     else
149       if test $check -eq 1; then
150         mv "$n.$$" "$n.Z" && rm -f "$n"
151         echo error while recompressing $n
152       else
153         # compress $n  (might be dangerous if disk full)
154         echo error while recompressing $n, left uncompressed
155       fi
156       res=1; continue
157     fi
158   fi
159   test $keep -eq 1 && new=`wc -c < "$n$ext"`
160   if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
161                               `expr \( $new + $block - 1 \) / $block`; then
162     if test $pipe -eq 1; then
163       rm -f "$n$ext"
164     elif test $check -eq 1; then
165       mv "$n.$$" "$n.Z" && rm -f "$n$ext"
166     else
167       gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
168     fi
169     echo "$n.Z smaller than $n$ext -- unchanged"
170
171   elif test $check -eq 1; then
172     if gzip -t "$n$ext" ; then
173       rm -f "$n.$$" "$n.Z"
174     else
175       test $pipe -eq 0 && mv "$n.$$" "$n.Z"
176       rm -f "$n$ext"
177       echo error while testing $n$ext, $n.Z unchanged
178       res=1; continue
179     fi
180   elif test $pipe -eq 1; then
181     rm -f "$n.Z"
182   fi
183 done
184 exit $res