home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / comptree < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  3.9 KB  |  151 lines

  1. #!/bin/ksh
  2. # @(#) comptree.ksh 1.2 96/03/01
  3. # 90/12/13 john h. dubois iii (john@armory.com)
  4. # 91/06/07 added help message.
  5. # 91/10/14 renamed from compare_files
  6. # 92/02/07 added check for whether root dirs are really dirs
  7. # 93/06/13 added pipes to valid file types
  8. # 93/06/17 Added -a test before executing l on a file.  Remove diff files.
  9. # 96/03/01 Cleaned up a bit.
  10.  
  11. name=${0##*/}
  12. Usage="Usage: $name [-hv] root1 root2 base < filelist"
  13. verbose=false
  14.  
  15. while getopts :hv opt; do
  16.     case $opt in
  17.     h)
  18.     print \
  19. "$name: compare files from two directory trees.
  20. $Usage
  21. $name assumes that files that exist in the two trees are at least of the
  22. same type.
  23. root1 is the root of the first directory tree;
  24. root2 is the root of the second directory tree;
  25. base is the basename of the output files.
  26. Each filename read from the standard input is concatenated to the
  27. two root names and the permissions, owner, group, and contents or
  28. major/minor number of the resulting files is compared.
  29. The filename is written to one or more files created by adding a
  30. suffix to base as follows:
  31. base.s: Same: ordinary files, directories, and devices that have identical
  32.         permissions, owner, and group, and identical contents or major/minor
  33.         number.
  34. base.d: Different: ordinary files that have different contents.
  35. base.i: Inode difference: files, directories, or devices that have different
  36.         permissions, owner, or group, or different major/minor number.
  37. base.a: Access denied: ordinary files whose contents could not be accessed
  38.         in one or both of the directory trees.
  39. base.1: 1st tree only: files that exist only in the first directory tree.
  40. base.2: 2nd tree only: files that exist only in the second directory tree.
  41. Options:
  42. -h: Print this help.
  43. -v: Print more detailed information to the .i file."
  44.     exit 0
  45.     ;;
  46.     v) verbose=true
  47.     ;;
  48.     +?)
  49.     print -u2 "$name: options should not be preceded by a '+'."
  50.     exit 1
  51.     ;;
  52.     :) 
  53.     print -r -u2 -- \
  54.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  55.     exit 1
  56.     ;;
  57.     ?)
  58.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  59.     exit 1
  60.     ;;
  61.     esac
  62. done
  63.  
  64. # remove args that were options
  65. let OPTIND=OPTIND-1
  66. shift $OPTIND
  67.  
  68. if [ $# -lt 3 ]; then
  69.     print -u2 "$Usage\nUse -h for help."
  70.     exit 1
  71. fi
  72.  
  73. root1="$1"
  74. root2="$2"
  75.  
  76. same="$3.s"
  77. diff="$3.d"
  78. diffi="$3.i"
  79. nofile1="$3.2"
  80. nofile2="$3.1"
  81. noaccess="$3.a"
  82.  
  83. rm -f "$same" "$diff" "$diffi" "$nofile1" "$nofile2" "$noaccess"
  84.  
  85. if [ ! -d "$root1" ]; then
  86.     print -r -u2 -- "$root1 is not a directory."
  87.     exit 1
  88. fi
  89.  
  90. if [ ! -d "$root2" ]; then
  91.     print -r -u2 -- "$root2 is not a directory."
  92.     exit 1
  93. fi
  94.  
  95. unset ENV
  96.  
  97. function ChkFile {
  98.     if [ -a "$1" ] && set -- $(/bin/l -d -- "$1" 2> /dev/null); then
  99.     print -r -- "$1" "$3" "$4" "$5" "$5$6"
  100.     return 0
  101.     else
  102.     return 1
  103.     fi
  104. }
  105.  
  106. while read f; do
  107.     idiff=false
  108.     file1="$root1/$f"
  109.     file2="$root2/$f"
  110.     if ! ChkFile "$file1" | read perms1 owner1 group1 size1 dev1; then
  111.     print -r -- "$f" >> "$nofile1"
  112.     continue
  113.     fi
  114.     if ! ChkFile "$file2" | read perms2 owner2 group2 size2 dev2; then
  115.     print -r -- "$f" >> "$nofile2"
  116.     continue
  117.     fi
  118.  
  119.     if [ "$perms1" != "$perms2" -o "$owner1" != "$owner2" -o \
  120.     "$group1" != "$group2" ]; then
  121.     $verbose && print -r -n -- \
  122.     "$perms1 $perms2 $owner1\t$owner2\t$group1\t$group2\t" >> "$diffi"
  123.     print -r -- "$f" >> "$diffi"
  124.     idiff=true
  125.     fi
  126.  
  127.     if [ -c "$file1" -o -b "$file1" ]; then
  128.     if ! $idiff; then
  129.         if [ "$dev1" != "$dev2" ]; then
  130.         $verbose && print -r -n -- "$dev1\t$dev2\t" >> "$diffi"
  131.         print -r -- "$f" >> "$diffi"
  132.         else
  133.         print -r -- "$f" >> "$same"
  134.         fi
  135.     fi
  136.     elif [ -f "$file1" ]; then
  137.     cmp -s -- "$file1" "$file2"
  138.     case $? in
  139.         0) $idiff || print -r -- "$f" >> "$same";;
  140.         1) print -r -- "$f" >> "$diff";;
  141.         2) print -r -- "$f" >> "$noaccess";;
  142.         *) print -r "Invalid return code for from cmp: $? (comparing $f)."
  143.         ;;
  144.     esac
  145.     elif [ -d "$file1" -o -p "$file1" ]; then
  146.     $idiff || print -r -- "$f" >> "$same"
  147.     else
  148.     print -r "Invalid file type for $file1."
  149.     fi
  150. done
  151.