Skip to content

Commit

Permalink
tinystdio: Mark ungetc using +1 instead of |0x100
Browse files Browse the repository at this point in the history
Prepare for wide char support.  This allows use of nearly the full
range of __ungetc_t values, which means all of UCS2 (as 0xffff is
invalid). Still not all of a 4-byte wchar_t type, but it's better?

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Feb 15, 2023
1 parent f452d18 commit 4cbfcfa
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 7 deletions.
2 changes: 1 addition & 1 deletion newlib/libc/tinystdio/fgetc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fgetc(FILE *stream)
return EOF;

if ((unget = __atomic_exchange_ungetc(&stream->unget, 0)) != 0)
return (unsigned char) unget;
return (unsigned char) (unget - 1);

rv = stream->get(stream);
if (rv < 0) {
Expand Down
5 changes: 0 additions & 5 deletions newlib/libc/tinystdio/stdio_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@
#define SCANF_STD 1
#define SCANF_FLT 2

/* This is OR'd into the char stored in unget to make it non-zero to
* flag the unget buffer as being full
*/
#define UNGETC_MARK 0x0100

struct __file_str {
struct __file file; /* main file struct */
char *pos; /* current buffer position */
Expand Down
2 changes: 1 addition & 1 deletion newlib/libc/tinystdio/ungetc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ungetc(int c, FILE *stream)
if ((stream->flags & __SRD) == 0 || c == EOF)
return EOF;

if (!__atomic_compare_exchange_ungetc(&stream->unget, 0, c | UNGETC_MARK))
if (!__atomic_compare_exchange_ungetc(&stream->unget, 0, (__ungetc_t) c + 1 ))
return EOF;

stream->flags &= ~__SEOF;
Expand Down

0 comments on commit 4cbfcfa

Please sign in to comment.