xref: /illumos-gate/usr/src/lib/libc/port/locale/fgetwc.c (revision 8d0c3d29bb99f6521f2dc5058a7e4debebad7899)
1 /*
2  * Copyright (c) 2002-2004 Tim J. Robbins.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /*
27  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include "lint.h"
32 #include "mse_int.h"
33 #include "file64.h"
34 #include "mtlib.h"
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <wchar.h>
39 #include "mblocal.h"
40 #include "stdiom.h"
41 
42 /*
43  * Non-MT-safe version.
44  */
45 wint_t
46 _fgetwc_unlocked(FILE *fp)
47 {
48 	wchar_t wc;
49 	size_t nconv;
50 	int	c;
51 	mbstate_t	*statep;
52 
53 	if ((c = GETC(fp)) == EOF)
54 		return (WEOF);
55 
56 	if (MB_CUR_MAX == 1) {
57 		/* Fast path for single-byte encodings. */
58 		return ((wint_t)c);
59 	}
60 	if ((statep = _getmbstate(fp)) == NULL) {
61 		fp->_flag = _IOERR;
62 		errno = EBADF;
63 		return (WEOF);
64 	}
65 	do {
66 		char	x = (char)c;
67 		nconv = __mbrtowc(&wc, &x, 1, statep);
68 		if (nconv == (size_t)-1) {
69 			break;
70 		} else if (nconv == (size_t)-2) {
71 			/* Incompletely decoded, consume another char */
72 			continue;
73 		} else if (nconv == 0) {
74 			/*
75 			 * Assume that the only valid representation of
76 			 * the null wide character is a single null byte.
77 			 */
78 			return (L'\0');
79 		} else {
80 			return (wc);
81 		}
82 	} while ((c = GETC(fp)) != EOF);
83 
84 	/*
85 	 * If we got here it means we got truncated in a character, or
86 	 * the character did not decode properly.  Note that in the case
87 	 * of a botched decoding, we don't UNGETC the bad bytes.  Should
88 	 * we?
89 	 */
90 	fp->_flag |= _IOERR;
91 	errno = EILSEQ;
92 	return (WEOF);
93 }
94 
95 
96 /*
97  * MT safe version
98  */
99 wint_t
100 fgetwc(FILE *fp)
101 {
102 	wint_t		r;
103 	rmutex_t	*l;
104 
105 	FLOCKFILE(l, fp);
106 	r = _fgetwc_unlocked(fp);
107 	FUNLOCKFILE(l);
108 
109 	return (r);
110 }
111 
112 #undef	getwc
113 wint_t
114 getwc(FILE *fp)
115 {
116 	return (getwc(fp));
117 }
118 
119 /*
120  * XPG5 version.
121  */
122 wint_t
123 __fgetwc_xpg5(FILE *fp)
124 {
125 	wint_t		r;
126 	rmutex_t	*l;
127 
128 	FLOCKFILE(l, fp);
129 	if (GET_NO_MODE(fp))
130 		_setorientation(fp, _WC_MODE);
131 	r = _fgetwc_unlocked(fp);
132 	FUNLOCKFILE(l);
133 
134 	return (r);
135 }
136 
137 #undef	__getwc_xpg5
138 wint_t
139 __getwc_xpg5(FILE *fp)
140 {
141 	return (__fgetwc_xpg5(fp));
142 }
143