Update contrib.
3 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
8 * Copyright (c) 1990 The Regents of the University of California.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that the above copyright notice and this paragraph are
13 * duplicated in all such forms and that any documentation,
14 * advertising materials, and other materials related to such
15 * distribution and use acknowledge that the software was developed
16 * by the University of California, Berkeley. The name of the
17 * University may not be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 * Expand the ungetc buffer `in place'. That is, adjust fp->_p when
31 * the buffer moves, so that it points the same distance from the end,
32 * and move the bytes in the buffer around as necessary so that they
33 * are all at the end (stack-style).
38 __submore (register FILE *fp)
41 register unsigned char *p;
43 if (fp->_ub._base == fp->_ubuf)
46 * Get a new buffer (rather than expanding the old one).
48 if ((p = (unsigned char *) _malloc_r (fp->_data, (size_t) BUFSIZ)) == NULL)
51 fp->_ub._size = BUFSIZ;
52 p += BUFSIZ - sizeof (fp->_ubuf);
53 for (i = sizeof (fp->_ubuf); --i >= 0;)
59 p = (unsigned char *) _realloc_r (fp->_data, (void*) (fp->_ub._base), i << 1);
62 (void) memcpy ((void *) (p + i), (void *) p, (size_t) i);
65 fp->_ub._size = i << 1;
70 Push a character back into stream.
71 @return If successful, the character putted is returned.
72 Otherwise EOF is returned and the stream remains unchanged.
73 @param c character to be pushed.
74 @param fp pointer to an open file.
77 ungetc (int c,register FILE *fp)
82 /* Ensure stdio has been initialized.
83 ??? Might be able to remove this as some other stdio routine should
84 have already been called to get the char we are un-getting. */
88 /* After ungetc, we won't be at eof anymore */
89 fp->_flags &= ~__SEOF;
91 if ((fp->_flags & __SRD) == 0)
94 * Not already reading: no good unless reading-and-writing.
95 * Otherwise, flush any current write stuff.
97 if ((fp->_flags & __SRW) == 0)
99 if (fp->_flags & __SWR)
103 fp->_flags &= ~__SWR;
109 c = (unsigned char) c;
112 * If we are in the middle of ungetc'ing, just continue.
113 * This may require expanding the current ungetc buffer.
118 if (fp->_r >= fp->_ub._size && __submore (fp))
120 *--fp->_p = (unsigned char)c;
126 * If we can handle this by simply backing up, do so,
127 * but never replace the original character.
128 * (This makes sscanf() work when scanning `const' data.)
131 if (fp->_bf._base != NULL && fp->_p > fp->_bf._base && fp->_p[-1] == c)
139 * Create an ungetc buffer.
140 * Initially, we will use the `reserve' buffer.
145 fp->_ub._base = fp->_ubuf;
146 fp->_ub._size = sizeof (fp->_ubuf);
147 fp->_ubuf[sizeof (fp->_ubuf) - 1] = (unsigned char)c;
148 fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1];