tests: reference: don't rely on od's -w option
[gzip.git] / znew.in
blobb02f9deda4ef1d63a59aff5a3b699d2dea474a14
1 #!/bin/sh
3 # Copyright (C) 1998, 2002, 2004, 2007, 2010-2024 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
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 LC_ALL=C
21 export LC_ALL
23 version="znew (gzip) @VERSION@
24 Copyright (C) 2024 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 <https://www.gnu.org/licenses/gpl.html>.
27 There is NO WARRANTY, to the extent permitted by law.
29 Written by Jean-loup Gailly."
31 usage="Usage: $0 [OPTION]... [FILE]...
32 Recompress files from .Z (compress) format to .gz (gzip) format.
34 Options:
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; implies -t.
42 --help display this help and exit
43 --version output version information and exit
45 Report bugs to <bug-gzip@gnu.org>."
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)
58 # Beware -s or --suffix in $GZIP.
59 unset GZIP
60 ext=.gz
62 for arg
64 case "$arg" in
65 --help) printf '%s\n' "$usage" || exit 1; exit;;
66 --version) printf '%s\n' "$version" || exit 1; exit;;
67 -*) opt="$opt $arg"; shift;;
68 *) break;;
69 esac
70 done
72 if test $# -eq 0; then
73 echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"
74 exit 1
77 opt=`printf '%s\n' "$opt" | sed -e 's/ //g' -e 's/-//g'`
78 case "$opt" in
79 *t*) check=1; opt=`printf '%s\n' "$opt" | sed 's/t//g'`
80 esac
81 case "$opt" in
82 *K*) keep=1; check=1; opt=`printf '%s\n' "$opt" | sed 's/K//g'`
83 esac
84 case "$opt" in
85 *P*) pipe=1; opt=`printf '%s\n' "$opt" | sed 's/P//g'`
86 esac
87 if test -n "$opt"; then
88 opt="-$opt"
91 for i do
92 n=`printf '%s\n' "$i" | sed 's/.Z$//'`
93 if test ! -f "$n.Z" ; then
94 printf '%s\n' "$n.Z not found"
95 res=1; continue
97 test $keep -eq 1 && old=`wc -c < "$n.Z"`
98 if test $pipe -eq 1; then
99 if 'gzip' -d < "$n.Z" | 'gzip' $opt > "$n$ext"; then
100 # Copy file attributes from old file to new one, if possible.
101 touch -r"$n.Z" -- "$n$ext" 2>/dev/null
102 chmod --reference="$n.Z" -- "$n$ext" 2>/dev/null
103 else
104 printf '%s\n' "error while recompressing $n.Z"
105 res=1; continue
107 else
108 if test $check -eq 1; then
109 if cp -p "$n.Z" "$n.$$"; then
111 else
112 printf '%s\n' "cannot backup $n.Z"
113 res=1; continue
116 if 'gzip' -d "$n.Z"; then
118 else
119 test $check -eq 1 && mv "$n.$$" "$n.Z"
120 printf '%s\n' "error while uncompressing $n.Z"
121 res=1; continue
123 if 'gzip' $opt "$n"; then
125 else
126 if test $check -eq 1; then
127 mv "$n.$$" "$n.Z" && rm -f "$n"
128 printf '%s\n' "error while recompressing $n"
129 else
130 # compress $n (might be dangerous if disk full)
131 printf '%s\n' "error while recompressing $n, left uncompressed"
133 res=1; continue
136 test $keep -eq 1 && new=`wc -c < "$n$ext"`
137 if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
138 `expr \( $new + $block - 1 \) / $block`; then
139 if test $pipe -eq 1; then
140 rm -f "$n$ext"
141 else
142 mv "$n.$$" "$n.Z" && rm -f "$n$ext"
144 printf '%s\n' "$n.Z smaller than $n$ext -- unchanged"
146 elif test $check -eq 1; then
147 if 'gzip' -t "$n$ext" ; then
148 rm -f "$n.$$" "$n.Z"
149 else
150 test $pipe -eq 0 && mv "$n.$$" "$n.Z"
151 rm -f "$n$ext"
152 printf '%s\n' "error while testing $n$ext, $n.Z unchanged"
153 res=1; continue
155 elif test $pipe -eq 1; then
156 rm -f "$n.Z"
158 done
159 exit $res