home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 15 / hacker15 / 15_H4CK3R#15.ISO / linux_security / engarde / ENGARDE_COMMUNITY_FEINT.ISO / usr / bin / znew < prev   
Encoding:
Text File  |  2002-05-30  |  3.4 KB  |  148 lines

  1. #!/bin/sh
  2.  
  3. PATH="/usr/bin:$PATH"; export PATH
  4. check=0
  5. pipe=0
  6. opt=
  7. files=
  8. keep=0
  9. res=0
  10. old=0
  11. new=0
  12. block=1024
  13. # block is the disk block size (best guess, need not be exact)
  14.  
  15. warn="(does not preserve modes and timestamp)"
  16. cpmod=
  17. cpmodarg=
  18. if type ${CPMOD:-cpmod} 2>/dev/null; then
  19.   cpmod=${CPMOD:-cpmod}
  20.   warn=""
  21. fi
  22.  
  23. if test -z "$cpmod"; then
  24.   cpmod=touch
  25.   cpmodarg="-r"
  26.   warn="(does not preserve file modes)"
  27. fi
  28.  
  29. case "$GZIP" in
  30.   *-S*) ext=`echo "$GZIP" | sed 's/^.*-S[[:space:]]*\([^[:space:]]*\).*$/\1/'`
  31.     ;;
  32.   *--suffix=*) ext=`echo "$GZIP" | sed 's/^.*--suffix=\([^[:space:]]*\).*$/\1/'`
  33.     ;;
  34.   *--suffix*) ext=`echo "$GZIP" | sed 's/^.*--suffix[[:space:]][[:space:]]*\([^[:space:]]*\).*$/\1/'`
  35.     ;;
  36.   *) ext='.gz'
  37.     ;;
  38. +esac
  39.  
  40. if test "$ext" = ".Z"; then
  41.   echo znew: cannot use .Z as gzip extension.
  42.   exit 1
  43. fi
  44.  
  45. for arg
  46. do
  47.   case "$arg" in
  48.   -*)     opt="$opt $arg"; shift;;
  49.    *)     break;;
  50.   esac
  51. done
  52.  
  53. if test $# -eq 0; then
  54.   echo "recompress .Z files into $ext (gzip) files"
  55.   echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
  56.   echo "  -t tests the new files before deleting originals"
  57.   echo "  -v be verbose"
  58.   echo "  -9 use the slowest compression method (optimal compression)"
  59.   echo "  -K keep a .Z file when it is smaller than the $ext file"
  60.   echo "  -P use pipes for the conversion $warn"
  61.   exit 1
  62. fi
  63.  
  64. opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
  65. case "$opt" in
  66.   *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
  67. esac
  68. case "$opt" in
  69.   *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
  70. esac
  71. case "$opt" in
  72.   *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
  73. esac
  74. if test -n "$opt"; then
  75.   opt="-$opt"
  76. fi
  77.  
  78. for i do
  79.   n=`echo $i | sed 's/.Z$//'`
  80.   if test ! -f "$n.Z" ; then
  81.     echo $n.Z not found
  82.     res=1; continue
  83.   fi
  84.   test $keep -eq 1 && old=`wc -c < "$n.Z"`
  85.   if test $pipe -eq 1; then
  86.     if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
  87.       # Copy file attributes from old file to new one, if possible.
  88.       test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
  89.     else
  90.       echo error while recompressing $n.Z
  91.       res=1; continue
  92.     fi
  93.   else
  94.     if test $check -eq 1; then
  95.       if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then
  96.     :
  97.       else
  98.     echo cannot backup "$n.Z"
  99.         res=1; continue
  100.       fi
  101.     fi
  102.     if gzip -d "$n.Z"; then
  103.       :
  104.     else
  105.       test $check -eq 1 && mv "$n.$$" "$n.Z"
  106.       echo error while uncompressing $n.Z
  107.       res=1; continue
  108.     fi
  109.     if gzip $opt "$n"; then
  110.       :
  111.     else
  112.       if test $check -eq 1; then
  113.     mv "$n.$$" "$n.Z" && rm -f "$n"
  114.         echo error while recompressing $n
  115.       else
  116.     # compress $n  (might be dangerous if disk full)
  117.         echo error while recompressing $n, left uncompressed
  118.       fi
  119.       res=1; continue
  120.     fi
  121.   fi
  122.   test $keep -eq 1 && new=`wc -c < "$n$ext"`
  123.   if test $keep -eq 1 -a `expr \( $old + $block - 1 \) / $block` -lt \
  124.                          `expr \( $new + $block - 1 \) / $block`; then
  125.     if test $pipe -eq 1; then
  126.       rm -f "$n$ext"
  127.     elif test $check -eq 1; then
  128.       mv "$n.$$" "$n.Z" && rm -f "$n$ext"
  129.     else
  130.       gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
  131.     fi
  132.     echo "$n.Z smaller than $n$ext -- unchanged"
  133.  
  134.   elif test $check -eq 1; then
  135.     if gzip -t "$n$ext" ; then
  136.       rm -f "$n.$$" "$n.Z"
  137.     else
  138.       test $pipe -eq 0 && mv "$n.$$" "$n.Z"
  139.       rm -f "$n$ext"
  140.       echo error while testing $n$ext, $n.Z unchanged
  141.       res=1; continue
  142.     fi
  143.   elif test $pipe -eq 1; then
  144.     rm -f "$n.Z"
  145.   fi
  146. done
  147. exit $res
  148.