include	clib.inc
include	io.inc
include	iost.inc
include	errno.inc
include	conio.inc
include syserrls.inc

PPROC	oopen,	filename:dword, mode:word
	push	si
	push	di
	mov	di,mode
	test	di,di
	jnz	oopen_00
	invoke	openfile, filename, M_RDONLY, A_OPEN
	jmp	oopen_01
    oopen_00:
	invoke	ogetouth, filename
    oopen_01:
	mov	si,ax
	cmp	si,1
	jl	oopen_03
	test	di,di
	jnz	oopen_02
	invoke	oinitst,addr STDI,STDI.ios_size
	jz	oopen_05
	mov	STDI.ios_file,si
	jmp	oopen_03
    oopen_02:
	invoke	oinitst,addr STDO,STDO.ios_size
	jz	oopen_05
	mov	STDO.ios_file,si
    oopen_03:
	mov	ax,si
    oopen_04:
	pop	di
	pop	si
	ret
    oopen_05:
	invoke	close, si
	mov	ax,offset CP_ENOMEM
	call	@ermsg
	dec	ax
	jmp	oopen_04
PEND	oopen

	END
