]> git.sur5r.net Git - cc65/blobdiff - libsrc/cbm/close.s
Renamed INITBSS to INIT and INIT to ONCE.
[cc65] / libsrc / cbm / close.s
index 0bca167bbba75700089b2c581d474aebe48172ac..004b88df9d3dc794cb63dc8d72d6a766e1e049a2 100644 (file)
@@ -6,8 +6,9 @@
 
         .export         _close
 
-        .import         getdiskerror
-        .import         __errno, __oserror
+        .import         CLOSE
+        .import         readdiskerror, closecmdchannel
+        .importzp       tmp2
 
         .include        "errno.inc"
         .include        "cbm.inc"
@@ -16,7 +17,7 @@
 
 ;--------------------------------------------------------------------------
 ; _close
-
+                                                   
 .proc   _close
 
 ; Check if we have a valid handle
         bne     invalidfd
         cmp     #MAX_FDS        ; Is it valid?
         bcs     invalidfd       ; Jump if no
+        sta     tmp2            ; Save the handle
 
-; Check if the LFN is valid and the file is open for writing
+; Check if the file is actually open
 
-        adc     #LFN_OFFS       ; Carry is already clear
         tax
-        lda     fdtab-LFN_OFFS,x; Get flags for this handle
-        beq     notopen
+        lda     fdtab,x         ; Get flags for this handle
+        and     #LFN_OPEN
+        beq     invalidfd
 
 ; Valid lfn, close it. The close call is always error free, at least as far
 ; as the kernal is involved
 
         lda     #LFN_CLOSED
-        sta     fdtab-LFN_OFFS,x
-        lda     unittab,x
-        pha                     ; Push unit for this file
-        txa
+        sta     fdtab,x
+        lda     tmp2            ; Get the handle
+        clc
+        adc     #LFN_OFFS       ; Make LFN from handle
         jsr     CLOSE
-        pla
-
-; Read the drive error channel
 
-        lda     unittab,x
-        tax
-        jsr     getdiskerror
-        cmp     #$00
-        bne     error
+; Read the drive error channel, then close it
 
-; Successful
-
-        tax
-        rts
+        ldy     tmp2            ; Get the handle
+        ldx     unittab,y       ; Get the disk for this handle
+        jsr     readdiskerror   ; Read the disk error code
+        pha                     ; Save it on stack
+        ldy     tmp2
+        ldx     unittab,y
+        jsr     closecmdchannel ; Close the disk command channel
+        pla                     ; Get the error code from the disk
+        jmp     __mappederrno   ; Set _oserror and _errno, return 0/-1
 
-; Error entry, file descriptor is invalid
+; Error entry: The given file descriptor is not valid or not open
 
 invalidfd:
-        lda     #EINVAL
-        sta     __errno
-        lda     #0
-        sta     __errno+1
-        beq     errout
-
-; Error entry, file is not open
-
-notopen:
-        lda     #3              ; File not open
-        bne     error
-
-; Error entry, status not ok
-
-error:  sta     __oserror
-errout: lda     #$FF
-        tax                     ; Return -1
-        rts
+        lda     #EBADF
+        jmp     __directerrno   ; Set _errno, clear _oserror, return -1
 
 .endproc