ungetc.c revision 13545
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Chris Torek.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
381573Srgrimesstatic char sccsid[] = "@(#)ungetc.c	8.2 (Berkeley) 11/3/93";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
401573Srgrimes
411573Srgrimes#include <stdio.h>
421573Srgrimes#include <stdlib.h>
431573Srgrimes#include <string.h>
441573Srgrimes#include "local.h"
4513545Sjulian#ifdef _THREAD_SAFE
4613545Sjulian#include <pthread.h>
4713545Sjulian#include "pthread_private.h"
4813545Sjulian#endif
491573Srgrimes
501573Srgrimes/*
511573Srgrimes * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
521573Srgrimes * the buffer moves, so that it points the same distance from the end,
531573Srgrimes * and move the bytes in the buffer around as necessary so that they
541573Srgrimes * are all at the end (stack-style).
551573Srgrimes */
5613545Sjulianstatic int
571573Srgrimes__submore(fp)
581573Srgrimes	register FILE *fp;
591573Srgrimes{
601573Srgrimes	register int i;
611573Srgrimes	register unsigned char *p;
621573Srgrimes
631573Srgrimes	if (fp->_ub._base == fp->_ubuf) {
641573Srgrimes		/*
651573Srgrimes		 * Get a new buffer (rather than expanding the old one).
661573Srgrimes		 */
671573Srgrimes		if ((p = malloc((size_t)BUFSIZ)) == NULL)
681573Srgrimes			return (EOF);
691573Srgrimes		fp->_ub._base = p;
701573Srgrimes		fp->_ub._size = BUFSIZ;
711573Srgrimes		p += BUFSIZ - sizeof(fp->_ubuf);
721573Srgrimes		for (i = sizeof(fp->_ubuf); --i >= 0;)
731573Srgrimes			p[i] = fp->_ubuf[i];
741573Srgrimes		fp->_p = p;
751573Srgrimes		return (0);
761573Srgrimes	}
771573Srgrimes	i = fp->_ub._size;
781573Srgrimes	p = realloc(fp->_ub._base, i << 1);
791573Srgrimes	if (p == NULL)
801573Srgrimes		return (EOF);
811573Srgrimes	/* no overlap (hence can use memcpy) because we doubled the size */
821573Srgrimes	(void)memcpy((void *)(p + i), (void *)p, (size_t)i);
831573Srgrimes	fp->_p = p + i;
841573Srgrimes	fp->_ub._base = p;
851573Srgrimes	fp->_ub._size = i << 1;
861573Srgrimes	return (0);
871573Srgrimes}
881573Srgrimes
8913545Sjulianint
901573Srgrimesungetc(c, fp)
911573Srgrimes	int c;
921573Srgrimes	register FILE *fp;
931573Srgrimes{
941573Srgrimes	if (c == EOF)
951573Srgrimes		return (EOF);
961573Srgrimes	if (!__sdidinit)
971573Srgrimes		__sinit();
9813545Sjulian#ifdef _THREAD_SAFE
9913545Sjulian	_thread_flockfile(fp,__FILE__,__LINE__);
10013545Sjulian#endif
1011573Srgrimes	if ((fp->_flags & __SRD) == 0) {
1021573Srgrimes		/*
1031573Srgrimes		 * Not already reading: no good unless reading-and-writing.
1041573Srgrimes		 * Otherwise, flush any current write stuff.
1051573Srgrimes		 */
10613545Sjulian		if ((fp->_flags & __SRW) == 0) {
10713545Sjulian#ifdef _THREAD_SAFE
10813545Sjulian			_thread_funlockfile(fp);
10913545Sjulian#endif
1101573Srgrimes			return (EOF);
11113545Sjulian		}
1121573Srgrimes		if (fp->_flags & __SWR) {
11313545Sjulian			if (__sflush(fp)) {
11413545Sjulian#ifdef _THREAD_SAFE
11513545Sjulian				_thread_funlockfile(fp);
11613545Sjulian#endif
1171573Srgrimes				return (EOF);
11813545Sjulian			}
1191573Srgrimes			fp->_flags &= ~__SWR;
1201573Srgrimes			fp->_w = 0;
1211573Srgrimes			fp->_lbfsize = 0;
1221573Srgrimes		}
1231573Srgrimes		fp->_flags |= __SRD;
1241573Srgrimes	}
1251573Srgrimes	c = (unsigned char)c;
1261573Srgrimes
1271573Srgrimes	/*
1281573Srgrimes	 * If we are in the middle of ungetc'ing, just continue.
1291573Srgrimes	 * This may require expanding the current ungetc buffer.
1301573Srgrimes	 */
1311573Srgrimes	if (HASUB(fp)) {
13213545Sjulian		if (fp->_r >= fp->_ub._size && __submore(fp)) {
13313545Sjulian#ifdef _THREAD_SAFE
13413545Sjulian			_thread_funlockfile(fp);
13513545Sjulian#endif
1361573Srgrimes			return (EOF);
13713545Sjulian		}
1381573Srgrimes		*--fp->_p = c;
1391573Srgrimes		fp->_r++;
14013545Sjulian#ifdef _THREAD_SAFE
14113545Sjulian		_thread_funlockfile(fp);
14213545Sjulian#endif
1431573Srgrimes		return (c);
1441573Srgrimes	}
1451573Srgrimes	fp->_flags &= ~__SEOF;
1461573Srgrimes
1471573Srgrimes	/*
1481573Srgrimes	 * If we can handle this by simply backing up, do so,
1491573Srgrimes	 * but never replace the original character.
1501573Srgrimes	 * (This makes sscanf() work when scanning `const' data.)
1511573Srgrimes	 */
1521573Srgrimes	if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
1531573Srgrimes	    fp->_p[-1] == c) {
1541573Srgrimes		fp->_p--;
1551573Srgrimes		fp->_r++;
15613545Sjulian#ifdef _THREAD_SAFE
15713545Sjulian		_thread_funlockfile(fp);
15813545Sjulian#endif
1591573Srgrimes		return (c);
1601573Srgrimes	}
1611573Srgrimes
1621573Srgrimes	/*
1631573Srgrimes	 * Create an ungetc buffer.
1641573Srgrimes	 * Initially, we will use the `reserve' buffer.
1651573Srgrimes	 */
1661573Srgrimes	fp->_ur = fp->_r;
1671573Srgrimes	fp->_up = fp->_p;
1681573Srgrimes	fp->_ub._base = fp->_ubuf;
1691573Srgrimes	fp->_ub._size = sizeof(fp->_ubuf);
1701573Srgrimes	fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
1711573Srgrimes	fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
1721573Srgrimes	fp->_r = 1;
17313545Sjulian#ifdef _THREAD_SAFE
17413545Sjulian	_thread_funlockfile(fp);
17513545Sjulian#endif
1761573Srgrimes	return (c);
1771573Srgrimes}
178