diff options
Diffstat (limited to 'lib/libc/stdio/ungetc.c')
-rw-r--r-- | lib/libc/stdio/ungetc.c | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/lib/libc/stdio/ungetc.c b/lib/libc/stdio/ungetc.c index deaed75..c0355b7 100644 --- a/lib/libc/stdio/ungetc.c +++ b/lib/libc/stdio/ungetc.c @@ -35,13 +35,23 @@ */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)ungetc.c 8.2 (Berkeley) 11/3/93"; +#endif +static const char rcsid[] = + "$Id$"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "local.h" +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" +#endif + +static int __submore __P((FILE *)); /* * Expand the ungetc buffer `in place'. That is, adjust fp->_p when @@ -49,7 +59,7 @@ static char sccsid[] = "@(#)ungetc.c 8.2 (Berkeley) 11/3/93"; * and move the bytes in the buffer around as necessary so that they * are all at the end (stack-style). */ -static +static int __submore(fp) register FILE *fp; { @@ -71,7 +81,7 @@ __submore(fp) return (0); } i = fp->_ub._size; - p = realloc(fp->_ub._base, i << 1); + p = realloc(fp->_ub._base, (size_t)(i << 1)); if (p == NULL) return (EOF); /* no overlap (hence can use memcpy) because we doubled the size */ @@ -82,6 +92,7 @@ __submore(fp) return (0); } +int ungetc(c, fp) int c; register FILE *fp; @@ -90,16 +101,27 @@ ungetc(c, fp) return (EOF); if (!__sdidinit) __sinit(); +#ifdef _THREAD_SAFE + _thread_flockfile(fp,__FILE__,__LINE__); +#endif if ((fp->_flags & __SRD) == 0) { /* * Not already reading: no good unless reading-and-writing. * Otherwise, flush any current write stuff. */ - if ((fp->_flags & __SRW) == 0) + if ((fp->_flags & __SRW) == 0) { +#ifdef _THREAD_SAFE + _thread_funlockfile(fp); +#endif return (EOF); + } if (fp->_flags & __SWR) { - if (__sflush(fp)) + if (__sflush(fp)) { +#ifdef _THREAD_SAFE + _thread_funlockfile(fp); +#endif return (EOF); + } fp->_flags &= ~__SWR; fp->_w = 0; fp->_lbfsize = 0; @@ -113,10 +135,17 @@ ungetc(c, fp) * This may require expanding the current ungetc buffer. */ if (HASUB(fp)) { - if (fp->_r >= fp->_ub._size && __submore(fp)) + if (fp->_r >= fp->_ub._size && __submore(fp)) { +#ifdef _THREAD_SAFE + _thread_funlockfile(fp); +#endif return (EOF); + } *--fp->_p = c; fp->_r++; +#ifdef _THREAD_SAFE + _thread_funlockfile(fp); +#endif return (c); } fp->_flags &= ~__SEOF; @@ -130,6 +159,9 @@ ungetc(c, fp) fp->_p[-1] == c) { fp->_p--; fp->_r++; +#ifdef _THREAD_SAFE + _thread_funlockfile(fp); +#endif return (c); } @@ -144,5 +176,8 @@ ungetc(c, fp) fp->_ubuf[sizeof(fp->_ubuf) - 1] = c; fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1]; fp->_r = 1; +#ifdef _THREAD_SAFE + _thread_funlockfile(fp); +#endif return (c); } |