Files
semi-libc/src/stdio/fgetwc.c
T
Rich Felker 835f9f950e clean up stdio_impl.h
this header evolved to facilitate the extremely lazy practice of
omitting explicit includes of the necessary headers in individual
stdio source files; not only was this sloppy, but it also increased
build time.

now, stdio_impl.h is only including the headers it needs for its own
use; any further headers needed by source files are included directly
where needed.
2012-11-08 16:39:41 -05:00

53 lines
930 B
C

#include "stdio_impl.h"
#include <wchar.h>
#include <errno.h>
wint_t __fgetwc_unlocked(FILE *f)
{
mbstate_t st = { 0 };
wchar_t wc;
int c;
unsigned char b;
size_t l;
f->mode |= f->mode+1;
/* Convert character from buffer if possible */
if (f->rpos < f->rend) {
l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st);
if (l+2 >= 2) {
f->rpos += l + !l; /* l==0 means 1 byte, null */
return wc;
}
if (l == -1) {
f->rpos++;
return WEOF;
}
} else l = -2;
/* Convert character byte-by-byte */
while (l == -2) {
b = c = getc_unlocked(f);
if (c < 0) {
if (!mbsinit(&st)) errno = EILSEQ;
return WEOF;
}
l = mbrtowc(&wc, (void *)&b, 1, &st);
if (l == -1) return WEOF;
}
return wc;
}
wint_t fgetwc(FILE *f)
{
wint_t c;
FLOCK(f);
c = __fgetwc_unlocked(f);
FUNLOCK(f);
return c;
}
weak_alias(__fgetwc_unlocked, fgetwc_unlocked);
weak_alias(__fgetwc_unlocked, getwc_unlocked);