ungetc.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * Copyright (c) 1993, 1994 Chris Provenzano. 
  4.  * All rights reserved.
  5.  *
  6.  * This code is derived from software contributed to Berkeley by
  7.  * Chris Torek.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  * This product includes software developed by the University of
  20.  * California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. /*static char *sccsid = "from: @(#)ungetc.c 5.6 (Berkeley) 5/4/91";*/
  39. static char *rcsid = "$Id$";
  40. #endif /* LIBC_SCCS and not lint */
  41. #include <pthread.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "local.h"
  46. /*
  47.  * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
  48.  * the buffer moves, so that it points the same distance from the end,
  49.  * and move the bytes in the buffer around as necessary so that they
  50.  * are all at the end (stack-style).
  51.  */
  52. static
  53. __submore(fp)
  54. register FILE *fp;
  55. {
  56. register int i;
  57. register unsigned char *p;
  58. if (fp->_ub._base == fp->_ubuf) {
  59. /*
  60.  * Get a new buffer (rather than expanding the old one).
  61.  */
  62. if ((p = malloc((size_t)BUFSIZ)) == NULL)
  63. return (EOF);
  64. fp->_ub._base = p;
  65. fp->_ub._size = BUFSIZ;
  66. p += BUFSIZ - sizeof(fp->_ubuf);
  67. for (i = sizeof(fp->_ubuf); --i >= 0;)
  68. p[i] = fp->_ubuf[i];
  69. fp->_p = p;
  70. return (0);
  71. }
  72. i = fp->_ub._size;
  73. p = realloc(fp->_ub._base, i << 1);
  74. if (p == NULL)
  75. return (EOF);
  76. (void) memcpy((void *)(p + i), (void *)p, (size_t)i);
  77. fp->_p = p + i;
  78. fp->_ub._base = p;
  79. fp->_ub._size = i << 1;
  80. return (0);
  81. }
  82. ungetc(c, fp)
  83. int c;
  84. register FILE *fp;
  85. {
  86. if (c == EOF)
  87. return (EOF);
  88. __sinit ();
  89. flockfile(fp);
  90. if ((fp->_flags & __SRD) == 0) {
  91. /*
  92.  * Not already reading: no good unless reading-and-writing.
  93.  * Otherwise, flush any current write stuff.
  94.  */
  95. if ((fp->_flags & __SRW) == 0)
  96. c = EOF;
  97. goto ungetc_end;
  98. if (fp->_flags & __SWR) {
  99. if (__sflush(fp))
  100. c = EOF;
  101. goto ungetc_end;
  102. fp->_flags &= ~__SWR;
  103. fp->_w = 0;
  104. fp->_lbfsize = 0;
  105. }
  106. fp->_flags |= __SRD;
  107. }
  108. c = (unsigned char)c;
  109. /*
  110.  * If we are in the middle of ungetc'ing, just continue.
  111.  * This may require expanding the current ungetc buffer.
  112.  */
  113. if (HASUB(fp)) {
  114. if (fp->_r >= fp->_ub._size && __submore(fp))
  115. return (EOF);
  116. *--fp->_p = c;
  117. fp->_r++;
  118. goto ungetc_end;
  119. }
  120. /*
  121.  * If we can handle this by simply backing up, do so,
  122.  * but never replace the original character.
  123.  * (This makes sscanf() work when scanning `const' data.)
  124.  */
  125. if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
  126.     fp->_p[-1] == c) {
  127. fp->_p--;
  128. fp->_r++;
  129. goto ungetc_end;
  130. }
  131. /*
  132.  * Create an ungetc buffer.
  133.  * Initially, we will use the `reserve' buffer.
  134.  */
  135. fp->_ur = fp->_r;
  136. fp->_up = fp->_p;
  137. fp->_ub._base = fp->_ubuf;
  138. fp->_ub._size = sizeof(fp->_ubuf);
  139. fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
  140. fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
  141. fp->_r = 1;
  142. ungetc_end:;
  143. funlockfile(fp);
  144. return (c);
  145. }