xzdiff 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. # Copyright (C) 1998, 2002, 2006, 2007 Free Software Foundation
  3. # Copyright (C) 1993 Jean-loup Gailly
  4. # Modified for XZ Utils by Andrew Dudman and Lasse Collin.
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #SET_PATH - This line is a placeholder to ease patching this script.
  14. # Instead of unsetting XZ_OPT, just make sure that xz will use file format
  15. # autodetection. This way memory usage limit and thread limit can be
  16. # specified via XZ_OPT. With gzip, bzip2, and lzop it's OK to just unset the
  17. # environment variables.
  18. xz='xz --format=auto'
  19. unset GZIP BZIP BZIP2 LZOP
  20. case ${0##*/} in
  21. *cmp*) prog=xzcmp; cmp=${CMP:-cmp};;
  22. *) prog=xzdiff; cmp=${DIFF:-diff};;
  23. esac
  24. version="$prog (XZ Utils) 5.2.2"
  25. usage="Usage: ${0##*/} [OPTION]... FILE1 [FILE2]
  26. Compare FILE1 to FILE2, using their uncompressed contents if they are
  27. compressed. If FILE2 is omitted, then the files compared are FILE1 and
  28. FILE1 from which the compression format suffix has been stripped.
  29. Do comparisons like '$cmp' does. OPTIONs are the same as for '$cmp'.
  30. Report bugs to <lasse.collin@tukaani.org>."
  31. # sed script to escape all ' for the shell, and then (to handle trailing
  32. # newlines correctly) turn trailing X on last line into '.
  33. escape='
  34. s/'\''/'\''\\'\'''\''/g
  35. $s/X$/'\''/
  36. '
  37. while :; do
  38. case $1 in
  39. --h*) printf '%s\n' "$usage" || exit 2; exit;;
  40. --v*) echo "$version" || exit 2; exit;;
  41. --) shift; break;;
  42. -*\'*) cmp="$cmp '"`printf '%sX\n' "$1" | sed "$escape"`;;
  43. -?*) cmp="$cmp '$1'";;
  44. *) break;;
  45. esac
  46. shift
  47. done
  48. cmp="$cmp --"
  49. for file; do
  50. test "X$file" = X- || <"$file" || exit 2
  51. done
  52. xz1=$xz
  53. xz2=$xz
  54. xz_status=0
  55. exec 3>&1
  56. if test $# -eq 1; then
  57. case $1 in
  58. *[-.]xz | *[-.]lzma | *.t[lx]z)
  59. ;;
  60. *[-.]bz2 | *.tbz | *.tbz2)
  61. xz1=bzip2;;
  62. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z)
  63. xz1=gzip;;
  64. *[-.]lzo | *.tzo)
  65. xz1=lzop;;
  66. *)
  67. echo >&2 "$0: $1: Unknown compressed file name suffix"
  68. exit 2;;
  69. esac
  70. case $1 in
  71. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *[-.]lzo)
  72. FILE=`expr "X$1" : 'X\(.*\)[-.][abglmoxzZ2]*$'`;;
  73. *.t[abglx]z)
  74. FILE=`expr "X$1" : 'X\(.*[-.]t\)[abglx]z$'`ar;;
  75. *.tbz2)
  76. FILE=`expr "X$1" : 'X\(.*[-.]t\)bz2$'`ar;;
  77. *.tzo)
  78. FILE=`expr "X$1" : 'X\(.*[-.]t\)zo$'`ar;;
  79. esac
  80. xz_status=$(
  81. exec 4>&1
  82. ($xz1 -cd -- "$1" 4>&-; echo $? >&4) 3>&- | eval "$cmp" - '"$FILE"' >&3
  83. )
  84. elif test $# -eq 2; then
  85. case $1 in
  86. *[-.]bz2 | *.tbz | *.tbz2) xz1=bzip2;;
  87. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz1=gzip;;
  88. *[-.]lzo | *.tzo) xz1=lzop;;
  89. esac
  90. case $2 in
  91. *[-.]bz2 | *.tbz | *.tbz2) xz2=bzip2;;
  92. *[-.][zZ] | *_z | *[-.]gz | *.t[ag]z) xz2=gzip;;
  93. *[-.]lzo | *.tzo) xz2=lzop;;
  94. esac
  95. case $1 in
  96. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | -)
  97. case "$2" in
  98. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | -)
  99. if test "$1$2" = --; then
  100. xz_status=$(
  101. exec 4>&1
  102. ($xz1 -cdfq - 4>&-; echo $? >&4) 3>&- |
  103. eval "$cmp" - - >&3
  104. )
  105. elif # Reject Solaris 8's buggy /bin/bash 2.03.
  106. echo X | (echo X | eval "$cmp" /dev/fd/5 - >/dev/null 2>&1) 5<&0; then
  107. xz_status=$(
  108. exec 4>&1
  109. ($xz1 -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
  110. ( ($xz2 -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- 5<&- </dev/null |
  111. eval "$cmp" /dev/fd/5 - >&3) 5<&0
  112. )
  113. cmp_status=$?
  114. case $xz_status in
  115. *[1-9]*) xz_status=1;;
  116. *) xz_status=0;;
  117. esac
  118. (exit $cmp_status)
  119. else
  120. F=`expr "/$2" : '.*/\(.*\)[-.][ablmotxz2]*$'` || F=$prog
  121. tmp=
  122. trap '
  123. test -n "$tmp" && rm -rf "$tmp"
  124. (exit 2); exit 2
  125. ' HUP INT PIPE TERM 0
  126. if type mktemp >/dev/null 2>&1; then
  127. # Note that FreeBSD's mktemp isn't fully compatible with
  128. # the implementations from mktemp.org and GNU coreutils.
  129. # It is important that the -t argument is the last argument
  130. # and that no "--" is used between -t and the template argument.
  131. # This way this command works on all implementations.
  132. tmp=`mktemp -d -t "$prog.XXXXXXXXXX"` || exit 2
  133. else
  134. # Fallback code if mktemp is missing. This isn't as
  135. # robust as using mktemp since this doesn't try with
  136. # different file names in case of a file name conflict.
  137. #
  138. # There's no need to save the original umask since
  139. # we don't create any non-temp files. Note that using
  140. # mkdir -m 0077 isn't secure since some mkdir implementations
  141. # create the dir with the default umask and chmod the
  142. # the dir afterwards.
  143. umask 0077
  144. mkdir -- "${TMPDIR-/tmp}/$prog.$$" || exit 2
  145. tmp="${TMPDIR-/tmp}/$prog.$$"
  146. fi
  147. $xz2 -cdfq -- "$2" > "$tmp/$F" || exit 2
  148. xz_status=$(
  149. exec 4>&1
  150. ($xz1 -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
  151. eval "$cmp" - '"$tmp/$F"' >&3
  152. )
  153. cmp_status=$?
  154. rm -rf "$tmp" || xz_status=$?
  155. trap - HUP INT PIPE TERM 0
  156. (exit $cmp_status)
  157. fi;;
  158. *)
  159. xz_status=$(
  160. exec 4>&1
  161. ($xz1 -cdfq -- "$1" 4>&-; echo $? >&4) 3>&- |
  162. eval "$cmp" - '"$2"' >&3
  163. );;
  164. esac;;
  165. *)
  166. case "$2" in
  167. *[-.][zZ] | *_z | *[-.][gx]z | *[-.]bz2 | *[-.]lzma | *.t[abglx]z | *.tbz2 | *[-.]lzo | *.tzo | -)
  168. xz_status=$(
  169. exec 4>&1
  170. ($xz2 -cdfq -- "$2" 4>&-; echo $? >&4) 3>&- |
  171. eval "$cmp" '"$1"' - >&3
  172. );;
  173. *)
  174. eval "$cmp" '"$1"' '"$2"';;
  175. esac;;
  176. esac
  177. else
  178. echo >&2 "$0: Invalid number of operands; try \`${0##*/} --help' for help"
  179. exit 2
  180. fi
  181. cmp_status=$?
  182. test "$xz_status" -eq 0 || exit 2
  183. exit $cmp_status