summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2022-10-10 22:16:48 +0200
committerpgen <p.gen.progs@gmail.com>2022-10-10 22:16:48 +0200
commitb332fef5c3ce3dce9e85857e8905f7291e0b1a6e (patch)
treeea62851102449e84ae17239b8eec4ff4d03c367a
parent2548023fc17c2c46e045c58a176a1c452cb83c3f (diff)
Improve byte reading from stdin
-rw-r--r--fgetc.c18
-rw-r--r--fgetc.h2
2 files changed, 19 insertions, 1 deletions
diff --git a/fgetc.c b/fgetc.c
index 95f7fd1..cf755d8 100644
--- a/fgetc.c
+++ b/fgetc.c
@@ -28,7 +28,23 @@ static long next_buffer_pos = 0; /* Next free position in the getc buffer. */
int
my_fgetc(FILE * input)
{
- return (next_buffer_pos > 0) ? getc_buffer[--next_buffer_pos] : fgetc(input);
+ int c;
+
+ if (next_buffer_pos > 0)
+ return getc_buffer[--next_buffer_pos];
+ else
+ {
+ errno = 0;
+ c = fgetc(input);
+
+ while (c == EOF && errno == EAGAIN)
+ {
+ errno = 0;
+ c = fgetc(input);
+ }
+
+ return c;
+ }
}
/* =============================== */
diff --git a/fgetc.h b/fgetc.h
index eae2f7b..9e2d53b 100644
--- a/fgetc.h
+++ b/fgetc.h
@@ -9,6 +9,8 @@
#ifndef FGETC_H
#define FGETC_H
+#include <errno.h>
+
int
my_fgetc(FILE * input);