]> git.sur5r.net Git - cc65/blob - libsrc/common/fclose.s
Resolved conflict and removed adaptation for strpbrk for time being.
[cc65] / libsrc / common / fclose.s
1 ;
2 ; Ullrich von Bassewitz, 22.11.2002
3 ;
4 ; int __fastcall__ fclose (FILE* f);
5 ; /* Close a file */
6 ;
7
8         .export         _fclose
9
10         .import         _close
11         .importzp       ptr1
12
13         .include        "errno.inc"
14         .include        "_file.inc"
15
16 ; ------------------------------------------------------------------------
17 ; Code
18
19 .proc   _fclose
20
21         sta     ptr1
22         stx     ptr1+1          ; Store f
23
24 ; Check if the file is really open
25
26         ldy     #_FILE::f_flags
27         lda     (ptr1),y
28         and     #_FOPEN
29         bne     @L1
30
31 ; File is not open
32
33         lda     #EINVAL
34         jsr     __seterrno
35         lda     #$FF            ; Return -1
36         tax
37         rts
38
39 ; File is open. Reset the flags and close the file.
40
41 @L1:    lda     #_FCLOSED
42         sta     (ptr1),y
43
44         ldy     #_FILE::f_fd
45         lda     (ptr1),y
46         ldx     #0
47         jmp     _close          ; Will set errno and return an error flag
48
49 .endproc
50