xref: /illumos-gate/usr/src/lib/libc/port/locale/fgetwc.c (revision 89abd63c)
14297a3b0SGarrett D'Amore /*
22d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 2002-2004 Tim J. Robbins.
54297a3b0SGarrett D'Amore  * All rights reserved.
64297a3b0SGarrett D'Amore  *
74297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
84297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
94297a3b0SGarrett D'Amore  * are met:
104297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
114297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
124297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
134297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
144297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
154297a3b0SGarrett D'Amore  *
164297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264297a3b0SGarrett D'Amore  * SUCH DAMAGE.
274297a3b0SGarrett D'Amore  */
284297a3b0SGarrett D'Amore 
294297a3b0SGarrett D'Amore #include "lint.h"
304297a3b0SGarrett D'Amore #include "mse_int.h"
314297a3b0SGarrett D'Amore #include "file64.h"
324297a3b0SGarrett D'Amore #include "mtlib.h"
334297a3b0SGarrett D'Amore #include <errno.h>
344297a3b0SGarrett D'Amore #include <stdio.h>
354297a3b0SGarrett D'Amore #include <stdlib.h>
364297a3b0SGarrett D'Amore #include <wchar.h>
374297a3b0SGarrett D'Amore #include "mblocal.h"
384297a3b0SGarrett D'Amore #include "stdiom.h"
392d08521bSGarrett D'Amore #include "localeimpl.h"
402d08521bSGarrett D'Amore #include "lctype.h"
414297a3b0SGarrett D'Amore 
424297a3b0SGarrett D'Amore /*
434297a3b0SGarrett D'Amore  * Non-MT-safe version.
444297a3b0SGarrett D'Amore  */
454297a3b0SGarrett D'Amore wint_t
_fgetwc_unlocked_l(FILE * fp,locale_t loc)462d08521bSGarrett D'Amore _fgetwc_unlocked_l(FILE *fp, locale_t loc)
474297a3b0SGarrett D'Amore {
484297a3b0SGarrett D'Amore 	wchar_t wc;
494297a3b0SGarrett D'Amore 	size_t nconv;
504297a3b0SGarrett D'Amore 	int	c;
514297a3b0SGarrett D'Amore 	mbstate_t	*statep;
522d08521bSGarrett D'Amore 	const struct lc_ctype *lct;
534297a3b0SGarrett D'Amore 
544297a3b0SGarrett D'Amore 	if ((c = GETC(fp)) == EOF)
554297a3b0SGarrett D'Amore 		return (WEOF);
564297a3b0SGarrett D'Amore 
572d08521bSGarrett D'Amore 	lct = loc->ctype;
582d08521bSGarrett D'Amore 	if (lct->lc_max_mblen == 1) {
594297a3b0SGarrett D'Amore 		/* Fast path for single-byte encodings. */
604297a3b0SGarrett D'Amore 		return ((wint_t)c);
614297a3b0SGarrett D'Amore 	}
62*89abd63cSToomas Soome 	statep = _getmbstate(fp);
63*89abd63cSToomas Soome 	if (statep == NULL) {
644297a3b0SGarrett D'Amore 		fp->_flag = _IOERR;
654297a3b0SGarrett D'Amore 		errno = EBADF;
664297a3b0SGarrett D'Amore 		return (WEOF);
674297a3b0SGarrett D'Amore 	}
684297a3b0SGarrett D'Amore 	do {
694297a3b0SGarrett D'Amore 		char	x = (char)c;
700ac311baSRobert Mustacchi 		nconv = lct->lc_mbrtowc(&wc, &x, 1, statep, B_FALSE);
714297a3b0SGarrett D'Amore 		if (nconv == (size_t)-1) {
724297a3b0SGarrett D'Amore 			break;
734297a3b0SGarrett D'Amore 		} else if (nconv == (size_t)-2) {
744297a3b0SGarrett D'Amore 			/* Incompletely decoded, consume another char */
754297a3b0SGarrett D'Amore 			continue;
764297a3b0SGarrett D'Amore 		} else if (nconv == 0) {
774297a3b0SGarrett D'Amore 			/*
784297a3b0SGarrett D'Amore 			 * Assume that the only valid representation of
794297a3b0SGarrett D'Amore 			 * the null wide character is a single null byte.
804297a3b0SGarrett D'Amore 			 */
814297a3b0SGarrett D'Amore 			return (L'\0');
824297a3b0SGarrett D'Amore 		} else {
834297a3b0SGarrett D'Amore 			return (wc);
844297a3b0SGarrett D'Amore 		}
854297a3b0SGarrett D'Amore 	} while ((c = GETC(fp)) != EOF);
864297a3b0SGarrett D'Amore 
874297a3b0SGarrett D'Amore 	/*
884297a3b0SGarrett D'Amore 	 * If we got here it means we got truncated in a character, or
894297a3b0SGarrett D'Amore 	 * the character did not decode properly.  Note that in the case
904297a3b0SGarrett D'Amore 	 * of a botched decoding, we don't UNGETC the bad bytes.  Should
914297a3b0SGarrett D'Amore 	 * we?
924297a3b0SGarrett D'Amore 	 */
934297a3b0SGarrett D'Amore 	fp->_flag |= _IOERR;
944297a3b0SGarrett D'Amore 	errno = EILSEQ;
954297a3b0SGarrett D'Amore 	return (WEOF);
964297a3b0SGarrett D'Amore }
974297a3b0SGarrett D'Amore 
982d08521bSGarrett D'Amore wint_t
_fgetwc_unlocked(FILE * fp)992d08521bSGarrett D'Amore _fgetwc_unlocked(FILE *fp)
1002d08521bSGarrett D'Amore {
1012d08521bSGarrett D'Amore 	return (_fgetwc_unlocked_l(fp, uselocale(NULL)));
1022d08521bSGarrett D'Amore }
1032d08521bSGarrett D'Amore 
1044297a3b0SGarrett D'Amore 
1054297a3b0SGarrett D'Amore /*
1064297a3b0SGarrett D'Amore  * MT safe version
1074297a3b0SGarrett D'Amore  */
1082d08521bSGarrett D'Amore #undef getwc
1092d08521bSGarrett D'Amore #pragma weak getwc = fgetwc
1104297a3b0SGarrett D'Amore wint_t
fgetwc(FILE * fp)1114297a3b0SGarrett D'Amore fgetwc(FILE *fp)
1124297a3b0SGarrett D'Amore {
1134297a3b0SGarrett D'Amore 	wint_t		r;
1144297a3b0SGarrett D'Amore 	rmutex_t	*l;
1152d08521bSGarrett D'Amore 	locale_t	loc = uselocale(NULL);
1164297a3b0SGarrett D'Amore 
1174297a3b0SGarrett D'Amore 	FLOCKFILE(l, fp);
1182d08521bSGarrett D'Amore 	r = _fgetwc_unlocked_l(fp, loc);
1194297a3b0SGarrett D'Amore 	FUNLOCKFILE(l);
1204297a3b0SGarrett D'Amore 
1214297a3b0SGarrett D'Amore 	return (r);
1224297a3b0SGarrett D'Amore }
1234297a3b0SGarrett D'Amore 
1244297a3b0SGarrett D'Amore /*
1254297a3b0SGarrett D'Amore  * XPG5 version.
1264297a3b0SGarrett D'Amore  */
1272d08521bSGarrett D'Amore #undef	__getwc_xpg5
1282d08521bSGarrett D'Amore #pragma weak __getwc_xpg5 = __fgetwc_xpg5
1294297a3b0SGarrett D'Amore wint_t
__fgetwc_xpg5(FILE * fp)1304297a3b0SGarrett D'Amore __fgetwc_xpg5(FILE *fp)
1314297a3b0SGarrett D'Amore {
1324297a3b0SGarrett D'Amore 	wint_t		r;
1334297a3b0SGarrett D'Amore 	rmutex_t	*l;
1342d08521bSGarrett D'Amore 	locale_t	loc = uselocale(NULL);
1354297a3b0SGarrett D'Amore 
1364297a3b0SGarrett D'Amore 	FLOCKFILE(l, fp);
1374297a3b0SGarrett D'Amore 	if (GET_NO_MODE(fp))
1384297a3b0SGarrett D'Amore 		_setorientation(fp, _WC_MODE);
1392d08521bSGarrett D'Amore 	r = _fgetwc_unlocked_l(fp, loc);
1404297a3b0SGarrett D'Amore 	FUNLOCKFILE(l);
1414297a3b0SGarrett D'Amore 
1424297a3b0SGarrett D'Amore 	return (r);
1434297a3b0SGarrett D'Amore }
1444297a3b0SGarrett D'Amore 
1452d08521bSGarrett D'Amore #pragma weak getwc_l = fgetwc_l
1464297a3b0SGarrett D'Amore wint_t
fgetwc_l(FILE * fp,locale_t loc)1472d08521bSGarrett D'Amore fgetwc_l(FILE *fp, locale_t loc)
1484297a3b0SGarrett D'Amore {
1492d08521bSGarrett D'Amore 	wint_t		r;
1502d08521bSGarrett D'Amore 	rmutex_t	*l;
1512d08521bSGarrett D'Amore 	FLOCKFILE(l, fp);
1522d08521bSGarrett D'Amore 	if (GET_NO_MODE(fp))
1532d08521bSGarrett D'Amore 		_setorientation(fp, _WC_MODE);
1542d08521bSGarrett D'Amore 	r = _fgetwc_unlocked_l(fp, loc);
1552d08521bSGarrett D'Amore 	FUNLOCKFILE(l);
1562d08521bSGarrett D'Amore 
1572d08521bSGarrett D'Amore 	return (r);
1584297a3b0SGarrett D'Amore }
159