xref: /illumos-gate/usr/src/lib/libc/port/locale/fgetwc.c (revision 6b5e5868)
14297a3b0SGarrett D'Amore /*
2*6b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
34297a3b0SGarrett D'Amore  * Copyright (c) 2002-2004 Tim J. Robbins.
44297a3b0SGarrett D'Amore  * All rights reserved.
54297a3b0SGarrett D'Amore  *
64297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
74297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
84297a3b0SGarrett D'Amore  * are met:
94297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
104297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
114297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
124297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
134297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
144297a3b0SGarrett D'Amore  *
154297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254297a3b0SGarrett D'Amore  * SUCH DAMAGE.
264297a3b0SGarrett D'Amore  */
274297a3b0SGarrett D'Amore 
284297a3b0SGarrett D'Amore #include "lint.h"
294297a3b0SGarrett D'Amore #include "mse_int.h"
304297a3b0SGarrett D'Amore #include "file64.h"
314297a3b0SGarrett D'Amore #include "mtlib.h"
324297a3b0SGarrett D'Amore #include <errno.h>
334297a3b0SGarrett D'Amore #include <stdio.h>
344297a3b0SGarrett D'Amore #include <stdlib.h>
354297a3b0SGarrett D'Amore #include <wchar.h>
364297a3b0SGarrett D'Amore #include "mblocal.h"
374297a3b0SGarrett D'Amore #include "stdiom.h"
384297a3b0SGarrett D'Amore 
394297a3b0SGarrett D'Amore /*
404297a3b0SGarrett D'Amore  * Non-MT-safe version.
414297a3b0SGarrett D'Amore  */
424297a3b0SGarrett D'Amore wint_t
434297a3b0SGarrett D'Amore _fgetwc_unlocked(FILE *fp)
444297a3b0SGarrett D'Amore {
454297a3b0SGarrett D'Amore 	wchar_t wc;
464297a3b0SGarrett D'Amore 	size_t nconv;
474297a3b0SGarrett D'Amore 	int	c;
484297a3b0SGarrett D'Amore 	mbstate_t	*statep;
494297a3b0SGarrett D'Amore 
504297a3b0SGarrett D'Amore 	if ((c = GETC(fp)) == EOF)
514297a3b0SGarrett D'Amore 		return (WEOF);
524297a3b0SGarrett D'Amore 
534297a3b0SGarrett D'Amore 	if (MB_CUR_MAX == 1) {
544297a3b0SGarrett D'Amore 		/* Fast path for single-byte encodings. */
554297a3b0SGarrett D'Amore 		return ((wint_t)c);
564297a3b0SGarrett D'Amore 	}
574297a3b0SGarrett D'Amore 	if ((statep = _getmbstate(fp)) == NULL) {
584297a3b0SGarrett D'Amore 		fp->_flag = _IOERR;
594297a3b0SGarrett D'Amore 		errno = EBADF;
604297a3b0SGarrett D'Amore 		return (WEOF);
614297a3b0SGarrett D'Amore 	}
624297a3b0SGarrett D'Amore 	do {
634297a3b0SGarrett D'Amore 		char	x = (char)c;
644297a3b0SGarrett D'Amore 		nconv = __mbrtowc(&wc, &x, 1, statep);
654297a3b0SGarrett D'Amore 		if (nconv == (size_t)-1) {
664297a3b0SGarrett D'Amore 			break;
674297a3b0SGarrett D'Amore 		} else if (nconv == (size_t)-2) {
684297a3b0SGarrett D'Amore 			/* Incompletely decoded, consume another char */
694297a3b0SGarrett D'Amore 			continue;
704297a3b0SGarrett D'Amore 		} else if (nconv == 0) {
714297a3b0SGarrett D'Amore 			/*
724297a3b0SGarrett D'Amore 			 * Assume that the only valid representation of
734297a3b0SGarrett D'Amore 			 * the null wide character is a single null byte.
744297a3b0SGarrett D'Amore 			 */
754297a3b0SGarrett D'Amore 			return (L'\0');
764297a3b0SGarrett D'Amore 		} else {
774297a3b0SGarrett D'Amore 			return (wc);
784297a3b0SGarrett D'Amore 		}
794297a3b0SGarrett D'Amore 	} while ((c = GETC(fp)) != EOF);
804297a3b0SGarrett D'Amore 
814297a3b0SGarrett D'Amore 	/*
824297a3b0SGarrett D'Amore 	 * If we got here it means we got truncated in a character, or
834297a3b0SGarrett D'Amore 	 * the character did not decode properly.  Note that in the case
844297a3b0SGarrett D'Amore 	 * of a botched decoding, we don't UNGETC the bad bytes.  Should
854297a3b0SGarrett D'Amore 	 * we?
864297a3b0SGarrett D'Amore 	 */
874297a3b0SGarrett D'Amore 	fp->_flag |= _IOERR;
884297a3b0SGarrett D'Amore 	errno = EILSEQ;
894297a3b0SGarrett D'Amore 	return (WEOF);
904297a3b0SGarrett D'Amore }
914297a3b0SGarrett D'Amore 
924297a3b0SGarrett D'Amore 
934297a3b0SGarrett D'Amore /*
944297a3b0SGarrett D'Amore  * MT safe version
954297a3b0SGarrett D'Amore  */
964297a3b0SGarrett D'Amore wint_t
974297a3b0SGarrett D'Amore fgetwc(FILE *fp)
984297a3b0SGarrett D'Amore {
994297a3b0SGarrett D'Amore 	wint_t		r;
1004297a3b0SGarrett D'Amore 	rmutex_t	*l;
1014297a3b0SGarrett D'Amore 
1024297a3b0SGarrett D'Amore 	FLOCKFILE(l, fp);
1034297a3b0SGarrett D'Amore 	r = _fgetwc_unlocked(fp);
1044297a3b0SGarrett D'Amore 	FUNLOCKFILE(l);
1054297a3b0SGarrett D'Amore 
1064297a3b0SGarrett D'Amore 	return (r);
1074297a3b0SGarrett D'Amore }
1084297a3b0SGarrett D'Amore 
1094297a3b0SGarrett D'Amore #undef	getwc
1104297a3b0SGarrett D'Amore wint_t
1114297a3b0SGarrett D'Amore getwc(FILE *fp)
1124297a3b0SGarrett D'Amore {
1134297a3b0SGarrett D'Amore 	return (getwc(fp));
1144297a3b0SGarrett D'Amore }
1154297a3b0SGarrett D'Amore 
1164297a3b0SGarrett D'Amore /*
1174297a3b0SGarrett D'Amore  * XPG5 version.
1184297a3b0SGarrett D'Amore  */
1194297a3b0SGarrett D'Amore wint_t
1204297a3b0SGarrett D'Amore __fgetwc_xpg5(FILE *fp)
1214297a3b0SGarrett D'Amore {
1224297a3b0SGarrett D'Amore 	wint_t		r;
1234297a3b0SGarrett D'Amore 	rmutex_t	*l;
1244297a3b0SGarrett D'Amore 
1254297a3b0SGarrett D'Amore 	FLOCKFILE(l, fp);
1264297a3b0SGarrett D'Amore 	if (GET_NO_MODE(fp))
1274297a3b0SGarrett D'Amore 		_setorientation(fp, _WC_MODE);
1284297a3b0SGarrett D'Amore 	r = _fgetwc_unlocked(fp);
1294297a3b0SGarrett D'Amore 	FUNLOCKFILE(l);
1304297a3b0SGarrett D'Amore 
1314297a3b0SGarrett D'Amore 	return (r);
1324297a3b0SGarrett D'Amore }
1334297a3b0SGarrett D'Amore 
1344297a3b0SGarrett D'Amore #undef	__getwc_xpg5
1354297a3b0SGarrett D'Amore wint_t
1364297a3b0SGarrett D'Amore __getwc_xpg5(FILE *fp)
1374297a3b0SGarrett D'Amore {
1384297a3b0SGarrett D'Amore 	return (__fgetwc_xpg5(fp));
1394297a3b0SGarrett D'Amore }
140