From 50c0a4ffc018d30032251f647ada7a615264553c Mon Sep 17 00:00:00 2001
From: pgen
Date: Wed, 14 Jul 2021 16:56:36 +0200
Subject: Improve and fix my_fgetc/my_ungetc again
---
fgetc.c | 13 ++++++++++---
fgetc.h | 2 +-
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/fgetc.c b/fgetc.c
index 7da6b24..f6a9e93 100644
--- a/fgetc.c
+++ b/fgetc.c
@@ -15,7 +15,7 @@ enum
GETC_BUFF_SIZE = 16
};
-static char getc_buffer[GETC_BUFF_SIZE] = { '\0' };
+static unsigned char getc_buffer[GETC_BUFF_SIZE] = { '\0' };
static long next_buffer_pos = 0; /* Next free position in the getc buffer. */
@@ -31,17 +31,24 @@ my_fgetc(FILE * input)
/* =============================== */
/* Pushes character back on input. */
/* =============================== */
-void
+int
my_ungetc(int c, FILE * input)
{
+ int rc;
+
if (next_buffer_pos >= GETC_BUFF_SIZE)
+ {
fprintf(stderr, "Error: cannot push back more than %d characters\n",
GETC_BUFF_SIZE);
+ rc = EOF;
+ }
else
{
- getc_buffer[next_buffer_pos++] = c;
+ rc = getc_buffer[next_buffer_pos++] = (unsigned char)c;
if (feof(input))
clearerr(input); /* No more EOF. */
}
+
+ return rc;
}
diff --git a/fgetc.h b/fgetc.h
index 3948588..4359d26 100644
--- a/fgetc.h
+++ b/fgetc.h
@@ -9,7 +9,7 @@
int
my_fgetc(FILE * input);
-void
+int
my_ungetc(int c, FILE * input);
#endif
--
cgit v1.2.3