delete trailing whitespace from control files
[debian/gzip] / znew.in
1 #!/bin/sh
2
3 # Copyright (C) 1998, 2002, 2004, 2007, 2010-2018 Free Software Foundation,
4 # Inc.
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 3 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 version="znew (gzip) @VERSION@
22 Copyright (C) 2010-2018 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.
26
27 Written by Jean-loup Gailly."
28
29 usage="Usage: $0 [OPTION]... [FILE]...
30 Recompress files from .Z (compress) format to .gz (gzip) format.
31
32 Options:
33
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
42
43 Report bugs to <bug-gzip@gnu.org>."
44
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)
55
56 # Beware -s or --suffix in $GZIP.
57 unset GZIP
58 ext=.gz
59
60 for arg
61 do
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
69
70 if test $# -eq 0; then
71   echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"
72   exit 1
73 fi
74
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"
87 fi
88
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
94   fi
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
104     fi
105   else
106     if test $check -eq 1; then
107       if cp -p "$n.Z" "$n.$$"; then
108         :
109       else
110         printf '%s\n' "cannot backup $n.Z"
111         res=1; continue
112       fi
113     fi
114     if gzip -d "$n.Z"; then
115       :
116     else
117       test $check -eq 1 && mv "$n.$$" "$n.Z"
118       printf '%s\n' "error while uncompressing $n.Z"
119       res=1; continue
120     fi
121     if gzip $opt "$n"; then
122       :
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"
130       fi
131       res=1; continue
132     fi
133   fi
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"
141     fi
142     printf '%s\n' "$n.Z smaller than $n$ext -- unchanged"
143
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
152     fi
153   elif test $pipe -eq 1; then
154     rm -f "$n.Z"
155   fi
156 done
157 exit $res