;*---------------------------------------------------------------------------
;  :Program.	fcmp.asm
;  :Contents.	compares two files quick
;  :Author.	Bert Jahn
;  :EMail.	wepl@whdload.org
;  :Address.	Franz-Liszt-Straße 16, Rudolstadt, 07404, Germany
;  :Version.	$Id: fcmp.asm 1.4 2000/09/24 15:17:13 jah Exp jah $
;  :History.	04.08.00 initial
;		06.08.00 major bug in compare fixed
;		07.08.00 option NOT/S added
;  :Requires.	OS V37+
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly 2.x
;---------------------------------------------------------------------------*
;##########################################################################

	INCDIR	Includes:
	INCLUDE	lvo/exec.i
	INCLUDE	lvo/dos.i
	INCLUDE	dos/dos.i
	INCLUDE	exec/libraries.i
	INCLUDE	exec/memory.i
	
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

GL	EQUR	A4		;a4 ptr to Globals
LOC	EQUR	A5		;a5 for local vars

BUFLEN	= 16384

	STRUCTURE	Globals,0
		APTR	gl_execbase
		APTR	gl_dosbase
		APTR	gl_rdargs
		LABEL	gl_rdarray
		ULONG	gl_arg_not
		ULONG	gl_arg_file1
		ULONG	gl_arg_file2
		ULONG	gl_len
		STRUCT	gl_buf1,BUFLEN
		STRUCT	gl_buf2,BUFLEN
		LABEL	gl_SIZEOF

;##########################################################################

	PURE

		bra	_start
		dc.b	"$VER: fcmp "
	DOSCMD	"WDate >t:date"
	INCBIN	"t:date"
		dc.b	" by Wepl"
		dc.b	" V37+",0
_dosname	dc.b	"dos.library",0
_template	dc.b	"NOT/S"			;return 0 if not equal
		dc.b	",FILE1/A"		;name eines zu ladenden Files
		dc.b	",FILE2/A"		;savefile name
		dc.b	0
	CNOP 0,2
_start

;##########################################################################

		moveq	#20,d7			;default rc

		move.l	#gl_SIZEOF,d0
		move.l	#MEMF_CLEAR,d1
		move.l	(4),a6
		cmp.w	#37,(LIB_VERSION,a6)
		blo	.pre37
		jsr	(_LVOAllocVec,a6)
		tst.l	d0
		beq	.nomem
		move.l	d0,GL
		move.l	a6,(gl_execbase,GL)
		
		move.l	#37,d0
		lea	(_dosname),a1
		jsr	(_LVOOpenLibrary,a6)
		move.l	d0,(gl_dosbase,GL)
		beq	.nodoslib

		lea	(_template),a0
		move.l	a0,d1
		lea	(gl_rdarray,GL),a0
		move.l	a0,d2
		moveq	#0,d3
		move.l	(gl_dosbase,GL),a6
		jsr	(_LVOReadArgs,a6)
		move.l	d0,(gl_rdargs,GL)
		beq	.noargs
		
		move.l	(gl_arg_file1,GL),d1
		bsr	_open
		move.l	d0,(gl_len,GL)
		bmi	.nofh1
		move.l	d4,d5				;d5 = fh1
	
		move.l	(gl_arg_file2,GL),d1
		bsr	_open
		tst.l	d0
		bmi	.nofh2
		move.l	d4,d6				;d6 = fh2

		cmp.l	(gl_len,GL),d0
		bne	.diff

.loop		move.l	(gl_len,GL),d3
		cmp.l	#BUFLEN,d3
		blo	.1
		move.l	#BUFLEN,d3
.1
		move.l	d5,d1
		lea	(gl_buf1,GL),a0
		move.l	a0,d2
		jsr	(_LVORead,a6)
		cmp.l	d0,d3
		bne	.readerr

		move.l	d6,d1
		lea	(gl_buf2,GL),a0
		move.l	a0,d2
		jsr	(_LVORead,a6)
		cmp.l	d0,d3
		bne	.readerr
		
		lea	(gl_buf1,GL),a0
		lea	(gl_buf2,GL),a1
		move.l	d3,d0
		addq.l	#3,d0				;round up
		lsr.l	#2,d0
		sub.w	#1,d0
.cmp		cmp.l	(a0)+,(a1)+
		dbne	d0,.cmp
		bne	.diff
		
		sub.l	d3,(gl_len,GL)
		bne	.loop
		
		moveq	#0,d7
		bra	.equ

.diff		moveq	#1,d7
.equ
.readerr
		move.l	d6,d1
		jsr	(_LVOClose,a6)
.nofh2
		move.l	d5,d1
		jsr	(_LVOClose,a6)
.nofh1
		tst.l	(gl_arg_not,GL)
		beq	.2
		tst.l	d7
		seq	d7
		and.l	#1,d7
.2
		move.l	(gl_rdargs,GL),d1
		jsr	(_LVOFreeArgs,a6)
.noargs
		move.l	(gl_dosbase,GL),a1
		move.l	(gl_execbase,GL),a6
		jsr	(_LVOCloseLibrary,a6)
.nodoslib
		move.l	GL,a1
		move.l	(gl_execbase,GL),a6
		jsr	(_LVOFreeVec,a6)
.nomem
.pre37		move.l	d7,d0
		rts

;##########################################################################

_open
		move.l	#MODE_OLDFILE,d2
		jsr	(_LVOOpen,a6)
		move.l	d0,d4
		beq	.nofh
		
		move.l	d4,d1
		moveq	#0,d2				;offset
		move.l	#OFFSET_END,d3			;mode
		jsr	(_LVOSeek,a6)
		tst.l	d0
		bmi	.noseek

		move.l	d4,d1
		moveq	#0,d2				;offset
		move.l	#OFFSET_BEGINNING,d3		;mode
		jsr	(_LVOSeek,a6)
		tst.l	d0
		bmi	.noseek
		
		rts
		
.noseek		move.l	d4,d1
		jsr	(_LVOClose,a6)
.nofh		moveq	#-1,d0
		rts

;##########################################################################

	END

