ungetwc.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. libc_hidden_proto(ungetwc)
  13. wint_t ungetwc(wint_t c, register FILE *stream)
  14. {
  15. __STDIO_AUTO_THREADLOCK_VAR;
  16. __STDIO_AUTO_THREADLOCK(stream);
  17. __STDIO_STREAM_VALIDATE(stream); /* debugging only */
  18. /* Note: Even if c == WEOF, we need to initialize/verify the
  19. * stream's orientation and ensure the stream is in reading
  20. * mode (if readable and properly oriented). */
  21. if ((!__STDIO_STREAM_IS_WIDE_READING(stream)
  22. && __STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_WIDE))
  23. || ((stream->__modeflags & __FLAG_UNGOT)
  24. && ((stream->__modeflags & 1) || stream->__ungot[1]))
  25. || (c == WEOF)
  26. ) {
  27. c = WEOF;
  28. } else {
  29. /* In the wide case, getc macros should already be disabled. */
  30. /* __STDIO_STREAM_DISABLE_GETC(stream); */
  31. /* Flag this as a user ungot, as scanf does the necessary fixup. */
  32. stream->__ungot[1] = 1;
  33. stream->__ungot[(++stream->__modeflags) & 1] = c;
  34. /* Note: ungot_width is handled by fgetwc. */
  35. __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
  36. }
  37. __STDIO_STREAM_VALIDATE(stream);
  38. __STDIO_AUTO_THREADUNLOCK(stream);
  39. return c;
  40. }
  41. libc_hidden_def(ungetwc)