; ReadDir.a
; Simple functions to enumerate directories
; (c) Musus Umbra, 1997


; Offsets of fields of a readdir_block

rdb_dir		= 0
rdb_buffer	= 4
rdb_read	= 8
rdb_start	= 12
rdb_size	= 16
rdb_spec	= 20


; Offsets within a readdir_record

rdr_name	= 20


	AREA	C$$Code , CODE , READONLY

	EXPORT	ReadDir
	; os_error *Read_Dir( int full, readdir_block *b )

ReadDir:
	stmfd	sp!,{r4-r7,lr}
	mov	r7,r1			; we'll need this later
	ldr	r6,[r1,#rdb_spec]	; name to match
	ldr	r5,[r1,#rdb_size]	; buffer size
	ldr	r4,[r1,#rdb_start]	; continuation point
	ldr	r3,[r1,#rdb_read]	; entries to read
	ldr	r2,[r1,#rdb_buffer]	; buffer
	ldr	r1,[r1,#rdb_dir]	; director to read
	cmp	r0,#0			; full ?
	moveq	r0,#9			; if not, use GBPB 9
	movne	r0,#10			; else use 10
	swi	"XOS_GBPB"		; do the swi
	movvc	r0,#0			; if OK, no error
	str	r3,[r7,#rdb_read]	; # entries read
	str	r4,[r7,#rdb_start]	; continuation point
	ldmfd	sp!,{r4-r7,pc}^		; byeee!


	EXPORT	next_record
	EXPORT	next_objname
	; readdir_record *next_record( readdir_record *current )
	; char *next_objname( char *current )

next_record:
	add	r0,r0,#rdr_name		; skip to the name part
nr1:	ldrb	r1,[r0],#1		; get character
	cmp	r1,#0			; terminator?
	bne	nr1			; loop until it bloody well is.
	add	r0,r0,#3		; align (r0->byte after terminator)
	bic	r0,r0,#3		; like so.
	movs	pc,lr			; bye!



next_objname:
	ldrb	r1,[r0],#1		; get character
	cmp	r1,#0			; terminator?
	bne	next_objname		; loop until it bloody well is.

