diff options
author | pgen <p.gen.progs@gmail.com> | 2021-07-14 16:56:36 +0200 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2021-07-14 16:58:23 +0200 |
commit | 50c0a4ffc018d30032251f647ada7a615264553c (patch) | |
tree | f960e705be834088ff357114d8caf811c61657d7 /fgetc.c | |
parent | 2ac8cc9f516ec5c5991ba22d48662a40c3bd6dd0 (diff) |
Improve and fix my_fgetc/my_ungetc again
Diffstat (limited to 'fgetc.c')
-rw-r--r-- | fgetc.c | 13 |
1 files changed, 10 insertions, 3 deletions
@@ -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; } |