]> git.sur5r.net Git - cc65/blob - libsrc/common/getcwd.s
Added an IRQ vector
[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         sta     __errno
57         lda     #>ERANGE
58         sta     __errno+1
59         tax                     ; High byte of ERANGE is zero, return zero
60         rts
61  
62 ; Success, return buf
63
64 done:   lda     ptr1
65         ldx     ptr1+1
66         rts
67
68 .endproc
69
70