ungetc.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* Having ungotten characters implies the stream is reading.
  9. * The scheme used here treats the least significant 2 bits of
  10. * the stream's modeflags member as follows:
  11. * 0 0 Not currently reading.
  12. * 0 1 Reading, but no ungetc() or scanf() push back chars.
  13. * 1 0 Reading with one ungetc() char (ungot[1] is 1)
  14. * or one scanf() pushed back char (ungot[1] is 0).
  15. * 1 1 Reading with both an ungetc() char and a scanf()
  16. * pushed back char. Note that this must be the result
  17. * of a scanf() push back (in ungot[0]) _followed_ by
  18. * an ungetc() call (in ungot[1]).
  19. *
  20. * Notes:
  21. * scanf() can NOT use ungetc() to push back characters.
  22. * (See section 7.19.6.2 of the C9X rationale -- WG14/N897.)
  23. */
  24. /* libc_hidden_proto(ungetc) */
  25. int ungetc(int c, register FILE *stream)
  26. {
  27. __STDIO_AUTO_THREADLOCK_VAR;
  28. __STDIO_AUTO_THREADLOCK(stream);
  29. __STDIO_STREAM_VALIDATE(stream);
  30. #ifdef __UCLIBC_MJN3_ONLY__
  31. #warning CONSIDER: Make fast ungetc an option?
  32. #endif
  33. #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  34. /* If buffered narrow reading with no ungot slots filled, and if not
  35. * ungetting a different char than the one last read from the buffer,
  36. * we can simply decrement the position and not worry about disabling
  37. * the getc macros. This will cut down on overhead in applications
  38. * that use getc/ungetc extensively (like gcc). */
  39. /* NOTE: If we can use getc, then we are buffered narrow reading with
  40. * no ungot slots filled. */
  41. if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)
  42. && (c != EOF)
  43. && (stream->__bufpos > stream->__bufstart)
  44. && (stream->__bufpos[-1] == ((unsigned char)c))
  45. ) {
  46. --stream->__bufpos;
  47. __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
  48. } else
  49. #endif
  50. /* Note: Even if c == EOF, we need to initialize/verify the
  51. * stream's orientation and ensure the stream is in reading
  52. * mode (if readable and properly oriented). */
  53. if ((!__STDIO_STREAM_IS_NARROW_READING(stream)
  54. && __STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW))
  55. || ((stream->__modeflags & __FLAG_UNGOT)
  56. && ((stream->__modeflags & 1) || stream->__ungot[1]))
  57. ) {
  58. c = EOF;
  59. } else if (c != EOF) {
  60. __STDIO_STREAM_DISABLE_GETC(stream);
  61. /* Flag this as a user ungot, as scanf does the necessary fixup. */
  62. stream->__ungot[1] = 1;
  63. stream->__ungot[(++stream->__modeflags) & 1] = c;
  64. __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
  65. }
  66. __STDIO_STREAM_VALIDATE(stream);
  67. __STDIO_AUTO_THREADUNLOCK(stream);
  68. return c;
  69. }
  70. libc_hidden_def(ungetc)