include libmacro.inc

beglib

; BUG-FIX 
; 13.05.00: исправлена ошибка в процедуре fcreate: регистр cx не занулялся }

item	'fattr'
	push	ax
	mov	ax, 15h
item	'fname'
	push	ax
	mov	ax, 1Eh

item	'findfile','*',0,0
;	findfile ( @file_rec )
	mov	di, ax
;set DTA	
	mov	dx, ax
	mov	ah, 1Ah
	int	21h
;
	mov	ah, 4Eh
	mov	dx, di
	add	dx, 1Eh
	mov	cl, [di+15h]
	sub	ch, ch
	int	21h	
;	
	setnc	al

item	'findnext','*',0,0
;	findnext ( @file_rec )
	mov	di, ax
;set DTA	
	mov	dx, ax
	mov	ah, 1Ah
	int	21h
;
	mov	ah, 4Fh
	int	21h	
;	
	setnc	al

item	'fmkdir','*'	; make dir 
	mov	dx, ax
	mov	ah, 39h
	int	21h
	pop	ax
mkdirExit:

item	'frmdir','*'	; remove dir 
	mov	dx, ax
	mov	ah, 3Ah
	int	21h
	pop	ax
rmdirExit:

item	'fchdir','*'	; set def dir }
	mov	dx, ax
	mov	ah, 3Bh
	int	21h
	pop	ax
chdirExit:

item	'fgetdir','*' ; get def dir }
	mov	ah, 47h
	xor	dl, dl
	pop	ax
	push	si
	mov	si, ax
	int	21h
	jnc	getdirExit
	mov	dx, ax
getdirExit:
	pop	si

item	'fcreate','*'
	mov	dx, ax
	push	cx
	mov	ah, 3Ch
	xor	cx, cx
	int	21h
	pop	cx
	
item	'fopen','*'
	mov	dx, ax
	mov	ah, 3Dh
	mov	al, 2
	int	21h


item	'fclose','*'	; close file }
	mov	bx, ax
	mov	ah, 3Eh
	int	21h
	pop	ax

item	'fread','*'	; read from file }
	xchg	cx, ax
	pop	dx
	pop	bx
	push	ax
	mov	ah, 3Fh
	int	21h
	pop	cx
	setnc	al

item	'fwrite','*'	; write to file }
	xchg	cx, ax
	pop	dx
	pop	bx
	push	ax
	mov	ah, 40h
	int	21h
	pop	cx
	setnc	al

item	'ftruncate','*' ; truncate file }
	push	cx
	xor	cx, cx
	mov	bx, ax
	mov	ah, 40h
	int	21h
	pop	cx

item	'fdelete','*'
	mov	dx, ax
	mov	ah, 41h
	int	21h
	pop	ax

item	'fseek','*'
	mov	dx, ax
	xchg	cx, ax
	pop	cx
	pop	bx
	push	ax
	mov	ax, 4200h
	int	21h
	pop	cx
	pop	ax

item	'fseekto','*'
	mov	dx, ax
	xchg	cx, ax
	pop	cx
	pop	bx
	push	ax
	mov	ax, 4201h
	int	21h
	pop	cx
fseektoExit:

item	'fduplicate','*'	; dup file handle }
	mov	bx, ax
	mov	ah, 45h
	int	21h

item	'fredirect','*'	;{ redirect file handle }
	xchg	cx, ax
	pop	bx
	push	ax
	mov	ah, 46h
	int	21h
	pop	cx
fredirectExit:


item	'errormsg','*'
	call	doit

RELO	DW msg01H-RELO, msg02H-RELO, msg03H-RELO, msg04H-RELO,
	DW msg05H-RELO, msg06H-RELO, msg07H-RELO, msg08H-RELO,
	DW msg09H-RELO, msg0aH-RELO, msg0bH-RELO, msg0cH-RELO,
	DW msg0dH-RELO, msg0eH-RELO, msg0fH-RELO, msg10H-RELO,
	DW msg11H-RELO, msg12H-RELO

msg01H:   db '  Invalid function number'      ,10,13,'$'
msg02H:   db '  File not found'               ,10,13,'$'
msg03H:   db '  Path not found'               ,10,13,'$'      
msg04H:   db '  Too many open files'          ,10,13,'$' 
msg05H:   db '  Access denied'                ,10,13,'$'       
msg06H:   db '  Invalid handle'               ,10,13,'$'      
msg07H:   db '  Memory ctrl blocks destroyed' ,10,13,'$'
msg08H:   db '  Insufficient memory'          ,10,13,'$'           
msg09H:   db '  Invalid memory block address' ,10,13,'$'
msg0aH:   db '  Invalid environment'          ,10,13,'$'
msg0bH:   db '  Invalid format'               ,10,13,'$'
msg0cH:   db '  Invalid access code'          ,10,13,'$'
msg0dH:   db '  Invalid data'                 ,10,13,'$'
msg0eH:   db '  (not used)'                   ,10,13,'$'
msg0fH:   db '  Invalid drive specified'      ,10,13,'$'
msg10H:   db '  Can not remove current dir'   ,10,13,'$'
msg11H:   db '  Not same device'              ,10,13,'$'
msg12H:   db '  No more matching files'       ,10,13,'$'

doit:	pop	bx	; offset of RELO
	mov	dx, bx
	dec	ax
	shl	ax, 1
	add	bx, ax
	add	dx, [bx]
	mov	ah, 9
	int	21h
	pop	ax

endlib
	