]> git.sur5r.net Git - cc65/commitdiff
Added a getcwd function
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 12 Aug 2003 13:51:11 +0000 (13:51 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 12 Aug 2003 13:51:11 +0000 (13:51 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@2275 b7a2c559-68d2-44c3-8de9-860c34a00d81

libsrc/common/Makefile
libsrc/common/_cwd.s [new file with mode: 0644]
libsrc/common/chdir.s
libsrc/common/getcwd.s [new file with mode: 0644]

index de9459c4f06b8b61181eb62d1577da32a6f1f6f9..bd373b15809ab4ce7e5d0713e454303b1c75df11 100644 (file)
@@ -57,7 +57,8 @@ C_OBJS =      _afailed.o      \
                 vsscanf.o
 
 
-S_OBJS =       _fdesc.o        \
+S_OBJS =       _cwd.o          \
+                _fdesc.o       \
                _file.o         \
                _fopen.o        \
                _heap.o         \
@@ -86,6 +87,7 @@ S_OBJS =      _fdesc.o        \
                free.o          \
                fwrite.o        \
                getcpu.o        \
+                getcwd.o        \
                isalnum.o       \
                isalpha.o       \
                isblank.o       \
diff --git a/libsrc/common/_cwd.s b/libsrc/common/_cwd.s
new file mode 100644 (file)
index 0000000..c393aa8
--- /dev/null
@@ -0,0 +1,16 @@
+;
+; Ullrich von Bassewitz, 2003-08-12
+;
+; Place to store the current working directory.
+;
+
+               .export         __cwd
+        .export         __cwd_buf_size
+
+        __cwd_buf_size  = 64
+
+.bss
+
+__cwd:  .res   __cwd_buf_size
+
+
index 4a17d4910e6c87b242d751d59df5dd3c5fe4d7a4..0d2c6952add039f7ea4b5db695537e8809d94501 100644 (file)
@@ -11,6 +11,8 @@
 
 
 ;--------------------------------------------------------------------------
+; The function calls __syschdir, which must check the directory, set it, and
+; copy it to __cwd if it is valid. The copycwd may be used for the latter.
 
 .proc   _chdir
 
@@ -20,5 +22,3 @@
 .endproc
 
 
-
-
diff --git a/libsrc/common/getcwd.s b/libsrc/common/getcwd.s
new file mode 100644 (file)
index 0000000..824f4d5
--- /dev/null
@@ -0,0 +1,64 @@
+;
+; Ullrich von Bassewitz, 2003-08-12
+;
+; char* __fastcall__ getcwd (char* buf, size_t size);
+;
+
+        .export         _getcwd
+
+        .import         popax
+        .import         __cwd
+        .importzp       ptr1, ptr2
+
+        .include        "errno.inc"
+
+
+;--------------------------------------------------------------------------
+
+.proc   _getcwd
+
+; Remember -size-1 because this simplifies the following loop
+
+        eor     #$FF
+        sta     ptr2
+        txa
+        eor     #$FF
+        sta     ptr2+1
+
+        jsr     popax           ; Get buf
+        sta     ptr1
+        stx     ptr1+1          ; Save buf
+
+; Copy __cwd to the given buffer checking the length
+
+        ldy     #$00
+loop:   inc     ptr2
+        bne     @L1
+        inc     ptr2+1
+        beq     overflow
+
+; Copy one character, end the loop if the zero terminator is reached
+
+@L1:    lda     __cwd,y
+        sta     (ptr1),y
+        bne     loop
+
+; Current working dir copied ok, A contains zero
+
+        tax                     ; Return zero in a/x
+        rts
+
+; String overflow, return ERANGE
+
+overflow:
+        lda     #<ERANGE
+        sta     __errno
+        lda     #>ERANGE
+        sta     __errno+1
+        lda     #$FF
+        tax                     ; Return -1
+        rts
+
+.endproc
+
+