]> git.sur5r.net Git - cc65/commitdiff
Implemented the ungetc function
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Thu, 13 May 2004 21:39:17 +0000 (21:39 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Thu, 13 May 2004 21:39:17 +0000 (21:39 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@3036 b7a2c559-68d2-44c3-8de9-860c34a00d81

libsrc/common/Makefile
libsrc/common/ungetc.s [new file with mode: 0644]

index 6d4b5a06d8000c801405842864c7eccbc960a734..63e5a5b2cfee3f7832918ade729f4cea911d5687 100644 (file)
@@ -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 (file)
index 0000000..590d2d6
--- /dev/null
@@ -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
+