1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-18 00:07:21 +00:00
cc65/libsrc/common/ungetc.s
cuz b61b5f3ec5 Fixed a bug
git-svn-id: svn://svn.cc65.org/cc65/trunk@3041 b7a2c559-68d2-44c3-8de9-860c34a00d81
2004-05-14 14:33:43 +00:00

72 lines
1.3 KiB
ArmAsm

;
; Ullrich von Bassewitz, 2004-05-12
;
; int __fastcall__ ungetc (int c, FILE* f);
; /* Push back a character into a file stream. */
;
.export _ungetc
.import popax
.import ptr1: zp, tmp1: zp
.include "_file.inc"
.include "errno.inc"
; ------------------------------------------------------------------------
; Code
.proc _ungetc
; Save the file pointer to ptr1
sta ptr1
stx ptr1+1
; Get c from stack and save the lower byte in tmp1
jsr popax
sta tmp1
; c must be in char range
txa
bne error
; Check if the file is open
ldy #_FILE::f_flags
lda (ptr1),y
and #_FOPEN ; Is the file open?
beq error ; Branch if no
; Set the pushback flag and reset the end-of-file indicator
lda (ptr1),y
ora #_FPUSHBACK
and #<~_FEOF
sta (ptr1),y
; Store the character into the pushback buffer
ldy #_FILE::f_pushback
lda tmp1
sta (ptr1),y
; Done, return c
ldx #0
rts
; File is not open or the character is invalid
error: lda #EINVAL
jsr __seterrno
lda #$FF ; Return -1
tax
rts
.endproc