maint: update --version copyright dates
[gzip.git] / znew.in
blob7d68b702554e3ad7734752caeeffc9a45d21a079
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 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 LC_ALL=C
22 export LC_ALL
24 version="znew (gzip) @VERSION@
25 Copyright (C) 2024 Free Software Foundation, Inc.
26 This is free software. You may redistribute copies of it under the terms of
27 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
28 There is NO WARRANTY, to the extent permitted by law.
30 Written by Jean-loup Gailly."
32 usage="Usage: $0 [OPTION]... [FILE]...
33 Recompress files from .Z (compress) format to .gz (gzip) format.
35 Options:
37 -f Force recompression even if a .gz file already exists.
38 -t Test the new files before deleting originals.
39 -v Verbose; display name and statistics for each file compressed.
40 -9 Use the slowest compression method (optimal compression).
41 -P Use pipes for the conversion to reduce disk space usage.
42 -K Keep a .Z file when it is smaller than the .gz file; implies -t.
43 --help display this help and exit
44 --version output version information and exit
46 Report bugs to <bug-gzip@gnu.org>."
48 check=0
49 pipe=0
50 opt=
51 files=
52 keep=0
53 res=0
54 old=0
55 new=0
56 block=1024
57 # block is the disk block size (best guess, need not be exact)
59 # Beware -s or --suffix in $GZIP.
60 unset GZIP
61 ext=.gz
63 for arg
65 case "$arg" in
66 --help) printf '%s\n' "$usage" || exit 1; exit;;
67 --version) printf '%s\n' "$version" || exit 1; exit;;
68 -*) opt="$opt $arg"; shift;;
69 *) break;;
70 esac
71 done
73 if test $# -eq 0; then
74 echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"
75 exit 1
78 opt=`printf '%s\n' "$opt" | sed -e 's/ //g' -e 's/-//g'`
79 case "$opt" in
80 *t*) check=1; opt=`printf '%s\n' "$opt" | sed 's/t//g'`
81 esac
82 case "$opt" in
83 *K*) keep=1; check=1; opt=`printf '%s\n' "$opt" | sed 's/K//g'`
84 esac
85 case "$opt" in
86 *P*) pipe=1; opt=`printf '%s\n' "$opt" | sed 's/P//g'`
87 esac
88 if test -n "$opt"; then
89 opt="-$opt"
92 for i do
93 n=`printf '%s\n' "$i" | sed 's/.Z$//'`
94 if test ! -f "$n.Z" ; then
95 printf '%s\n' "$n.Z not found"
96 res=1; continue
98 test $keep -eq 1 && old=`wc -c < "$n.Z"`
99 if test $pipe -eq 1; then
100 if 'gzip' -d < "$n.Z" | 'gzip' $opt > "$n$ext"; then
101 # Copy file attributes from old file to new one, if possible.
102 touch -r"$n.Z" -- "$n$ext" 2>/dev/null
103 chmod --reference="$n.Z" -- "$n$ext" 2>/dev/null
104 else
105 printf '%s\n' "error while recompressing $n.Z"
106 res=1; continue
108 else
109 if test $check -eq 1; then
110 if cp -p "$n.Z" "$n.$$"; then
112 else
113 printf '%s\n' "cannot backup $n.Z"
114 res=1; continue
117 if 'gzip' -d "$n.Z"; then
119 else
120 test $check -eq 1 && mv "$n.$$" "$n.Z"
121 printf '%s\n' "error while uncompressing $n.Z"
122 res=1; continue
124 if 'gzip' $opt "$n"; then
126 else
127 if test $check -eq 1; then
128 mv "$n.$$" "$n.Z" && rm -f "$n"
129 printf '%s\n' "error while recompressing $n"
130 else
131 # compress $n (might be dangerous if disk full)
132 printf '%s\n' "error while recompressing $n, left uncompressed"
134 res=1; continue
137 test $keep -eq 1 && new=`wc -c < "$n$ext"`
138 if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
139 `expr \( $new + $block - 1 \) / $block`; then
140 if test $pipe -eq 1; then
141 rm -f "$n$ext"
142 else
143 mv "$n.$$" "$n.Z" && rm -f "$n$ext"
145 printf '%s\n' "$n.Z smaller than $n$ext -- unchanged"
147 elif test $check -eq 1; then
148 if 'gzip' -t "$n$ext" ; then
149 rm -f "$n.$$" "$n.Z"
150 else
151 test $pipe -eq 0 && mv "$n.$$" "$n.Z"
152 rm -f "$n$ext"
153 printf '%s\n' "error while testing $n$ext, $n.Z unchanged"
154 res=1; continue
156 elif test $pipe -eq 1; then
157 rm -f "$n.Z"
159 done
160 exit $res