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