162f08720SJoerg Sonnenberger /*-
2*0d5acd74SJohn Marino * Copyright (c) 2002-2004 Tim J. Robbins.
362f08720SJoerg Sonnenberger * All rights reserved.
462f08720SJoerg Sonnenberger *
5*0d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
6*0d5acd74SJohn Marino * All rights reserved.
7*0d5acd74SJohn Marino * Portions of this software were developed by David Chisnall
8*0d5acd74SJohn Marino * under sponsorship from the FreeBSD Foundation.
9*0d5acd74SJohn Marino *
1062f08720SJoerg Sonnenberger * Redistribution and use in source and binary forms, with or without
1162f08720SJoerg Sonnenberger * modification, are permitted provided that the following conditions
1262f08720SJoerg Sonnenberger * are met:
1362f08720SJoerg Sonnenberger * 1. Redistributions of source code must retain the above copyright
1462f08720SJoerg Sonnenberger * notice, this list of conditions and the following disclaimer.
1562f08720SJoerg Sonnenberger * 2. Redistributions in binary form must reproduce the above copyright
1662f08720SJoerg Sonnenberger * notice, this list of conditions and the following disclaimer in the
1762f08720SJoerg Sonnenberger * documentation and/or other materials provided with the distribution.
1862f08720SJoerg Sonnenberger *
1962f08720SJoerg Sonnenberger * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2062f08720SJoerg Sonnenberger * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2162f08720SJoerg Sonnenberger * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2262f08720SJoerg Sonnenberger * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2362f08720SJoerg Sonnenberger * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2462f08720SJoerg Sonnenberger * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2562f08720SJoerg Sonnenberger * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2662f08720SJoerg Sonnenberger * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2762f08720SJoerg Sonnenberger * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2862f08720SJoerg Sonnenberger * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2962f08720SJoerg Sonnenberger * SUCH DAMAGE.
3062f08720SJoerg Sonnenberger *
31*0d5acd74SJohn Marino * $FreeBSD: head/lib/libc/stdio/fgetwc.c 234799 2012-04-29 16:28:39Z das $
3262f08720SJoerg Sonnenberger */
3362f08720SJoerg Sonnenberger
34*0d5acd74SJohn Marino
3562f08720SJoerg Sonnenberger #include "namespace.h"
3662f08720SJoerg Sonnenberger #include <errno.h>
3762f08720SJoerg Sonnenberger #include <stdio.h>
38*0d5acd74SJohn Marino #include <stdlib.h>
3962f08720SJoerg Sonnenberger #include <wchar.h>
4062f08720SJoerg Sonnenberger #include "un-namespace.h"
4162f08720SJoerg Sonnenberger #include "libc_private.h"
4262f08720SJoerg Sonnenberger #include "local.h"
43*0d5acd74SJohn Marino #include "mblocal.h"
44*0d5acd74SJohn Marino #include "xlocale_private.h"
4562f08720SJoerg Sonnenberger
46*0d5acd74SJohn Marino /*
47*0d5acd74SJohn Marino * MT-safe version.
48*0d5acd74SJohn Marino */
4962f08720SJoerg Sonnenberger wint_t
fgetwc_l(FILE * fp,locale_t locale)50*0d5acd74SJohn Marino fgetwc_l(FILE *fp, locale_t locale)
5162f08720SJoerg Sonnenberger {
5262f08720SJoerg Sonnenberger wint_t r;
53*0d5acd74SJohn Marino FIX_LOCALE(locale);
5462f08720SJoerg Sonnenberger
5562f08720SJoerg Sonnenberger FLOCKFILE(fp);
56*0d5acd74SJohn Marino ORIENT(fp, 1);
57*0d5acd74SJohn Marino r = __fgetwc(fp, locale);
5862f08720SJoerg Sonnenberger FUNLOCKFILE(fp);
5962f08720SJoerg Sonnenberger
6062f08720SJoerg Sonnenberger return (r);
6162f08720SJoerg Sonnenberger }
6262f08720SJoerg Sonnenberger
63*0d5acd74SJohn Marino wint_t
fgetwc(FILE * fp)64*0d5acd74SJohn Marino fgetwc(FILE *fp)
65*0d5acd74SJohn Marino {
66*0d5acd74SJohn Marino return fgetwc_l(fp, __get_locale());
67*0d5acd74SJohn Marino }
68*0d5acd74SJohn Marino
69*0d5acd74SJohn Marino /*
70*0d5acd74SJohn Marino * Internal (non-MPSAFE) version of fgetwc(). This version takes an
71*0d5acd74SJohn Marino * mbstate_t argument specifying the initial conversion state. For
72*0d5acd74SJohn Marino * wide streams, this should always be fp->_mbstate. On return, *nread
73*0d5acd74SJohn Marino * is set to the number of bytes read.
74*0d5acd74SJohn Marino */
75*0d5acd74SJohn Marino wint_t
__fgetwc_mbs(FILE * fp,mbstate_t * mbs,int * nread,locale_t locale)76*0d5acd74SJohn Marino __fgetwc_mbs(FILE *fp, mbstate_t *mbs, int *nread, locale_t locale)
77*0d5acd74SJohn Marino {
78*0d5acd74SJohn Marino wchar_t wc;
79*0d5acd74SJohn Marino size_t nconv;
80*0d5acd74SJohn Marino struct xlocale_ctype *l = XLOCALE_CTYPE(locale);
81*0d5acd74SJohn Marino
82*0d5acd74SJohn Marino if (fp->pub._r <= 0 && __srefill(fp)) {
83*0d5acd74SJohn Marino *nread = 0;
84*0d5acd74SJohn Marino return (WEOF);
85*0d5acd74SJohn Marino }
86*0d5acd74SJohn Marino if (MB_CUR_MAX == 1) {
87*0d5acd74SJohn Marino /* Fast path for single-byte encodings. */
88*0d5acd74SJohn Marino wc = *fp->pub._p++;
89*0d5acd74SJohn Marino fp->pub._r--;
90*0d5acd74SJohn Marino *nread = 1;
91*0d5acd74SJohn Marino return (wc);
92*0d5acd74SJohn Marino }
93*0d5acd74SJohn Marino *nread = 0;
94*0d5acd74SJohn Marino do {
95*0d5acd74SJohn Marino nconv = l->__mbrtowc(&wc, fp->pub._p, fp->pub._r, mbs);
96*0d5acd74SJohn Marino if (nconv == (size_t)-1)
97*0d5acd74SJohn Marino break;
98*0d5acd74SJohn Marino else if (nconv == (size_t)-2)
99*0d5acd74SJohn Marino continue;
100*0d5acd74SJohn Marino else if (nconv == 0) {
101*0d5acd74SJohn Marino fp->pub._p++;
102*0d5acd74SJohn Marino fp->pub._r--;
103*0d5acd74SJohn Marino (*nread)++;
104*0d5acd74SJohn Marino return (L'\0');
105*0d5acd74SJohn Marino } else {
106*0d5acd74SJohn Marino fp->pub._p += nconv;
107*0d5acd74SJohn Marino fp->pub._r -= nconv;
108*0d5acd74SJohn Marino *nread += nconv;
109*0d5acd74SJohn Marino return (wc);
110*0d5acd74SJohn Marino }
111*0d5acd74SJohn Marino } while (__srefill(fp) == 0);
112*0d5acd74SJohn Marino fp->pub._flags |= __SERR;
113*0d5acd74SJohn Marino errno = EILSEQ;
114*0d5acd74SJohn Marino return (WEOF);
115*0d5acd74SJohn Marino }
116