Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.3 KiB

  1. /***
  2. *ungetc.c - unget a character from a stream
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines ungetc() - pushes a character back onto an input stream
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 04-16-87 JCR added support for _IOUNGETC flag
  12. * 08-04-87 JCR (1) Added _IOSTRG check before setting _IOUNGETC flag.
  13. * (2) Allow an ugnetc() before a read has occurred (get a
  14. * buffer (ANSI). [MSC only]
  15. * 09-28-87 JCR Corrected _iob2 indexing (now uses _iob_index() macro).
  16. * 11-04-87 JCR Multi-thread support
  17. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  18. * 05-25-88 JCR Allow an ungetc() before read for file opened "r+".
  19. * 05-31-88 PHG Merged DLL and normal versions
  20. * 06-06-88 JCR Optimized _iob2 references
  21. * 06-15-88 JCR Near reference to _iob[] entries; improve REG variables
  22. * 08-25-88 GJF Don't use FP_OFF() macro for the 386
  23. * 04-11-89 JCR Removed _IOUNGETC flag, fseek() no longer needs it
  24. * 08-17-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  25. * model). Also fixed copyright and indents.
  26. * 02-16-90 GJF Fixed copyright
  27. * 03-20-90 GJF Made calling type _CALLTYPE1, added #include
  28. * <cruntime.h> and removed #include <register.h>.
  29. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  30. * 08-13-90 SBM Compiles cleanly with -W3
  31. * 10-03-90 GJF New-style function declarators.
  32. * 11-07-92 SRW Dont modify buffer if stream opened by sscanf
  33. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  34. * 04-26-93 CFW Wide char enable.
  35. * 04-30-93 CFW Remove wide char support to ungetwc.c.
  36. * 09-06-94 CFW Replace MTHREAD with _MT.
  37. * 02-06-94 CFW assert -> _ASSERTE.
  38. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  39. * 03-02-98 GJF Exception-safe locking.
  40. *
  41. *******************************************************************************/
  42. #include <cruntime.h>
  43. #include <stdio.h>
  44. #include <file2.h>
  45. #include <dbgint.h>
  46. #include <internal.h>
  47. #include <mtdll.h>
  48. #ifdef _MT /* multi-thread; define both ungetc and _lk_ungetc */
  49. /***
  50. *int ungetc(ch, stream) - put a character back onto a stream
  51. *
  52. *Purpose:
  53. * Guaranteed one char pushback on a stream as long as open for reading.
  54. * More than one char pushback in a row is not guaranteed, and will fail
  55. * if it follows an ungetc which pushed the first char in buffer. Failure
  56. * causes return of EOF.
  57. *
  58. *Entry:
  59. * char ch - character to push back
  60. * FILE *stream - stream to push character onto
  61. *
  62. *Exit:
  63. * returns ch
  64. * returns EOF if tried to push EOF, stream not opened for reading or
  65. * or if we have already ungetc'd back to beginning of buffer.
  66. *
  67. *Exceptions:
  68. *
  69. *******************************************************************************/
  70. int __cdecl ungetc (
  71. REG2 int ch,
  72. REG1 FILE *stream
  73. )
  74. {
  75. int retval;
  76. _ASSERTE(stream != NULL);
  77. _lock_str(stream);
  78. __try {
  79. retval = _ungetc_lk (ch, stream);
  80. }
  81. __finally {
  82. _unlock_str(stream);
  83. }
  84. return(retval);
  85. }
  86. /***
  87. *_ungetc_lk() - Ungetc() core routine (locked version)
  88. *
  89. *Purpose:
  90. * Core ungetc() routine; assumes stream is already locked.
  91. *
  92. * [See ungetc() above for more info.]
  93. *
  94. *Entry: [See ungetc()]
  95. *
  96. *Exit: [See ungetc()]
  97. *
  98. *Exceptions:
  99. *
  100. *******************************************************************************/
  101. int __cdecl _ungetc_lk (
  102. REG2 int ch,
  103. FILE *str
  104. )
  105. {
  106. #else /* non multi-thread; just define ungetc */
  107. int __cdecl ungetc (
  108. REG2 int ch,
  109. FILE *str
  110. )
  111. {
  112. #endif /* rejoin common code */
  113. REG1 FILE *stream;
  114. _ASSERTE(str != NULL);
  115. /* Init stream pointer and file descriptor */
  116. stream = str;
  117. /* Stream must be open for read and can NOT be currently in write mode.
  118. Also, ungetc() character cannot be EOF. */
  119. if (
  120. (ch == EOF) ||
  121. !(
  122. (stream->_flag & _IOREAD) ||
  123. ((stream->_flag & _IORW) && !(stream->_flag & _IOWRT))
  124. )
  125. )
  126. return(EOF);
  127. /* If stream is unbuffered, get one. */
  128. if (stream->_base == NULL)
  129. _getbuf(stream);
  130. /* now we know _base != NULL; since file must be buffered */
  131. if (stream->_ptr == stream->_base) {
  132. if (stream->_cnt)
  133. /* my back is against the wall; i've already done
  134. * ungetc, and there's no room for this one
  135. */
  136. return(EOF);
  137. stream->_ptr++;
  138. }
  139. if (stream->_flag & _IOSTRG) {
  140. /* If stream opened by sscanf do not modify buffer */
  141. if (*--stream->_ptr != (char)ch) {
  142. ++stream->_ptr;
  143. return(EOF);
  144. }
  145. } else
  146. *--stream->_ptr = (char)ch;
  147. stream->_cnt++;
  148. stream->_flag &= ~_IOEOF;
  149. stream->_flag |= _IOREAD; /* may already be set */
  150. return(0xff & ch);
  151. }