ungetwc.c 517 字节
Newer Older
R
Rich Felker 已提交
1
#include "stdio_impl.h"
R
Rich Felker 已提交
2 3 4 5
#include <wchar.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
R
Rich Felker 已提交
6 7 8 9 10 11 12 13 14 15

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

	FLOCK(f);

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

16
	if (!f->rpos) __toread(f);
17
	if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
18
	    (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
R
Rich Felker 已提交
19
		FUNLOCK(f);
20
		return WEOF;
R
Rich Felker 已提交
21 22 23 24 25 26 27 28 29 30
	}

	if (isascii(c)) *--f->rpos = c;
	else memcpy(f->rpos -= l, mbc, l);

	f->flags &= ~F_EOF;

	FUNLOCK(f);
	return c;
}