	.include "os9.d"
	.global	errno

	.text

	.global	open
	; int open(const char *,int)
open:
	tfr	d,x
	lda	3,s
	os9	I$Open
	bcs	err
	tfr	a,b
	clra
	rts


	.global	close
	; int close(int)
close:
	tfr	b,a
	os9	I$Close
	bcs	err
	rts
err:
	stb	<errno+1
	ldd	#-1
	rts


	.global	creat
	; int creat(const char *,int)
creat:
	pshs	y
	tfr	d,y
.1:	leax	,y
	lda	5,s
	ldb	#S_IOREAD|S_IWRITE|S_IREAD  ; that seems to be the default?
	os9	I$Create
	bcs	.2
	tfr	a,b
	clra
	puls	y,pc

	; WTF!? I$Create cannot recreate an existing file?
.2:	cmpb	#218			; File exists?
	bne	erry
	lda	5,s
	leax	,y
	os9	I$DeletX		; delete and try again
	bcs	erry
	bra	.1


	.global	read
	; int read(int,void *,int)
read:
	pshs	y
	ldx	4,s
	ldy	6,s
	tfr	b,a
	os9	I$Read
	bcs	erry
	tfr	y,d
	puls	y,pc


	.global	write
	; int write(int,void *,int)
write:
	pshs	y
	ldx	4,s
	ldy	6,s
	tfr	b,a
	os9	I$Write
	bcs	erry
	tfr	y,d
	puls	y,pc

erry:
	stb	<errno+1
	ldd	#-1
	puls	y,pc
