diff --git a/libsrc/common/checkferror.s b/libsrc/common/checkferror.s new file mode 100644 index 000000000..736fa3ccd --- /dev/null +++ b/libsrc/common/checkferror.s @@ -0,0 +1,24 @@ +; +; Colin Leroy-Mira, 2024 +; +; Helper to check for file opened, not eof, not ferror +; Expects file pointer in ptr1, +; Returns with Z flag set if everything is OK, +; Destroys A, X, Y, +; Sets file flags in A +; + + .export checkferror + .importzp ptr1 + + .include "_file.inc" + +checkferror: + ldy #_FILE::f_flags + lda (ptr1),y + tax + and #(_FOPEN|_FERROR|_FEOF); Check for file open, error/eof + tay + txa + cpy #_FOPEN + rts diff --git a/libsrc/common/fgetc.s b/libsrc/common/fgetc.s index 777696b7a..98e6bb2f7 100644 --- a/libsrc/common/fgetc.s +++ b/libsrc/common/fgetc.s @@ -5,7 +5,8 @@ ; .export _fgetc - .import _read, pusha0, pushax, popptr1, incsp2, returnFFFF + .import _read, checkferror + .import pusha0, pushax, popptr1, incsp2, returnFFFF .importzp ptr1 .include "stdio.inc" @@ -16,16 +17,10 @@ _fgetc: stx ptr1+1 jsr pushax ; Backup our ptr - ldy #_FILE::f_flags - lda (ptr1),y - tax - and #_FOPEN ; Check for file open - beq ret_eof - txa - and #(_FERROR|_FEOF); Check for error/eof + jsr checkferror bne ret_eof - txa + tax and #_FPUSHBACK ; Check for pushed back char beq do_read diff --git a/libsrc/common/fputc.s b/libsrc/common/fputc.s index 4633f24f1..358723538 100644 --- a/libsrc/common/fputc.s +++ b/libsrc/common/fputc.s @@ -6,7 +6,7 @@ .export _fputc .importzp ptr1 - .import _write + .import _write, checkferror .import pushax, pusha0, popax, incsp2 .import pushptr1, popptr1, returnFFFF @@ -21,13 +21,7 @@ _fputc: sta c ; to return it anyway stx c+1 - ldy #_FILE::f_flags - lda (ptr1),y - tax - and #_FOPEN ; Check for file open - beq ret_eof - txa - and #(_FERROR|_FEOF); Check for error/eof + jsr checkferror bne ret_eof jsr pushptr1 ; Backup fp pointer diff --git a/libsrc/common/fputs.s b/libsrc/common/fputs.s index 00415aef1..b79a4707f 100644 --- a/libsrc/common/fputs.s +++ b/libsrc/common/fputs.s @@ -6,7 +6,7 @@ .export _fputs .importzp ptr1, ptr2 - .import _write, _strlen + .import _write, _strlen, checkferror .import swapstk, pushax, returnFFFF .include "stdio.inc" @@ -16,13 +16,7 @@ _fputs: sta ptr1 stx ptr1+1 - ldy #_FILE::f_flags - lda (ptr1),y - tax - and #_FOPEN ; Check for file open - beq ret_eof - txa - and #(_FERROR|_FEOF); Check for error/eof + jsr checkferror bne ret_eof ; Push _write parameters