]> git.sur5r.net Git - cc65/blob - libsrc/cbm/close.s
Shortenned the CBM close() by a byte and a cycle.
[cc65] / libsrc / cbm / close.s
1 ;
2 ; Ullrich von Bassewitz, 16.11.2002
3 ;
4 ; int __fastcall__ close (int fd);
5 ;
6
7         .export         _close
8
9         .import         CLOSE
10         .import         readdiskerror, closecmdchannel
11         .importzp       tmp2
12
13         .include        "errno.inc"
14         .include        "cbm.inc"
15         .include        "filedes.inc"
16
17
18 ;--------------------------------------------------------------------------
19 ; _close
20                                                    
21 .proc   _close
22
23 ; Check if we have a valid handle
24
25         cpx     #$00
26         bne     invalidfd
27         cmp     #MAX_FDS        ; Is it valid?
28         bcs     invalidfd       ; Jump if no
29         sta     tmp2            ; Save the handle
30
31 ; Check if the file is actually open
32
33         tax
34         lda     fdtab,x         ; Get flags for this handle
35         and     #LFN_OPEN
36         beq     invalidfd
37
38 ; Valid lfn, close it. The close call is always error free, at least as far
39 ; as the kernal is involved
40
41         lda     #LFN_CLOSED
42         sta     fdtab,x
43         txa                     ; Get handle
44         clc
45         adc     #LFN_OFFS       ; Make LFN from handle
46         jsr     CLOSE
47
48 ; Read the drive error channel, then close it
49
50         ldy     tmp2            ; Get the handle
51         ldx     unittab,y       ; Get the disk for this handle
52         jsr     readdiskerror   ; Read the disk error code
53         pha                     ; Save it on stack
54         ldy     tmp2
55         ldx     unittab,y
56         jsr     closecmdchannel ; Close the disk command channel
57         pla                     ; Get the error code from the disk
58         jmp     __mappederrno   ; Set _oserror and _errno, return 0/-1
59
60 ; Error entry: The given file descriptor is not valid or not open
61
62 invalidfd:
63         lda     #EBADF
64         jmp     __directerrno   ; Set _errno, clear _oserror, return -1
65
66 .endproc
67
68
69
70