xref: /netbsd-src/lib/libc/stdio/ungetc.c (revision 526d942790cc352b6ea7931f9884cc6bdbf20404)
1*526d9427Schristos /*	$NetBSD: ungetc.c,v 1.17 2012/03/15 18:22:30 christos Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*-
4*526d9427Schristos  * Copyright c 1990, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Chris Torek.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
350c339c44Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)ungetc.c	8.2 (Berkeley) 11/3/93";
390c339c44Schristos #else
40*526d9427Schristos __RCSID("$NetBSD: ungetc.c,v 1.17 2012/03/15 18:22:30 christos Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd 
44b48252f3Slukem #include <assert.h>
45b48252f3Slukem #include <errno.h>
4661f28255Scgd #include <stdio.h>
4761f28255Scgd #include <stdlib.h>
4861f28255Scgd #include <string.h>
49100b813fSkleink #include "reentrant.h"
503fdac2b8Sthorpej #include "local.h"
5161f28255Scgd 
5261f28255Scgd /*
5361f28255Scgd  * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
5461f28255Scgd  * the buffer moves, so that it points the same distance from the end,
5561f28255Scgd  * and move the bytes in the buffer around as necessary so that they
5661f28255Scgd  * are all at the end (stack-style).
5761f28255Scgd  */
58da8d0ce8Sjtc static int
__submore(FILE * fp)59*526d9427Schristos __submore(FILE *fp)
6061f28255Scgd {
61c8bafd62Sperry 	int i;
62c8bafd62Sperry 	unsigned char *p;
6361f28255Scgd 
64b48252f3Slukem 	_DIAGASSERT(fp != NULL);
65b48252f3Slukem 
6617f3654aSyamt 	if (_UB(fp)._base == fp->_ubuf) {
6761f28255Scgd 		/*
6861f28255Scgd 		 * Get a new buffer (rather than expanding the old one).
6961f28255Scgd 		 */
70cfbb35edSchristos 		if ((p = malloc((size_t)BUFSIZ)) == NULL)
71*526d9427Schristos 			return EOF;
7217f3654aSyamt 		_UB(fp)._base = p;
7317f3654aSyamt 		_UB(fp)._size = BUFSIZ;
7461f28255Scgd 		p += BUFSIZ - sizeof(fp->_ubuf);
7561f28255Scgd 		for (i = sizeof(fp->_ubuf); --i >= 0;)
7661f28255Scgd 			p[i] = fp->_ubuf[i];
7761f28255Scgd 		fp->_p = p;
78*526d9427Schristos 		return 0;
7961f28255Scgd 	}
8017f3654aSyamt 	i = _UB(fp)._size;
81cfbb35edSchristos 	p = realloc(_UB(fp)._base, (size_t)(i << 1));
8261f28255Scgd 	if (p == NULL)
83*526d9427Schristos 		return EOF;
84255db7b2Sjtc 	/* no overlap (hence can use memcpy) because we doubled the size */
85cfbb35edSchristos 	(void)memcpy((p + i), p, (size_t)i);
8661f28255Scgd 	fp->_p = p + i;
8717f3654aSyamt 	_UB(fp)._base = p;
8817f3654aSyamt 	_UB(fp)._size = i << 1;
89*526d9427Schristos 	return 0;
9061f28255Scgd }
9161f28255Scgd 
92da8d0ce8Sjtc int
ungetc(int c,FILE * fp)93*526d9427Schristos ungetc(int c, FILE *fp)
9461f28255Scgd {
95b48252f3Slukem 
96b48252f3Slukem 	_DIAGASSERT(fp != NULL);
97b48252f3Slukem 
9861f28255Scgd 	if (c == EOF)
99*526d9427Schristos 		return EOF;
10061f28255Scgd 	if (!__sdidinit)
10161f28255Scgd 		__sinit();
102100b813fSkleink 	FLOCKFILE(fp);
10317f3654aSyamt 	_SET_ORIENTATION(fp, -1);
10461f28255Scgd 	if ((fp->_flags & __SRD) == 0) {
10561f28255Scgd 		/*
10661f28255Scgd 		 * Not already reading: no good unless reading-and-writing.
10761f28255Scgd 		 * Otherwise, flush any current write stuff.
10861f28255Scgd 		 */
109100b813fSkleink 		if ((fp->_flags & __SRW) == 0) {
110100b813fSkleink 			FUNLOCKFILE(fp);
111*526d9427Schristos 			return EOF;
112100b813fSkleink 		}
11361f28255Scgd 		if (fp->_flags & __SWR) {
114100b813fSkleink 			if (__sflush(fp)) {
115100b813fSkleink 				FUNLOCKFILE(fp);
116*526d9427Schristos 				return EOF;
117100b813fSkleink 			}
11861f28255Scgd 			fp->_flags &= ~__SWR;
11961f28255Scgd 			fp->_w = 0;
12061f28255Scgd 			fp->_lbfsize = 0;
12161f28255Scgd 		}
12261f28255Scgd 		fp->_flags |= __SRD;
12361f28255Scgd 	}
12461f28255Scgd 	c = (unsigned char)c;
12561f28255Scgd 
12661f28255Scgd 	/*
12761f28255Scgd 	 * If we are in the middle of ungetc'ing, just continue.
12861f28255Scgd 	 * This may require expanding the current ungetc buffer.
12961f28255Scgd 	 */
13061f28255Scgd 	if (HASUB(fp)) {
13117f3654aSyamt 		if (fp->_r >= _UB(fp)._size && __submore(fp)) {
132100b813fSkleink 			FUNLOCKFILE(fp);
133*526d9427Schristos 			return EOF;
134100b813fSkleink 		}
13561f28255Scgd 		*--fp->_p = c;
13661f28255Scgd 		fp->_r++;
137100b813fSkleink 		FUNLOCKFILE(fp);
138*526d9427Schristos 		return c;
13961f28255Scgd 	}
140255db7b2Sjtc 	fp->_flags &= ~__SEOF;
14161f28255Scgd 
14261f28255Scgd 	/*
14361f28255Scgd 	 * If we can handle this by simply backing up, do so,
14461f28255Scgd 	 * but never replace the original character.
14561f28255Scgd 	 * (This makes sscanf() work when scanning `const' data.)
14661f28255Scgd 	 */
14761f28255Scgd 	if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
14861f28255Scgd 	    fp->_p[-1] == c) {
14961f28255Scgd 		fp->_p--;
15061f28255Scgd 		fp->_r++;
151100b813fSkleink 		FUNLOCKFILE(fp);
152*526d9427Schristos 		return c;
15361f28255Scgd 	}
15461f28255Scgd 
15561f28255Scgd 	/*
15661f28255Scgd 	 * Create an ungetc buffer.
15761f28255Scgd 	 * Initially, we will use the `reserve' buffer.
15861f28255Scgd 	 */
15961f28255Scgd 	fp->_ur = fp->_r;
16061f28255Scgd 	fp->_up = fp->_p;
16117f3654aSyamt 	_UB(fp)._base = fp->_ubuf;
16217f3654aSyamt 	_UB(fp)._size = sizeof(fp->_ubuf);
16361f28255Scgd 	fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
16461f28255Scgd 	fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
16561f28255Scgd 	fp->_r = 1;
166100b813fSkleink 	FUNLOCKFILE(fp);
167*526d9427Schristos 	return c;
16861f28255Scgd }
169