xref: /minix3/lib/libc/stdio/ungetc.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: ungetc.c,v 1.17 2012/03/15 18:22:30 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
4*f14fb602SLionel Sambuc  * Copyright c 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
33b7061124SArun Thomas  */
34b7061124SArun Thomas 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)ungetc.c	8.2 (Berkeley) 11/3/93";
392fe8fb19SBen Gras #else
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: ungetc.c,v 1.17 2012/03/15 18:22:30 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <assert.h>
452fe8fb19SBen Gras #include <errno.h>
46b7061124SArun Thomas #include <stdio.h>
472fe8fb19SBen Gras #include <stdlib.h>
482fe8fb19SBen Gras #include <string.h>
492fe8fb19SBen Gras #include "reentrant.h"
502fe8fb19SBen Gras #include "local.h"
51b7061124SArun Thomas 
522fe8fb19SBen Gras /*
532fe8fb19SBen Gras  * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
542fe8fb19SBen Gras  * the buffer moves, so that it points the same distance from the end,
552fe8fb19SBen Gras  * and move the bytes in the buffer around as necessary so that they
562fe8fb19SBen Gras  * are all at the end (stack-style).
572fe8fb19SBen Gras  */
582fe8fb19SBen Gras static int
__submore(FILE * fp)59*f14fb602SLionel Sambuc __submore(FILE *fp)
60b7061124SArun Thomas {
612fe8fb19SBen Gras 	int i;
62b7061124SArun Thomas 	unsigned char *p;
63b7061124SArun Thomas 
642fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
652fe8fb19SBen Gras 
662fe8fb19SBen Gras 	if (_UB(fp)._base == fp->_ubuf) {
672fe8fb19SBen Gras 		/*
682fe8fb19SBen Gras 		 * Get a new buffer (rather than expanding the old one).
692fe8fb19SBen Gras 		 */
702fe8fb19SBen Gras 		if ((p = malloc((size_t)BUFSIZ)) == NULL)
71*f14fb602SLionel Sambuc 			return EOF;
722fe8fb19SBen Gras 		_UB(fp)._base = p;
732fe8fb19SBen Gras 		_UB(fp)._size = BUFSIZ;
742fe8fb19SBen Gras 		p += BUFSIZ - sizeof(fp->_ubuf);
752fe8fb19SBen Gras 		for (i = sizeof(fp->_ubuf); --i >= 0;)
762fe8fb19SBen Gras 			p[i] = fp->_ubuf[i];
772fe8fb19SBen Gras 		fp->_p = p;
78*f14fb602SLionel Sambuc 		return 0;
79b7061124SArun Thomas 	}
802fe8fb19SBen Gras 	i = _UB(fp)._size;
812fe8fb19SBen Gras 	p = realloc(_UB(fp)._base, (size_t)(i << 1));
822fe8fb19SBen Gras 	if (p == NULL)
83*f14fb602SLionel Sambuc 		return EOF;
842fe8fb19SBen Gras 	/* no overlap (hence can use memcpy) because we doubled the size */
852fe8fb19SBen Gras 	(void)memcpy((p + i), p, (size_t)i);
862fe8fb19SBen Gras 	fp->_p = p + i;
872fe8fb19SBen Gras 	_UB(fp)._base = p;
882fe8fb19SBen Gras 	_UB(fp)._size = i << 1;
89*f14fb602SLionel Sambuc 	return 0;
902fe8fb19SBen Gras }
912fe8fb19SBen Gras 
922fe8fb19SBen Gras int
ungetc(int c,FILE * fp)93*f14fb602SLionel Sambuc ungetc(int c, FILE *fp)
942fe8fb19SBen Gras {
952fe8fb19SBen Gras 
962fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
972fe8fb19SBen Gras 
982fe8fb19SBen Gras 	if (c == EOF)
99*f14fb602SLionel Sambuc 		return EOF;
1002fe8fb19SBen Gras 	if (!__sdidinit)
1012fe8fb19SBen Gras 		__sinit();
1022fe8fb19SBen Gras 	FLOCKFILE(fp);
1032fe8fb19SBen Gras 	_SET_ORIENTATION(fp, -1);
1042fe8fb19SBen Gras 	if ((fp->_flags & __SRD) == 0) {
1052fe8fb19SBen Gras 		/*
1062fe8fb19SBen Gras 		 * Not already reading: no good unless reading-and-writing.
1072fe8fb19SBen Gras 		 * Otherwise, flush any current write stuff.
1082fe8fb19SBen Gras 		 */
1092fe8fb19SBen Gras 		if ((fp->_flags & __SRW) == 0) {
1102fe8fb19SBen Gras 			FUNLOCKFILE(fp);
111*f14fb602SLionel Sambuc 			return EOF;
1122fe8fb19SBen Gras 		}
1132fe8fb19SBen Gras 		if (fp->_flags & __SWR) {
1142fe8fb19SBen Gras 			if (__sflush(fp)) {
1152fe8fb19SBen Gras 				FUNLOCKFILE(fp);
116*f14fb602SLionel Sambuc 				return EOF;
1172fe8fb19SBen Gras 			}
1182fe8fb19SBen Gras 			fp->_flags &= ~__SWR;
1192fe8fb19SBen Gras 			fp->_w = 0;
1202fe8fb19SBen Gras 			fp->_lbfsize = 0;
1212fe8fb19SBen Gras 		}
1222fe8fb19SBen Gras 		fp->_flags |= __SRD;
1232fe8fb19SBen Gras 	}
1242fe8fb19SBen Gras 	c = (unsigned char)c;
1252fe8fb19SBen Gras 
1262fe8fb19SBen Gras 	/*
1272fe8fb19SBen Gras 	 * If we are in the middle of ungetc'ing, just continue.
1282fe8fb19SBen Gras 	 * This may require expanding the current ungetc buffer.
1292fe8fb19SBen Gras 	 */
1302fe8fb19SBen Gras 	if (HASUB(fp)) {
1312fe8fb19SBen Gras 		if (fp->_r >= _UB(fp)._size && __submore(fp)) {
1322fe8fb19SBen Gras 			FUNLOCKFILE(fp);
133*f14fb602SLionel Sambuc 			return EOF;
1342fe8fb19SBen Gras 		}
1352fe8fb19SBen Gras 		*--fp->_p = c;
1362fe8fb19SBen Gras 		fp->_r++;
1372fe8fb19SBen Gras 		FUNLOCKFILE(fp);
138*f14fb602SLionel Sambuc 		return c;
1392fe8fb19SBen Gras 	}
1402fe8fb19SBen Gras 	fp->_flags &= ~__SEOF;
1412fe8fb19SBen Gras 
1422fe8fb19SBen Gras 	/*
1432fe8fb19SBen Gras 	 * If we can handle this by simply backing up, do so,
1442fe8fb19SBen Gras 	 * but never replace the original character.
1452fe8fb19SBen Gras 	 * (This makes sscanf() work when scanning `const' data.)
1462fe8fb19SBen Gras 	 */
1472fe8fb19SBen Gras 	if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
1482fe8fb19SBen Gras 	    fp->_p[-1] == c) {
1492fe8fb19SBen Gras 		fp->_p--;
1502fe8fb19SBen Gras 		fp->_r++;
1512fe8fb19SBen Gras 		FUNLOCKFILE(fp);
152*f14fb602SLionel Sambuc 		return c;
1532fe8fb19SBen Gras 	}
1542fe8fb19SBen Gras 
1552fe8fb19SBen Gras 	/*
1562fe8fb19SBen Gras 	 * Create an ungetc buffer.
1572fe8fb19SBen Gras 	 * Initially, we will use the `reserve' buffer.
1582fe8fb19SBen Gras 	 */
1592fe8fb19SBen Gras 	fp->_ur = fp->_r;
1602fe8fb19SBen Gras 	fp->_up = fp->_p;
1612fe8fb19SBen Gras 	_UB(fp)._base = fp->_ubuf;
1622fe8fb19SBen Gras 	_UB(fp)._size = sizeof(fp->_ubuf);
1632fe8fb19SBen Gras 	fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
1642fe8fb19SBen Gras 	fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
1652fe8fb19SBen Gras 	fp->_r = 1;
1662fe8fb19SBen Gras 	FUNLOCKFILE(fp);
167*f14fb602SLionel Sambuc 	return c;
168b7061124SArun Thomas }
169