ungetwc.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. /* Note: This is the application-callable ungetwc. If wscanf calls this, it
  9. * should also set stream->__ungot[1] to 0 if this is the only ungot, as well
  10. * as reset stream->__ungot_width[1] for use by _stdio_adjpos().
  11. */
  12. wint_t attribute_hidden __ungetwc(wint_t c, register FILE *stream)
  13. {
  14. __STDIO_AUTO_THREADLOCK_VAR;
  15. __STDIO_AUTO_THREADLOCK(stream);
  16. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  17. /* Note: Even if c == WEOF, we need to initialize/verify the
  18. * stream's orientation and ensure the stream is in reading
  19. * mode (if readable and properly oriented). */
  20. if ((!__STDIO_STREAM_IS_WIDE_READING(stream)
  21. && __STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_WIDE))
  22. || ((stream->__modeflags & __FLAG_UNGOT)
  23. && ((stream->__modeflags & 1) || stream->__ungot[1]))
  24. || (c == WEOF)
  25. ) {
  26. c = WEOF;
  27. } else {
  28. /* In the wide case, getc macros should already be disabled. */
  29. /* __STDIO_STREAM_DISABLE_GETC(stream); */
  30. /* Flag this as a user ungot, as scanf does the necessary fixup. */
  31. stream->__ungot[1] = 1;
  32. stream->__ungot[(++stream->__modeflags) & 1] = c;
  33. /* Note: ungot_width is handled by fgetwc. */
  34. __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
  35. }
  36. __STDIO_STREAM_VALIDATE(stream);
  37. __STDIO_AUTO_THREADUNLOCK(stream);
  38. return c;
  39. }
  40. strong_alias(__ungetwc,ungetwc)