	.include "os9.d"
	.global	errno

	.text

	.global	sbrk
	; char *sbrk(int)
sbrk:
	pshs	y
	addd	datasz,u		; d: new data segment size
	os9	F$Mem
	bcs	.1
	std	datasz,u
	ldd	datatop,u		; return old upper bound
	sty	datatop,u		; set new upper bound offset
	puls	y,pc

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


	.global	_INIT_1_sbrk
_INIT_1_sbrk:
	pshs	y
	clra
	clrb
	os9	F$Mem			; get current upper bound and size
	bcs	.1
	std	datasz,u
	sty	datatop,u		; initial upper bound
	puls	y,pc
.1:	os9	F$Exit			; shouldn't happen


	.bss

datatop:
	.space	2
datasz:
	.space	2
