xref: /csrg-svn/lib/libc/stdio/ungetc.c (revision 46108)
1*46108Sbostic /*-
2*46108Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46108Sbostic  * All rights reserved.
4*46108Sbostic  *
5*46108Sbostic  * This code is derived from software contributed to Berkeley by
6*46108Sbostic  * Chris Torek.
7*46108Sbostic  *
8*46108Sbostic  * %sccs.include.redist.c%
9*46108Sbostic  */
10*46108Sbostic 
1126665Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*46108Sbostic static char sccsid[] = "@(#)ungetc.c	5.4 (Berkeley) 01/20/91";
13*46108Sbostic #endif /* LIBC_SCCS and not lint */
1415084Ssam 
152037Swnj #include <stdio.h>
16*46108Sbostic #include <stdlib.h>
17*46108Sbostic #include <string.h>
18*46108Sbostic #include "local.h"
192037Swnj 
20*46108Sbostic /*
21*46108Sbostic  * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
22*46108Sbostic  * the buffer moves, so that it points the same distance from the end,
23*46108Sbostic  * and move the bytes in the buffer around as necessary so that they
24*46108Sbostic  * are all at the end (stack-style).
25*46108Sbostic  */
26*46108Sbostic static
27*46108Sbostic __submore(fp)
28*46108Sbostic 	register FILE *fp;
292037Swnj {
30*46108Sbostic 	register int i;
31*46108Sbostic 	register unsigned char *p;
32*46108Sbostic 
33*46108Sbostic 	if (fp->_ub._base == fp->_ubuf) {
34*46108Sbostic 		/*
35*46108Sbostic 		 * Get a new buffer (rather than expanding the old one).
36*46108Sbostic 		 */
37*46108Sbostic 		if ((p = malloc((size_t)BUFSIZ)) == NULL)
38*46108Sbostic 			return (EOF);
39*46108Sbostic 		fp->_ub._base = p;
40*46108Sbostic 		fp->_ub._size = BUFSIZ;
41*46108Sbostic 		p += BUFSIZ - sizeof(fp->_ubuf);
42*46108Sbostic 		for (i = sizeof(fp->_ubuf); --i >= 0;)
43*46108Sbostic 			p[i] = fp->_ubuf[i];
44*46108Sbostic 		fp->_p = p;
45*46108Sbostic 		return (0);
46*46108Sbostic 	}
47*46108Sbostic 	i = fp->_ub._size;
48*46108Sbostic 	p = realloc(fp->_ub._base, i << 1);
49*46108Sbostic 	if (p == NULL)
5017951Sserge 		return (EOF);
51*46108Sbostic 	(void) memcpy((void *)(p + i), (void *)p, (size_t)i);
52*46108Sbostic 	fp->_p = p + i;
53*46108Sbostic 	fp->_ub._base = p;
54*46108Sbostic 	fp->_ub._size = i << 1;
55*46108Sbostic 	return (0);
56*46108Sbostic }
5717951Sserge 
58*46108Sbostic ungetc(c, fp)
59*46108Sbostic 	int c;
60*46108Sbostic 	register FILE *fp;
61*46108Sbostic {
62*46108Sbostic 	if (c == EOF)
63*46108Sbostic 		return (EOF);
64*46108Sbostic 	if (!__sdidinit)
65*46108Sbostic 		__sinit();
66*46108Sbostic 	if ((fp->_flags & __SRD) == 0) {
67*46108Sbostic 		/*
68*46108Sbostic 		 * Not already reading: no good unless reading-and-writing.
69*46108Sbostic 		 * Otherwise, flush any current write stuff.
70*46108Sbostic 		 */
71*46108Sbostic 		if ((fp->_flags & __SRW) == 0)
7226938Sbloom 			return (EOF);
73*46108Sbostic 		if (fp->_flags & __SWR) {
74*46108Sbostic 			if (fflush(fp))
75*46108Sbostic 				return (EOF);
76*46108Sbostic 			fp->_flags &= ~__SWR;
77*46108Sbostic 			fp->_w = 0;
78*46108Sbostic 			fp->_lbfsize = 0;
79*46108Sbostic 		}
80*46108Sbostic 		fp->_flags |= __SRD;
81*46108Sbostic 	}
82*46108Sbostic 	c = (unsigned char)c;
8317951Sserge 
84*46108Sbostic 	/*
85*46108Sbostic 	 * If we are in the middle of ungetc'ing, just continue.
86*46108Sbostic 	 * This may require expanding the current ungetc buffer.
87*46108Sbostic 	 */
88*46108Sbostic 	if (HASUB(fp)) {
89*46108Sbostic 		if (fp->_r >= fp->_ub._size && __submore(fp))
90*46108Sbostic 			return (EOF);
91*46108Sbostic 		*--fp->_p = c;
92*46108Sbostic 		fp->_r++;
93*46108Sbostic 		return (c);
94*46108Sbostic 	}
9517951Sserge 
96*46108Sbostic 	/*
97*46108Sbostic 	 * If we can handle this by simply backing up, do so,
98*46108Sbostic 	 * but never replace the original character.
99*46108Sbostic 	 * (This makes sscanf() work when scanning `const' data.)
100*46108Sbostic 	 */
101*46108Sbostic 	if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
102*46108Sbostic 	    fp->_p[-1] == c) {
103*46108Sbostic 		fp->_p--;
104*46108Sbostic 		fp->_r++;
105*46108Sbostic 		return (c);
106*46108Sbostic 	}
107*46108Sbostic 
108*46108Sbostic 	/*
109*46108Sbostic 	 * Create an ungetc buffer.
110*46108Sbostic 	 * Initially, we will use the `reserve' buffer.
111*46108Sbostic 	 */
112*46108Sbostic 	fp->_ur = fp->_r;
113*46108Sbostic 	fp->_up = fp->_p;
114*46108Sbostic 	fp->_ub._base = fp->_ubuf;
115*46108Sbostic 	fp->_ub._size = sizeof(fp->_ubuf);
116*46108Sbostic 	fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
117*46108Sbostic 	fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
118*46108Sbostic 	fp->_r = 1;
11915084Ssam 	return (c);
1202037Swnj }
121