]> git.sur5r.net Git - cc65/blob - libsrc/common/getcwd.s
The IRQ handler needs to save the registers. By Stefan Haubenthal.
[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         popax
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     popax           ; Get buf
29         sta     ptr1
30         stx     ptr1+1          ; Save buf
31
32 ; Copy __cwd to the given buffer checking the length
33
34         ldy     #$00
35 loop:   inc     ptr2
36         bne     @L1
37         inc     ptr2+1
38         beq     overflow
39
40 ; Copy one character, end the loop if the zero terminator is reached. We
41 ; don't support directories longer than 255 characters for now.
42
43 @L1:    lda     __cwd,y
44         sta     (ptr1),y
45         beq     done
46         iny
47         bne     loop
48
49 ; For some reason the cwd is longer than 255 characters. This should not
50 ; happen, we handle it as if the passed buffer was too short.
51 ;
52 ; String overflow, return ERANGE
53
54 overflow:
55         lda     #<ERANGE
56         jsr     __seterrno      ; Returns 0 in A
57         tax                     ; Return zero
58         rts                        
59
60 ; Success, return buf
61
62 done:   lda     ptr1
63         ldx     ptr1+1
64         rts
65
66 .endproc
67
68