1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7 
8 #include "_stdio.h"
9 
10 /* Note: This is the application-callable ungetwc.  If wscanf calls this, it
11  * should also set stream->__ungot[1] to 0 if this is the only ungot, as well
12  * as reset stream->__ungot_width[1] for use by _stdio_adjpos().
13  */
14 
ungetwc(wint_t c,register FILE * stream)15 wint_t ungetwc(wint_t c, register FILE *stream)
16 {
17 	__STDIO_AUTO_THREADLOCK_VAR;
18 
19 	__STDIO_AUTO_THREADLOCK(stream);
20 	__STDIO_STREAM_VALIDATE(stream); /* debugging only */
21 
22 	/* Note: Even if c == WEOF, we need to initialize/verify the
23 	 * stream's orientation and ensure the stream is in reading
24 	 * mode (if readable and properly oriented). */
25 	if ((!__STDIO_STREAM_IS_WIDE_READING(stream)
26 		 && __STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_WIDE))
27 		|| ((stream->__modeflags & __FLAG_UNGOT)
28 			&& ((stream->__modeflags & 1) || stream->__ungot[1]))
29 		|| (c == WEOF)
30 		) {
31 		c = WEOF;
32 	} else {
33 		/* In the wide case, getc macros should already be disabled. */
34 		/* 	__STDIO_STREAM_DISABLE_GETC(stream); */
35 
36 		/* Flag this as a user ungot, as scanf does the necessary fixup. */
37 		stream->__ungot[1] = 1;
38 		stream->__ungot[(++stream->__modeflags) & 1] = c;
39 		/* Note: ungot_width is handled by fgetwc. */
40 
41 		__STDIO_STREAM_CLEAR_EOF(stream); /* Must clear end-of-file flag. */
42 	}
43 
44 	__STDIO_STREAM_VALIDATE(stream);
45 	__STDIO_AUTO_THREADUNLOCK(stream);
46 
47 	return c;
48 }
49 libc_hidden_def(ungetwc)
50