include	clib.inc
include	io.inc
include	stdio.inc

PPROC	fclose, FP:DPTR
	push	si
	xor	si,si
	MLES	bx,FP
	assert	bx,si,jne,"fclose"
	dec	si
	mov	ax,es:[bx.iob_flag]
	and	ax,_IOREAD or _IOWRT or _IORW
	jz	fclose_01
	push	es:[bx.iob_file]
	push	es
	push	bx
	push	es
	push	bx
	call	fflush
	mov	si,ax
	call	_freebuf
	call	close
	cmp	ax,0
	jge	fclose_00
	mov	si,-1
    fclose_00:
	MLES	bx,FP
	xor	ax,ax
	mov	es:[bx.iob_flag],ax
	dec	ax
	mov	es:[bx.iob_file],ax
    fclose_01:
	mov	ax,si
	pop	si
	ret
PEND	fclose

	END
