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