Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / ungetc.c
blob8b10c8218616b6f9f5a3f0c5b0afa5faba9ecdfa
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
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.
18 FUNCTION
19 <<ungetc>>---push data back into a stream
21 INDEX
22 ungetc
23 INDEX
24 _ungetc_r
26 SYNOPSIS
27 #include <stdio.h>
28 int ungetc(int <[c]>, FILE *<[stream]>);
30 int _ungetc_r(struct _reent *<[reent]>, int <[c]>, FILE *<[stream]>);
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>>.
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.
46 The alternate function <<_ungetc_r>> is a reentrant version. The
47 extra argument <[reent]> is a pointer to a reentrancy structure.
49 RETURNS
50 The character pushed back, or <<EOF>> on error.
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.
57 Supporting OS subroutines required: <<sbrk>>.
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid[] = "%W% (Berkeley) %G%";
62 #endif /* LIBC_SCCS and not lint */
64 #include <_ansi.h>
65 #include <reent.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include "local.h"
72 * Expand the ungetc buffer `in place'. That is, adjust fp->_p when
73 * the buffer moves, so that it points the same distance from the end,
74 * and move the bytes in the buffer around as necessary so that they
75 * are all at the end (stack-style).
78 /*static*/
79 int
80 __submore (struct _reent *rptr,
81 register FILE *fp)
83 register int i;
84 register unsigned char *p;
86 if (fp->_ub._base == fp->_ubuf)
89 * Get a new buffer (rather than expanding the old one).
91 if ((p = (unsigned char *) _malloc_r (rptr, (size_t) BUFSIZ)) == NULL)
92 return EOF;
93 fp->_ub._base = p;
94 fp->_ub._size = BUFSIZ;
95 p += BUFSIZ - sizeof (fp->_ubuf);
96 for (i = sizeof (fp->_ubuf); --i >= 0;)
97 p[i] = fp->_ubuf[i];
98 fp->_p = p;
99 return 0;
101 i = fp->_ub._size;
102 p = (unsigned char *) _realloc_r (rptr, (void *) (fp->_ub._base), i << 1);
103 if (p == NULL)
104 return EOF;
105 (void) memcpy ((void *) (p + i), (void *) p, (size_t) i);
106 fp->_p = p + i;
107 fp->_ub._base = p;
108 fp->_ub._size = i << 1;
109 return 0;
113 _ungetc_r (struct _reent *rptr,
114 int c,
115 register FILE *fp)
117 if (c == EOF)
118 return (EOF);
120 /* Ensure stdio has been initialized.
121 ??? Might be able to remove this as some other stdio routine should
122 have already been called to get the char we are un-getting. */
124 CHECK_INIT (rptr, fp);
126 _newlib_flockfile_start (fp);
128 /* After ungetc, we won't be at eof anymore */
129 fp->_flags &= ~__SEOF;
131 if ((fp->_flags & __SRD) == 0)
134 * Not already reading: no good unless reading-and-writing.
135 * Otherwise, flush any current write stuff.
137 if ((fp->_flags & __SRW) == 0)
139 _newlib_flockfile_exit (fp);
140 return EOF;
142 if (fp->_flags & __SWR)
144 if (_fflush_r (rptr, fp))
146 _newlib_flockfile_exit (fp);
147 return EOF;
149 fp->_flags &= ~__SWR;
150 fp->_w = 0;
151 fp->_lbfsize = 0;
153 fp->_flags |= __SRD;
155 c = (unsigned char) c;
158 * If we are in the middle of ungetc'ing, just continue.
159 * This may require expanding the current ungetc buffer.
162 if (HASUB (fp))
164 if (fp->_r >= fp->_ub._size && __submore (rptr, fp))
166 _newlib_flockfile_exit (fp);
167 return EOF;
169 *--fp->_p = c;
170 fp->_r++;
171 _newlib_flockfile_exit (fp);
172 return c;
176 * If we can handle this by simply backing up, do so,
177 * but never replace the original character.
178 * (This makes sscanf() work when scanning `const' data.)
181 if (fp->_bf._base != NULL && fp->_p > fp->_bf._base && fp->_p[-1] == c)
183 fp->_p--;
184 fp->_r++;
185 _newlib_flockfile_exit (fp);
186 return c;
190 * Create an ungetc buffer.
191 * Initially, we will use the `reserve' buffer.
194 fp->_ur = fp->_r;
195 fp->_up = fp->_p;
196 fp->_ub._base = fp->_ubuf;
197 fp->_ub._size = sizeof (fp->_ubuf);
198 fp->_ubuf[sizeof (fp->_ubuf) - 1] = c;
199 fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1];
200 fp->_r = 1;
201 _newlib_flockfile_end (fp);
202 return c;
205 #ifndef _REENT_ONLY
207 ungetc (int c,
208 register FILE *fp)
210 return _ungetc_r (_REENT, c, fp);
212 #endif /* !_REENT_ONLY */