1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 <<ungetc>>---push data back into a stream
20 
21 INDEX
22 	ungetc
23 INDEX
24 	_ungetc_r
25 
26 SYNOPSIS
27 	#include <stdio.h>
28 	int ungetc(int <[c]>, FILE *<[stream]>);
29 
30 	int ungetc( int <[c]>, FILE *<[stream]>);
31 
32 DESCRIPTION
33 <<ungetc>> is used to return bytes back to <[stream]> to be read again.
34 If <[c]> is EOF, the stream is unchanged.  Otherwise, the unsigned
35 char <[c]> is put back on the stream, and subsequent reads will see
36 the bytes pushed back in reverse order.  Pushed byes are lost if the
37 stream is repositioned, such as by <<fseek>>, <<fsetpos>>, or
38 <<rewind>>.
39 
40 The underlying file is not changed, but it is possible to push back
41 something different than what was originally read.  Ungetting a
42 character will clear the end-of-stream marker, and decrement the file
43 position indicator.  Pushing back beyond the beginning of a file gives
44 unspecified behavior.
45 
46 The alternate function <<_ungetc_r>> is a reentrant version.  The
47 extra argument <[reent]> is a pointer to a reentrancy structure.
48 
49 RETURNS
50 The character pushed back, or <<EOF>> on error.
51 
52 PORTABILITY
53 ANSI C requires <<ungetc>>, but only requires a pushback buffer of one
54 byte; although this implementation can handle multiple bytes, not all
55 can.  Pushing back a signed char is a common application bug.
56 
57 Supporting OS subroutines required: <<sbrk>>.
58 */
59 
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid[] = "%W% (Berkeley) %G%";
62 #endif /* LIBC_SCCS and not lint */
63 
64 #define _DEFAULT_SOURCE
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include "local.h"
69 
70 /*
71  * Expand the ungetc buffer `in place'.  That is, adjust fp->_p when
72  * the buffer moves, so that it points the same distance from the end,
73  * and move the bytes in the buffer around as necessary so that they
74  * are all at the end (stack-style).
75  */
76 
77 /*static*/
78 int
__submore(register FILE * fp)79 __submore (
80        register FILE *fp)
81 {
82   register int i;
83   register unsigned char *p;
84 
85   if (fp->_ub._base == fp->_ubuf)
86     {
87       /*
88        * Get a new buffer (rather than expanding the old one).
89        */
90       if ((p = (unsigned char *) malloc ((size_t) BUFSIZ)) == NULL)
91 	return EOF;
92       fp->_ub._base = p;
93       fp->_ub._size = BUFSIZ;
94       p += BUFSIZ - sizeof (fp->_ubuf);
95       for (i = sizeof (fp->_ubuf); --i >= 0;)
96 	p[i] = fp->_ubuf[i];
97       fp->_p = p;
98       return 0;
99     }
100   i = fp->_ub._size;
101   p = (unsigned char *) realloc ((void *) (fp->_ub._base), i << 1);
102   if (p == NULL)
103     return EOF;
104   (void) memcpy ((void *) (p + i), (void *) p, (size_t) i);
105   fp->_p = p + i;
106   fp->_ub._base = p;
107   fp->_ub._size = i << 1;
108   return 0;
109 }
110 
111 int
ungetc(int c,register FILE * fp)112 ungetc (
113        int c,
114        register FILE *fp)
115 {
116   if (c == EOF)
117     return (EOF);
118 
119   /* Ensure stdio has been initialized.
120      ??? Might be able to remove this as some other stdio routine should
121      have already been called to get the char we are un-getting.  */
122 
123   CHECK_INIT (rptr, fp);
124 
125   _newlib_flockfile_start (fp);
126 
127   /* After ungetc, we won't be at eof anymore */
128   fp->_flags &= ~__SEOF;
129 
130   if ((fp->_flags & __SRD) == 0)
131     {
132       /*
133        * Not already reading: no good unless reading-and-writing.
134        * Otherwise, flush any current write stuff.
135        */
136       if ((fp->_flags & __SRW) == 0)
137         {
138           _newlib_flockfile_exit (fp);
139           return EOF;
140         }
141       if (fp->_flags & __SWR)
142 	{
143 	  if (fflush ( fp))
144             {
145               _newlib_flockfile_exit (fp);
146               return EOF;
147             }
148 	  fp->_flags &= ~__SWR;
149 	  fp->_w = 0;
150 	  fp->_lbfsize = 0;
151 	}
152       fp->_flags |= __SRD;
153     }
154   c = (unsigned char) c;
155 
156   /*
157    * If we are in the middle of ungetc'ing, just continue.
158    * This may require expanding the current ungetc buffer.
159    */
160 
161   if (HASUB (fp))
162     {
163       if (fp->_r >= fp->_ub._size && __submore (fp))
164         {
165           _newlib_flockfile_exit (fp);
166           return EOF;
167         }
168       *--fp->_p = c;
169       fp->_r++;
170       _newlib_flockfile_exit (fp);
171       return c;
172     }
173 
174   /*
175    * If we can handle this by simply backing up, do so,
176    * but never replace the original character.
177    * (This makes sscanf() work when scanning `const' data.)
178    */
179 
180   if (fp->_bf._base != NULL && fp->_p > fp->_bf._base && fp->_p[-1] == c)
181     {
182       fp->_p--;
183       fp->_r++;
184       _newlib_flockfile_exit (fp);
185       return c;
186     }
187 
188   /*
189    * Create an ungetc buffer.
190    * Initially, we will use the `reserve' buffer.
191    */
192 
193   fp->_ur = fp->_r;
194   fp->_up = fp->_p;
195   fp->_ub._base = fp->_ubuf;
196   fp->_ub._size = sizeof (fp->_ubuf);
197   fp->_ubuf[sizeof (fp->_ubuf) - 1] = c;
198   fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1];
199   fp->_r = 1;
200   _newlib_flockfile_end (fp);
201   return c;
202 }
203