gzexe: ensure file always exists
[gzip.git] / znew.in
blob48475e9d0dd8580fac76f4f4c18efe354fd4be42
1 #!/bin/sh
3 # Copyright (C) 1998, 2002, 2004, 2007, 2010-2017 Free Software Foundation,
4 # Inc.
5 # Copyright (C) 1993 Jean-loup Gailly
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 3 of the License, or
10 # (at your option) any later version.
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.
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.
21 version="znew (gzip) @VERSION@
22 Copyright (C) 2010-2017 Free Software Foundation, Inc.
23 This is free software. You may redistribute copies of it under the terms of
24 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
25 There is NO WARRANTY, to the extent permitted by law.
27 Written by Jean-loup Gailly."
29 usage="Usage: $0 [OPTION]... [FILE]...
30 Recompress files from .Z (compress) format to .gz (gzip) format.
32 Options:
34 -f Force recompression even if a .gz file already exists.
35 -t Test the new files before deleting originals.
36 -v Verbose; display name and statistics for each file compressed.
37 -9 Use the slowest compression method (optimal compression).
38 -P Use pipes for the conversion to reduce disk space usage.
39 -K Keep a .Z file when it is smaller than the .gz file; implies -t.
40 --help display this help and exit
41 --version output version information and exit
43 Report bugs to <bug-gzip@gnu.org>."
45 check=0
46 pipe=0
47 opt=
48 files=
49 keep=0
50 res=0
51 old=0
52 new=0
53 block=1024
54 # block is the disk block size (best guess, need not be exact)
56 # Beware -s or --suffix in $GZIP.
57 unset GZIP
58 ext=.gz
60 for arg
62 case "$arg" in
63 --help) printf '%s\n' "$usage" || exit 1; exit;;
64 --version) printf '%s\n' "$version" || exit 1; exit;;
65 -*) opt="$opt $arg"; shift;;
66 *) break;;
67 esac
68 done
70 if test $# -eq 0; then
71 echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"
72 exit 1
75 opt=`printf '%s\n' "$opt" | sed -e 's/ //g' -e 's/-//g'`
76 case "$opt" in
77 *t*) check=1; opt=`printf '%s\n' "$opt" | sed 's/t//g'`
78 esac
79 case "$opt" in
80 *K*) keep=1; check=1; opt=`printf '%s\n' "$opt" | sed 's/K//g'`
81 esac
82 case "$opt" in
83 *P*) pipe=1; opt=`printf '%s\n' "$opt" | sed 's/P//g'`
84 esac
85 if test -n "$opt"; then
86 opt="-$opt"
89 for i do
90 n=`printf '%s\n' "$i" | sed 's/.Z$//'`
91 if test ! -f "$n.Z" ; then
92 printf '%s\n' "$n.Z not found"
93 res=1; continue
95 test $keep -eq 1 && old=`wc -c < "$n.Z"`
96 if test $pipe -eq 1; then
97 if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
98 # Copy file attributes from old file to new one, if possible.
99 touch -r"$n.Z" -- "$n$ext" 2>/dev/null
100 chmod --reference="$n.Z" -- "$n$ext" 2>/dev/null
101 else
102 printf '%s\n' "error while recompressing $n.Z"
103 res=1; continue
105 else
106 if test $check -eq 1; then
107 if cp -p "$n.Z" "$n.$$"; then
109 else
110 printf '%s\n' "cannot backup $n.Z"
111 res=1; continue
114 if gzip -d "$n.Z"; then
116 else
117 test $check -eq 1 && mv "$n.$$" "$n.Z"
118 printf '%s\n' "error while uncompressing $n.Z"
119 res=1; continue
121 if gzip $opt "$n"; then
123 else
124 if test $check -eq 1; then
125 mv "$n.$$" "$n.Z" && rm -f "$n"
126 printf '%s\n' "error while recompressing $n"
127 else
128 # compress $n (might be dangerous if disk full)
129 printf '%s\n' "error while recompressing $n, left uncompressed"
131 res=1; continue
134 test $keep -eq 1 && new=`wc -c < "$n$ext"`
135 if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
136 `expr \( $new + $block - 1 \) / $block`; then
137 if test $pipe -eq 1; then
138 rm -f "$n$ext"
139 else
140 mv "$n.$$" "$n.Z" && rm -f "$n$ext"
142 printf '%s\n' "$n.Z smaller than $n$ext -- unchanged"
144 elif test $check -eq 1; then
145 if gzip -t "$n$ext" ; then
146 rm -f "$n.$$" "$n.Z"
147 else
148 test $pipe -eq 0 && mv "$n.$$" "$n.Z"
149 rm -f "$n$ext"
150 printf '%s\n' "error while testing $n$ext, $n.Z unchanged"
151 res=1; continue
153 elif test $pipe -eq 1; then
154 rm -f "$n.Z"
156 done
157 exit $res