ungetc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. int ungetc(int c, register FILE *stream)
  25. {
  26. __STDIO_AUTO_THREADLOCK_VAR;
  27. __STDIO_AUTO_THREADLOCK(stream);
  28. __STDIO_STREAM_VALIDATE(stream);
  29. #ifdef __UCLIBC_HAS_STDIO_GETC_MACRO__
  30. /* If buffered narrow reading with no ungot slots filled, and if not
  31. * ungetting a different char than the one last read from the buffer,
  32. * we can simply decrement the position and not worry about disabling
  33. * the getc macros. This will cut down on overhead in applications
  34. * that use getc/ungetc extensively (like gcc). */
  35. /* NOTE: If we can use getc, then we are buffered narrow reading with
  36. * no ungot slots filled. */
  37. if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)
  38. && (c != EOF)
  39. && (stream->__bufpos > stream->__bufstart)
  40. && (stream->__bufpos[-1] == ((unsigned char)c))
  41. ) {
  42. --stream->__bufpos;
  43. __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
  44. } else
  45. #endif
  46. /* Note: Even if c == EOF, we need to initialize/verify the
  47. * stream's orientation and ensure the stream is in reading
  48. * mode (if readable and properly oriented). */
  49. if ((!__STDIO_STREAM_IS_NARROW_READING(stream)
  50. && __STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW))
  51. || ((stream->__modeflags & __FLAG_UNGOT)
  52. && ((stream->__modeflags & 1) || stream->__ungot[1]))
  53. ) {
  54. c = EOF;
  55. } else if (c != EOF) {
  56. __STDIO_STREAM_DISABLE_GETC(stream);
  57. /* Flag this as a user ungot, as scanf does the necessary fixup. */
  58. stream->__ungot[1] = 1;
  59. stream->__ungot[(++stream->__modeflags) & 1] = c;
  60. __STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
  61. }
  62. __STDIO_STREAM_VALIDATE(stream);
  63. __STDIO_AUTO_THREADUNLOCK(stream);
  64. return c;
  65. }
  66. libc_hidden_def(ungetc)