ungetwc.c raw

   1  #include "stdio_impl.h"
   2  #include "locale_impl.h"
   3  #include <wchar.h>
   4  #include <limits.h>
   5  #include <ctype.h>
   6  #include <string.h>
   7  
   8  wint_t ungetwc(wint_t c, FILE *f)
   9  {
  10  	unsigned char mbc[MB_LEN_MAX];
  11  	int l;
  12  	locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  13  
  14  	FLOCK(f);
  15  
  16  	if (f->mode <= 0) fwide(f, 1);
  17  	*ploc = f->locale;
  18  
  19  	if (!f->rpos) __toread(f);
  20  	if (!f->rpos || c == WEOF || (l = wcrtomb((void *)mbc, c, 0)) < 0 ||
  21  	    f->rpos < f->buf - UNGET + l) {
  22  		FUNLOCK(f);
  23  		*ploc = loc;
  24  		return WEOF;
  25  	}
  26  
  27  	if (isascii(c)) *--f->rpos = c;
  28  	else memcpy(f->rpos -= l, mbc, l);
  29  
  30  	f->flags &= ~F_EOF;
  31  
  32  	FUNLOCK(f);
  33  	*ploc = loc;
  34  	return c;
  35  }
  36