; LISTDUP
;
; Program for Proteus
;
; (C) 2002-2004 Simone Zanella Productions
;
; This program finds identical files with different names in the directory specified.
#!proteus -z
IF ISEMPTY(ARGV(5))
CONSOLELN "Syntax: proteus LISTDUP filespec"
CONSOLELN ""
CONSOLELN "Purpose: find identical files and display their names."
ABORT 0
FI
Path = SplitPath(ARGV(5))
H = DIROPEN(ARGV(5), 1)
F = IIF(NEQ(H, -1), 1, 0)
A = AVLNEW()
WHILE GT(F, 0)
AVLSET(A, DIRLAST(H, 1), ".")
F = DIRNEXT(H)
LOOP
DIRCLOSE(H)
B = AVLNEW()
AVLTRAVERSE(A, VerifyFile)
AVLTRAVERSE(B, PrintNames)
ABORT 0
FUNCTION PrintNames(crcdim, L)
IF GT(QUEUELEN(L), 1)
CONSOLELN "Duplicate: " crcdim
WHILE QUEUELEN(L)
CONSOLELN " " DEQUEUE(L)
LOOP
CONSOLELN ""
FI
RETURN 0
FUNCTION VerifyFile(nome, trash)
lfnsource = _Path nome
crc32 = CalculateCrc32(nome)
IF AVLBELONG(_B, crc32)
L = AVLGET(_B, crc32)
ENQUEUE(L, nome)
ELSE
L = QUEUENEW()
ENQUEUE(L, nome)
AVLSET(_B, crc32, L)
FI
RETURN 0
FUNCTION SplitPath(pathname)
P = STRRSTR(pathname, "\\")
IF NEQ(P, 0)
RETURN LEFT(pathname, P)
FI
RETURN ""
FUNCTION CalculateCrc32(filename)
; Returns 32 bit crc and file size
H = FOPEN(filename, 1)
crc32 = 0xFFFFFFFF
WHILE NOT(FEOF(H))
CRC32(FREAD(H, 2048), @crc32)
LOOP
size = FSIZE(H)
FCLOSE(H)
RETURN PADL(PFORMAT("lX", NNOT(crc32)), 8, "0") PADL(size, 10, "0")