]> git.sur5r.net Git - cc65/blob - libsrc/cbm/close.s
Dito for the enhanced apple2
[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         .import         __oserror
12         .importzp       tmp2
13
14         .include        "errno.inc"
15         .include        "cbm.inc"
16         .include        "filedes.inc"
17
18
19 ;--------------------------------------------------------------------------
20 ; _close
21
22 .proc   _close
23
24 ; Check if we have a valid handle
25
26         cpx     #$00
27         bne     invalidfd
28         cmp     #MAX_FDS        ; Is it valid?
29         bcs     invalidfd       ; Jump if no
30         sta     tmp2            ; Save the handle
31
32 ; Check if the file is actually open
33
34         tax
35         lda     fdtab,x         ; Get flags for this handle
36         and     #LFN_OPEN
37         beq     notopen
38
39 ; Valid lfn, close it. The close call is always error free, at least as far
40 ; as the kernal is involved
41
42         lda     #LFN_CLOSED
43         sta     fdtab,x
44         lda     tmp2            ; Get the handle
45         clc
46         adc     #LFN_OFFS       ; Make LFN from handle
47         jsr     CLOSE
48
49 ; Read the drive error channel, then close it
50
51         ldy     tmp2            ; Get the handle
52         ldx     unittab,y       ; Get teh disk for this handle
53         jsr     readdiskerror   ; Read the disk error code
54         pha                     ; Save it on stack
55         ldy     tmp2
56         ldx     unittab,y
57         jsr     closecmdchannel ; Close the disk command channel
58         pla                     ; Get the error code from the disk
59         bne     error           ; Jump if error
60
61 ; Successful
62
63         tax                     ; Return zero in a/x
64         rts
65
66 ; Error entry, file descriptor is invalid
67
68 invalidfd:
69         lda     #EINVAL
70         sta     __errno
71         lda     #0
72         sta     __errno+1
73         beq     errout
74
75 ; Error entry, file is not open
76
77 notopen:
78         lda     #3              ; File not open
79         bne     error
80
81 ; Error entry, status not ok
82
83 error:  sta     __oserror
84 errout: lda     #$FF
85         tax                     ; Return -1
86         rts
87
88 .endproc
89
90
91
92