From dc16edb6b22397529733fdc22e90959b7c61a6cd Mon Sep 17 00:00:00 2001 From: cuz Date: Thu, 13 May 2004 21:39:17 +0000 Subject: [PATCH] Implemented the ungetc function git-svn-id: svn://svn.cc65.org/cc65/trunk@3036 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- libsrc/common/Makefile | 1 + libsrc/common/ungetc.s | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 libsrc/common/ungetc.s diff --git a/libsrc/common/Makefile b/libsrc/common/Makefile index 6d4b5a06d..63e5a5b2c 100644 --- a/libsrc/common/Makefile +++ b/libsrc/common/Makefile @@ -148,6 +148,7 @@ S_OBJS = _cwd.o \ tolower.o \ toupper.o \ uname.o \ + ungetc.o \ unlink.o \ utscopy.o \ vfprintf.o \ diff --git a/libsrc/common/ungetc.s b/libsrc/common/ungetc.s new file mode 100644 index 000000000..590d2d671 --- /dev/null +++ b/libsrc/common/ungetc.s @@ -0,0 +1,71 @@ +; +; 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 + + tax + 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 + -- 2.39.5