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 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 } 624297a3b0SGarrett D'Amore if ((statep = _getmbstate(fp)) == NULL) { 634297a3b0SGarrett D'Amore fp->_flag = _IOERR; 644297a3b0SGarrett D'Amore errno = EBADF; 654297a3b0SGarrett D'Amore return (WEOF); 664297a3b0SGarrett D'Amore } 674297a3b0SGarrett D'Amore do { 684297a3b0SGarrett D'Amore char x = (char)c; 69*d8e0a9a1SRobert Mustacchi nconv = lct->lc_mbrtowc(&wc, &x, 1, statep, B_FALSE); 704297a3b0SGarrett D'Amore if (nconv == (size_t)-1) { 714297a3b0SGarrett D'Amore break; 724297a3b0SGarrett D'Amore } else if (nconv == (size_t)-2) { 734297a3b0SGarrett D'Amore /* Incompletely decoded, consume another char */ 744297a3b0SGarrett D'Amore continue; 754297a3b0SGarrett D'Amore } else if (nconv == 0) { 764297a3b0SGarrett D'Amore /* 774297a3b0SGarrett D'Amore * Assume that the only valid representation of 784297a3b0SGarrett D'Amore * the null wide character is a single null byte. 794297a3b0SGarrett D'Amore */ 804297a3b0SGarrett D'Amore return (L'\0'); 814297a3b0SGarrett D'Amore } else { 824297a3b0SGarrett D'Amore return (wc); 834297a3b0SGarrett D'Amore } 844297a3b0SGarrett D'Amore } while ((c = GETC(fp)) != EOF); 854297a3b0SGarrett D'Amore 864297a3b0SGarrett D'Amore /* 874297a3b0SGarrett D'Amore * If we got here it means we got truncated in a character, or 884297a3b0SGarrett D'Amore * the character did not decode properly. Note that in the case 894297a3b0SGarrett D'Amore * of a botched decoding, we don't UNGETC the bad bytes. Should 904297a3b0SGarrett D'Amore * we? 914297a3b0SGarrett D'Amore */ 924297a3b0SGarrett D'Amore fp->_flag |= _IOERR; 934297a3b0SGarrett D'Amore errno = EILSEQ; 944297a3b0SGarrett D'Amore return (WEOF); 954297a3b0SGarrett D'Amore } 964297a3b0SGarrett D'Amore 972d08521bSGarrett D'Amore wint_t 982d08521bSGarrett D'Amore _fgetwc_unlocked(FILE *fp) 992d08521bSGarrett D'Amore { 1002d08521bSGarrett D'Amore return (_fgetwc_unlocked_l(fp, uselocale(NULL))); 1012d08521bSGarrett D'Amore } 1022d08521bSGarrett D'Amore 1034297a3b0SGarrett D'Amore 1044297a3b0SGarrett D'Amore /* 1054297a3b0SGarrett D'Amore * MT safe version 1064297a3b0SGarrett D'Amore */ 1072d08521bSGarrett D'Amore #undef getwc 1082d08521bSGarrett D'Amore #pragma weak getwc = fgetwc 1094297a3b0SGarrett D'Amore wint_t 1104297a3b0SGarrett D'Amore fgetwc(FILE *fp) 1114297a3b0SGarrett D'Amore { 1124297a3b0SGarrett D'Amore wint_t r; 1134297a3b0SGarrett D'Amore rmutex_t *l; 1142d08521bSGarrett D'Amore locale_t loc = uselocale(NULL); 1154297a3b0SGarrett D'Amore 1164297a3b0SGarrett D'Amore FLOCKFILE(l, fp); 1172d08521bSGarrett D'Amore r = _fgetwc_unlocked_l(fp, loc); 1184297a3b0SGarrett D'Amore FUNLOCKFILE(l); 1194297a3b0SGarrett D'Amore 1204297a3b0SGarrett D'Amore return (r); 1214297a3b0SGarrett D'Amore } 1224297a3b0SGarrett D'Amore 1234297a3b0SGarrett D'Amore /* 1244297a3b0SGarrett D'Amore * XPG5 version. 1254297a3b0SGarrett D'Amore */ 1262d08521bSGarrett D'Amore #undef __getwc_xpg5 1272d08521bSGarrett D'Amore #pragma weak __getwc_xpg5 = __fgetwc_xpg5 1284297a3b0SGarrett D'Amore wint_t 1294297a3b0SGarrett D'Amore __fgetwc_xpg5(FILE *fp) 1304297a3b0SGarrett D'Amore { 1314297a3b0SGarrett D'Amore wint_t r; 1324297a3b0SGarrett D'Amore rmutex_t *l; 1332d08521bSGarrett D'Amore locale_t loc = uselocale(NULL); 1344297a3b0SGarrett D'Amore 1354297a3b0SGarrett D'Amore FLOCKFILE(l, fp); 1364297a3b0SGarrett D'Amore if (GET_NO_MODE(fp)) 1374297a3b0SGarrett D'Amore _setorientation(fp, _WC_MODE); 1382d08521bSGarrett D'Amore r = _fgetwc_unlocked_l(fp, loc); 1394297a3b0SGarrett D'Amore FUNLOCKFILE(l); 1404297a3b0SGarrett D'Amore 1414297a3b0SGarrett D'Amore return (r); 1424297a3b0SGarrett D'Amore } 1434297a3b0SGarrett D'Amore 1442d08521bSGarrett D'Amore #pragma weak getwc_l = fgetwc_l 1454297a3b0SGarrett D'Amore wint_t 1462d08521bSGarrett D'Amore fgetwc_l(FILE *fp, locale_t loc) 1474297a3b0SGarrett D'Amore { 1482d08521bSGarrett D'Amore wint_t r; 1492d08521bSGarrett D'Amore rmutex_t *l; 1502d08521bSGarrett D'Amore FLOCKFILE(l, fp); 1512d08521bSGarrett D'Amore if (GET_NO_MODE(fp)) 1522d08521bSGarrett D'Amore _setorientation(fp, _WC_MODE); 1532d08521bSGarrett D'Amore r = _fgetwc_unlocked_l(fp, loc); 1542d08521bSGarrett D'Amore FUNLOCKFILE(l); 1552d08521bSGarrett D'Amore 1562d08521bSGarrett D'Amore return (r); 1574297a3b0SGarrett D'Amore } 158