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