home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / compare.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  61 lines

  1. ############################################################################
  2. #
  3. #    File:     compare.icn
  4. #
  5. #    Subject:  Program to look for duplicates in a collection of files
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     January 7, 1997
  10. #
  11. ############################################################################
  12. #
  13. #  This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program compares files to locate ones that have the same content.
  18. #
  19. #  The file names are given on the command line.
  20. #
  21. #  This program has impossible time complexity if there are many files
  22. #  of the same size.
  23. #
  24. ############################################################################
  25. #
  26. #  Requires:  UNIX
  27. #
  28. ############################################################################
  29.  
  30. procedure main(args)
  31.    local filesets, filelist, file, xfile, size, line, input
  32.  
  33.    filesets := table()
  34.  
  35.    #  The strategy is to divide the files into equivalence classes by size.
  36.  
  37.    every file := !args do {
  38.       input := open("wc " || image(file), "p")
  39.       line := read(input)
  40.       close(input)
  41.       line ? {
  42.          move(20)
  43.          tab(many(' '))
  44.          size := integer(tab(many(&digits))) | stop("bogus size")
  45.          }
  46.       /filesets[size] := []
  47.       put(filesets[size], file)
  48.       }
  49.  
  50.    filesets := sort(filesets, 3)
  51.  
  52.    while get(filesets) do {            # don't need size for anything
  53.       filelist := get(filesets)            # just the files of that size
  54.       while file := get(filelist) do        # for every file
  55.          every xfile := !filelist do         # compare against the rest
  56.             if system("cmp -s " || image(file) || " " || image(xfile) ||
  57.                ">/dev/null") = 0 then write(file, "==", xfile)
  58.       }
  59.    
  60. end
  61.