;*********
;GET_FILES
;loads file_list table with info about the files in directory
;the structure of the info is following:
;	xxxxxxxx.xxxZ	13 bytes file name ASCIIZ string
;	00		1  byte reserved
;	00		1  byte reserved
;	?		1  byte file attribs 		
;	xxxx		4  bytes file size
; one file has 20 bytes record
; the rutine works within one segment CS=ES=DS 
; (I hope someone will adapt this routine for all segments range)
;
;INPUT:		CL	- file attributes
;		CH	- maximum of files to load into the table (1-254)
;		DX	- pointer to file spec. ASCIIZ
;		SI	- pointer to DTA (usually 080 hexa)
;		DI	- pointer to file_list table
;	
;OUTPUT:	CY	- set - error	
;			  AX  - error code
;		CY	- cleared - OK
;			  AX  - number of files in the file_list table
;
;DESTROYS:	flags
;		AX
;----------------------------------

GET_FILES:

	PUSH BX,CX,DX,SI,DI

	CLD
	PUSH	CX			;saves CX
	MOV 	CH,0
	MOV 	AH,04E
	INT 	021			;the first file
	POP	CX			;restores CX

 	JC	GF_ERR0			;error?

	PUSH	CX			;saves CX	
	CALL 	GF_SAVE			;process the info about the file
	POP	CX			;restores CX
 	MOV 	DL,1			;first file loaded

	XCHG	CH,CL
	MOV 	CH,0			;CX - max of files (for looping)

GF1:
	MOV 	AH,04F
	INT 	33			;get another file

 	JC 	GF_ERR
	ADD 	DI,20	;20 bytes per 1 file

	PUSH 	CX,DX
	CALL 	GF_SAVE
	POP 	DX,CX
	INC 	DL
	LOOP 	GF1

	;THIS MEANS THERE ARE MORE THAN 'CH' (on imput) FILES FOUND

GF2:
	CLC				;CLC no problem
	MOV AL,DL
	MOV AH,0			;loads AX with total of got files

GF3:
	POP DI,SI,DX,CX,BX
	RET				;exit the rutine

GF_SAVE:
	MOV 	BX,SI	;stores SI	
	MOV 	DX,DI	;stores DI

	mov	al,0
	mov	cx,14
	rep	stosb	;deletes previsous data from file_list

	mov	di,dx	;restores di

	ADD 	SI,01e	;si - pointer to file name
GF_S1:
	LODSB
	STOSB
	CMP 	AL,0	;end of file name?
	JNE 	GF_S1

	MOV 	SI,BX	;restores SI
	MOV 	DI,DX	;restores DI

	ADD 	SI,015	;si - pointer to file attribs
	ADD 	DI,0D
	MOV 	B[DI],0	;this byte will be used later
	INC 	DI
	MOV 	b[di],0	;this byte will be used later
	INC	di

	MOVSB

	MOV 	SI,BX
	ADD 	SI,01A	;si - pointer to file size
	MOV 	CX,4
	REP 	MOVSB

	MOV 	SI,BX
	MOV 	DI,DX
	RET 

GF_ERR0:
	MOV 	DL,0		;sets number of found files to 0
GF_ERR:
	
;	push 	ax		;this makes a blank record after
;	MOV	al,0		;the last record (not always needed)
;	add	di,20		;similar to zero terminated blocks
;	mov     cx,20
;       cld
;       rep stosb
;	pop	ax		

	CMP 	AX,012		;"last file" error
	JE 	GF2	
	STC			;STC to indicate the failture of the rutine
	JMP 	NEAR GF3

;-- end of rutine ------------------------------------------------------
