]> git.sur5r.net Git - cc65/blob - libsrc/common/getcwd.s
added optimization for indexed 16-bit array load of form (array[i & 0x7f])
[cc65] / libsrc / common / getcwd.s
1 ;
2 ; Ullrich von Bassewitz, 2003-08-12
3 ;
4 ; char* __fastcall__ getcwd (char* buf, size_t size);
5 ;
6
7         .export         _getcwd
8
9         .import         popptr1
10         .import         __cwd
11         .importzp       ptr1, ptr2
12
13         .include        "errno.inc"
14
15
16 ;--------------------------------------------------------------------------
17
18 .proc   _getcwd
19
20 ; Remember -size-1 because this simplifies the following loop
21
22         eor     #$FF
23         sta     ptr2
24         txa
25         eor     #$FF
26         sta     ptr2+1
27
28         jsr     popptr1         ; Get buf to ptr1
29
30 ; Copy __cwd to the given buffer checking the length
31
32         ; ldy     #$00          is guaranteed by popptr1
33 loop:   inc     ptr2
34         bne     @L1
35         inc     ptr2+1
36         beq     overflow
37
38 ; Copy one character, end the loop if the zero terminator is reached. We
39 ; don't support directories longer than 255 characters for now.
40
41 @L1:    lda     __cwd,y
42         sta     (ptr1),y
43         beq     done
44         iny
45         bne     loop
46
47 ; For some reason the cwd is longer than 255 characters. This should not
48 ; happen, we handle it as if the passed buffer was too short.
49 ;
50 ; String overflow, return ERANGE
51
52 overflow:
53         lda     #<ERANGE
54         jsr     __seterrno      ; Returns 0 in A
55         tax                     ; Return zero
56         rts                        
57
58 ; Success, return buf
59
60 done:   lda     ptr1
61         ldx     ptr1+1
62         rts
63
64 .endproc
65
66