]> git.sur5r.net Git - cc65/blobdiff - libsrc/cbm/close.s
no TGI_ERR_NO_MEM or TGI_ERR_NO_IOCB anymore: replaced by TGI_ERR_NO_RES
[cc65] / libsrc / cbm / close.s
index 0bca167bbba75700089b2c581d474aebe48172ac..989d8953ba23966c4e7add1ce700aee50a36ab76 100644 (file)
@@ -6,8 +6,10 @@
 
         .export         _close
 
-        .import         getdiskerror
-        .import         __errno, __oserror
+        .import         CLOSE
+        .import         readdiskerror, closecmdchannel
+        .import         __oserror
+        .importzp       tmp2
 
         .include        "errno.inc"
         .include        "cbm.inc"
         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
+        lda     fdtab,x         ; Get flags for this handle
+        and     #LFN_OPEN
         beq     notopen
 
 ; 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
+; Read the drive error channel, then close it
 
-        lda     unittab,x
-        tax
-        jsr     getdiskerror
-        cmp     #$00
-        bne     error
+        ldy     tmp2            ; Get the handle
+        ldx     unittab,y       ; Get teh 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
+        bne     error           ; Jump if error
 
 ; Successful
 
-        tax
+        tax                     ; Return zero in a/x
         rts
 
 ; Error entry, file descriptor is invalid