summaryrefslogtreecommitdiff
path: root/src/stdio/ungetwc.c
blob: 6871d0344a037314727235a99d49262fd77a4fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdio_impl.h"

wint_t ungetwc(wint_t c, FILE *f)
{
	unsigned char mbc[MB_LEN_MAX];
	int l=1;

	if (c == WEOF) return c;

	/* Try conversion early so we can fail without locking if invalid */
	if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
		return WEOF;

	FLOCK(f);

	f->mode |= f->mode+1;

	/* Fail if unreadable or writing and unable to flush */
	if ((f->flags & (F_ERR|F_NORD)) || (f->wpos && __oflow(f))) {
		FUNLOCK(f);
		return EOF;
	}

	/* Clear write mode */
	f->wpos = f->wstop = f->wend = 0;

	/* Put the file in read mode */
	if (!f->rpos) f->rpos = f->rend = f->buf;

	/* If unget buffer is nonempty, fail. */
	if (f->rpos < f->buf) {
		FUNLOCK(f);
		return WEOF;
	}

	/* Put character back into the buffer */
	if (isascii(c)) *--f->rpos = c;
	else memcpy(f->rpos -= l, mbc, l);

	/* Clear EOF */
	f->flags &= ~F_EOF;

	FUNLOCK(f);
	return c;
}