.export _getcwd
- .import popax
+ .import popptr1
.import __cwd
.importzp ptr1, ptr2
eor #$FF
sta ptr2+1
- jsr popax ; Get buf
- sta ptr1
- stx ptr1+1 ; Save buf
+ jsr popptr1 ; Get buf to ptr1
; Copy __cwd to the given buffer checking the length
- ldy #$00
+ ; ldy #$00 is guaranteed by popptr1
loop: inc ptr2
bne @L1
inc ptr2+1
beq overflow
-; Copy one character, end the loop if the zero terminator is reached
+; Copy one character, end the loop if the zero terminator is reached. We
+; don't support directories longer than 255 characters for now.
@L1: lda __cwd,y
sta (ptr1),y
+ beq done
+ iny
bne loop
-; Current working dir copied ok, A contains zero
-
- tax ; Return zero in a/x
- rts
-
+; For some reason the cwd is longer than 255 characters. This should not
+; happen, we handle it as if the passed buffer was too short.
+;
; String overflow, return ERANGE
overflow:
lda #<ERANGE
- sta __errno
- lda #>ERANGE
- sta __errno+1
- lda #$FF
- tax ; Return -1
+ jsr __seterrno ; Returns 0 in A
+ tax ; Return zero
+ rts
+
+; Success, return buf
+
+done: lda ptr1
+ ldx ptr1+1
rts
.endproc