Rewrote parts of the code for better error handling. fileno must set errno and
return -1 in case of an invalid argument, which it didn't until now. git-svn-id: svn://svn.cc65.org/cc65/trunk@4698 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
@@ -9,70 +9,81 @@
|
|||||||
.importzp ptr1
|
.importzp ptr1
|
||||||
|
|
||||||
.include "_file.inc"
|
.include "_file.inc"
|
||||||
|
.include "errno.inc"
|
||||||
|
|
||||||
;
|
;
|
||||||
; Get the FILE* parameter, check if the file is open
|
; Get the FILE* parameter, check if the file is open. Returns zero in A
|
||||||
;
|
; and zero flag set in case of an error.
|
||||||
|
|
||||||
getf: sta ptr1
|
.proc getf
|
||||||
stx ptr1+1
|
sta ptr1
|
||||||
ldy #_FILE::f_flags
|
stx ptr1+1
|
||||||
lda (ptr1),y ; get f->f_flags
|
ldy #_FILE::f_flags
|
||||||
and #_FOPEN ; file open?
|
lda (ptr1),y ; get f->f_flags
|
||||||
beq @L1 ; jump if no
|
and #_FOPEN ; file open?
|
||||||
clc ; ok
|
|
||||||
rts
|
|
||||||
@L1: sec
|
|
||||||
rts
|
rts
|
||||||
|
.endproc
|
||||||
|
|
||||||
;
|
;
|
||||||
; void clearerr (FILE* f);
|
; void clearerr (FILE* f);
|
||||||
;
|
;
|
||||||
|
|
||||||
_clearerr:
|
.proc _clearerr
|
||||||
jsr getf
|
jsr getf
|
||||||
bcs err
|
beq err
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
and #<~(_FEOF | _FERROR)
|
and #<~(_FEOF | _FERROR)
|
||||||
sta (ptr1),y
|
sta (ptr1),y
|
||||||
err: rts
|
err: rts
|
||||||
|
.endproc
|
||||||
|
|
||||||
;
|
;
|
||||||
; int feof (FILE* f);
|
; int feof (FILE* f);
|
||||||
;
|
;
|
||||||
|
|
||||||
_feof:
|
.proc _feof
|
||||||
jsr getf
|
jsr getf
|
||||||
; bcs err
|
beq @L1 ; Return 0 on error
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
and #_FEOF
|
and #_FEOF
|
||||||
ldx #0
|
@L1: ldx #0
|
||||||
rts
|
rts
|
||||||
|
.endproc
|
||||||
|
|
||||||
;
|
;
|
||||||
; int ferror (FILE* f);
|
; int ferror (FILE* f);
|
||||||
;
|
;
|
||||||
|
|
||||||
_ferror:
|
.proc _ferror
|
||||||
jsr getf
|
jsr getf
|
||||||
; bcs err
|
beq @L1 ; Return 0 on error
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
and #_FERROR
|
and #_FERROR
|
||||||
ldx #0
|
@L1: ldx #0
|
||||||
rts
|
rts
|
||||||
|
.endproc
|
||||||
|
|
||||||
;
|
;
|
||||||
; int fileno (FILE* f);
|
; int fileno (FILE* f);
|
||||||
;
|
;
|
||||||
|
|
||||||
_fileno:
|
.proc _fileno
|
||||||
jsr getf
|
jsr getf
|
||||||
; bcs err
|
beq error
|
||||||
ldy #_FILE::f_fd
|
ldy #_FILE::f_fd
|
||||||
lda (ptr1),y
|
lda (ptr1),y
|
||||||
ldx #0
|
ldx #0
|
||||||
rts
|
rts
|
||||||
|
|
||||||
|
; If the file is not valid, fileno must set errno and return -1
|
||||||
|
|
||||||
|
error: lda #<EBADF
|
||||||
|
jsr __seterrno
|
||||||
|
lda #$FF
|
||||||
|
tax
|
||||||
|
rts
|
||||||
|
.endproc
|
||||||
|
|
||||||
;
|
;
|
||||||
; int __fastcall__ fflush (FILE* f);
|
; int __fastcall__ fflush (FILE* f);
|
||||||
;
|
;
|
||||||
|
|||||||
Reference in New Issue
Block a user