	Move.W	#CFGOPENERR,_ErrorCode
	Lea	_ConfigHandle(PC),A4

	Move.L	_Config(PC),A3
	Cmp.L	#0,A3
	Beq.S	.UseDefConfig

	Move.L	A3,_CfgErr+4
	Move.L	A3,_CfgErr+4+SIZEOFERR
	Move.L	A3,_CfgErr+4+(SIZEOFERR*2)
	Bra.S	.OpenConfig

.UseDefConfig	Lea	IDerPrefs(PC),A3

	Move.L	A3,D1
	Addq	#4,D1		;try current directory
	Move.L	#MODE_OLDFILE,D2
	CALL	Open,DOS
	Move.L	D0,(A4)
	Bne.S	.ConfigFound

	Move.L	A3,D1		;try ENV:
	Move.L	#MODE_OLDFILE,D2
	CALL	Open
	Move.L	D0,(A4)
	Bne.S	.ConfigFound

	Addq	#2,A3
	Move.B	#'S',(A3)		;try S:
.OpenConfig	Move.L	A3,D1
	Move.L	#MODE_OLDFILE,D2
	CALL	Open,DOS
	Move.L	D0,(A4)
	Beq	CloseDown

.ConfigFound	Move.L	D0,D1
	Moveq	#0,D2
	Moveq	#OFFSET_END,D3		;end of the file
	CALL	Seek

	Move.L	(A4),D1
	Moveq	#0,D2
	Moveq	#OFFSET_BEGINNING,D3	;start of the file
	CALL	Seek

	Tst.L	D0		;file size (difference between end and start)
	Beq	CloseDown

	Addq	#1,D0		;we need a blank byte at the end for saftey
	Move.L	D0,_ConfigSize

	Move.W	#CFGMEMERR,_ErrorCode
	Move.L	#MEMF_CLEAR,D1		;the memory has to be clean for use to use it properly
	CALL	AllocMem,EXEC
	Move.L	D0,_ConfigMem
	Beq	CloseDown

	Move.L	(A4),D1
	Move.L	_ConfigMem(PC),D2
	Move.L	_ConfigSize(PC),D3
	CALL	Read,DOS

	Move.W	#CFGREADERR,_ErrorCode

	Move.L	_ConfigSize(PC),D1
	Addq	#1,D0		;make up for the extra byte
	Sub.L	D0,D1
	Bne	CloseDown

	Move.L	(A4),D1
	CALL	Close
	Clr.L	(A4)		;the closedown routine would try to close it again if we don't clean up

	Move.L	_ConfigMem(PC),_ConfigCurPos	;setup the current position indicator of the config file, as we are going to go walking through it
	Clr.W	_ErrorCode
