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.
16249808Semaste * 3. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
341573Srgrimesstatic char sccsid[] = "@(#)ungetc.c	8.2 (Berkeley) 11/3/93";
351573Srgrimes#endif /* LIBC_SCCS and not lint */
3692986Sobrien#include <sys/cdefs.h>
3792986Sobrien__FBSDID("$FreeBSD: stable/11/lib/libc/stdio/ungetc.c 320942 2017-07-13 09:27:11Z kib $");
381573Srgrimes
3971579Sdeischen#include "namespace.h"
401573Srgrimes#include <stdio.h>
411573Srgrimes#include <stdlib.h>
421573Srgrimes#include <string.h>
4371579Sdeischen#include "un-namespace.h"
441573Srgrimes#include "local.h"
4535129Sjb#include "libc_private.h"
461573Srgrimes
4792905Sobrienstatic int __submore(FILE *);
4816586Sjraynard
491573Srgrimes/*
501573Srgrimes * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
511573Srgrimes * the buffer moves, so that it points the same distance from the end,
521573Srgrimes * and move the bytes in the buffer around as necessary so that they
531573Srgrimes * are all at the end (stack-style).
541573Srgrimes */
5513545Sjulianstatic int
5671579Sdeischen__submore(FILE *fp)
571573Srgrimes{
5871579Sdeischen	int i;
5971579Sdeischen	unsigned char *p;
601573Srgrimes
611573Srgrimes	if (fp->_ub._base == fp->_ubuf) {
621573Srgrimes		/*
631573Srgrimes		 * Get a new buffer (rather than expanding the old one).
641573Srgrimes		 */
651573Srgrimes		if ((p = malloc((size_t)BUFSIZ)) == NULL)
661573Srgrimes			return (EOF);
671573Srgrimes		fp->_ub._base = p;
681573Srgrimes		fp->_ub._size = BUFSIZ;
691573Srgrimes		p += BUFSIZ - sizeof(fp->_ubuf);
701573Srgrimes		for (i = sizeof(fp->_ubuf); --i >= 0;)
711573Srgrimes			p[i] = fp->_ubuf[i];
721573Srgrimes		fp->_p = p;
731573Srgrimes		return (0);
741573Srgrimes	}
751573Srgrimes	i = fp->_ub._size;
76316613Spfg	p = reallocarray(fp->_ub._base, i, 2);
771573Srgrimes	if (p == NULL)
781573Srgrimes		return (EOF);
791573Srgrimes	/* no overlap (hence can use memcpy) because we doubled the size */
801573Srgrimes	(void)memcpy((void *)(p + i), (void *)p, (size_t)i);
811573Srgrimes	fp->_p = p + i;
821573Srgrimes	fp->_ub._base = p;
83316613Spfg	fp->_ub._size = i * 2;
841573Srgrimes	return (0);
851573Srgrimes}
861573Srgrimes
8771579Sdeischen/*
8871579Sdeischen * MT-safe version
8971579Sdeischen */
9013545Sjulianint
9171579Sdeischenungetc(int c, FILE *fp)
921573Srgrimes{
9371579Sdeischen	int ret;
9471579Sdeischen
951573Srgrimes	if (!__sdidinit)
961573Srgrimes		__sinit();
97320942Skib	FLOCKFILE_CANCELSAFE(fp);
98126809Stjr	ORIENT(fp, -1);
9971579Sdeischen	ret = __ungetc(c, fp);
100320942Skib	FUNLOCKFILE_CANCELSAFE();
10171579Sdeischen	return (ret);
10271579Sdeischen}
10371579Sdeischen
10471579Sdeischen/*
10571579Sdeischen * Non-MT-safe version
10671579Sdeischen */
10771579Sdeischenint
10871579Sdeischen__ungetc(int c, FILE *fp)
10971579Sdeischen{
110101776Stjr
11171579Sdeischen	if (c == EOF)
11271579Sdeischen		return (EOF);
1131573Srgrimes	if ((fp->_flags & __SRD) == 0) {
1141573Srgrimes		/*
1151573Srgrimes		 * Not already reading: no good unless reading-and-writing.
1161573Srgrimes		 * Otherwise, flush any current write stuff.
1171573Srgrimes		 */
11871579Sdeischen		if ((fp->_flags & __SRW) == 0)
1191573Srgrimes			return (EOF);
1201573Srgrimes		if (fp->_flags & __SWR) {
12171579Sdeischen			if (__sflush(fp))
1221573Srgrimes				return (EOF);
1231573Srgrimes			fp->_flags &= ~__SWR;
1241573Srgrimes			fp->_w = 0;
1251573Srgrimes			fp->_lbfsize = 0;
1261573Srgrimes		}
1271573Srgrimes		fp->_flags |= __SRD;
1281573Srgrimes	}
1291573Srgrimes	c = (unsigned char)c;
1301573Srgrimes
1311573Srgrimes	/*
1321573Srgrimes	 * If we are in the middle of ungetc'ing, just continue.
1331573Srgrimes	 * This may require expanding the current ungetc buffer.
1341573Srgrimes	 */
1351573Srgrimes	if (HASUB(fp)) {
13671579Sdeischen		if (fp->_r >= fp->_ub._size && __submore(fp))
1371573Srgrimes			return (EOF);
1381573Srgrimes		*--fp->_p = c;
1391573Srgrimes		fp->_r++;
1401573Srgrimes		return (c);
1411573Srgrimes	}
1421573Srgrimes	fp->_flags &= ~__SEOF;
1431573Srgrimes
1441573Srgrimes	/*
1451573Srgrimes	 * If we can handle this by simply backing up, do so,
1461573Srgrimes	 * but never replace the original character.
1471573Srgrimes	 * (This makes sscanf() work when scanning `const' data.)
1481573Srgrimes	 */
1491573Srgrimes	if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
1501573Srgrimes	    fp->_p[-1] == c) {
1511573Srgrimes		fp->_p--;
1521573Srgrimes		fp->_r++;
1531573Srgrimes		return (c);
1541573Srgrimes	}
1551573Srgrimes
1561573Srgrimes	/*
1571573Srgrimes	 * Create an ungetc buffer.
1581573Srgrimes	 * Initially, we will use the `reserve' buffer.
1591573Srgrimes	 */
1601573Srgrimes	fp->_ur = fp->_r;
161178287Sjhb	fp->_up = fp->_p;
1621573Srgrimes	fp->_ub._base = fp->_ubuf;
1631573Srgrimes	fp->_ub._size = sizeof(fp->_ubuf);
1641573Srgrimes	fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
1651573Srgrimes	fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
1661573Srgrimes	fp->_r = 1;
1671573Srgrimes	return (c);
1681573Srgrimes}
169