xref: /titanic_41/usr/src/lib/libc/port/stdio/vwscanf.c (revision f5488aa822e08905cde61d596e965030a1dfffcd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "file64.h"
31 #include <mtlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <thread.h>
36 #include <synch.h>
37 #include <wchar.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <alloca.h>
41 #include "mse.h"
42 #include "stdiom.h"
43 #include "libc.h"
44 
45 int
46 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
47 _vwscanf_c89(const wchar_t *fmt, va_list ap)
48 #else
49 vwscanf(const wchar_t *fmt, va_list ap)
50 #endif
51 {
52 	rmutex_t	*lk;
53 	int	ret;
54 
55 	FLOCKFILE(lk, stdin);
56 
57 	if (_set_orientation_wide(stdin, NULL, NULL, 0) == -1) {
58 		errno = EBADF;
59 		FUNLOCKFILE(lk);
60 		return (EOF);
61 	}
62 
63 #ifdef _C89_INTMAX32
64 	ret = __wdoscan_u(stdin, fmt, ap, _F_INTMAX32);
65 #else
66 	ret = __wdoscan_u(stdin, fmt, ap, 0);
67 #endif
68 	FUNLOCKFILE(lk);
69 	return (ret);
70 }
71 
72 int
73 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
74 _vfwscanf_c89(FILE *iop, const wchar_t *fmt, va_list ap)
75 #else
76 vfwscanf(FILE *iop, const wchar_t *fmt, va_list ap)
77 #endif
78 {
79 	rmutex_t	*lk;
80 	int	ret;
81 
82 	FLOCKFILE(lk, iop);
83 
84 	if (_set_orientation_wide(iop, NULL, NULL, 0) == -1) {
85 		errno = EBADF;
86 		FUNLOCKFILE(lk);
87 		return (EOF);
88 	}
89 
90 
91 #ifdef _C89_INTMAX32
92 	ret = __wdoscan_u(iop, fmt, ap, _F_INTMAX32);
93 #else
94 	ret = __wdoscan_u(iop, fmt, ap, 0);
95 #endif
96 	FUNLOCKFILE(lk);
97 	return (ret);
98 }
99 
100 int
101 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
102 _vswscanf_c89(const wchar_t *wstr, const wchar_t *fmt, va_list ap)
103 #else
104 vswscanf(const wchar_t *wstr, const wchar_t *fmt, va_list ap)
105 #endif
106 {
107 	FILE	strbuf;
108 	size_t	wlen, clen;
109 	char	*tmp_buf;
110 	int	ret;
111 
112 	/*
113 	 * The dummy FILE * created for swscanf has the _IOWRT
114 	 * flag set to distinguish it from wscanf and fwscanf
115 	 * invocations.
116 	 */
117 
118 	clen = wcstombs(NULL, wstr, 0);
119 	if (clen == (size_t)-1) {
120 		errno = EILSEQ;
121 		return (EOF);
122 	}
123 	tmp_buf = alloca(sizeof (char) * (clen + 1));
124 	if (tmp_buf == NULL)
125 		return (EOF);
126 	wlen = wcstombs(tmp_buf, wstr, clen + 1);
127 	if (wlen == (size_t)-1) {
128 		errno = EILSEQ;
129 		return (EOF);
130 	}
131 
132 	strbuf._flag = _IOREAD | _IOWRT;
133 	strbuf._ptr = strbuf._base = (unsigned char *)tmp_buf;
134 	strbuf._cnt = strlen(tmp_buf);
135 	SET_FILE(&strbuf, _NFILE);
136 
137 	/* Probably the following is not required. */
138 	/* _setorientation(&strbuf, _WC_MODE); */
139 
140 #ifdef _C89_INTMAX32
141 	ret = __wdoscan_u(&strbuf, fmt, ap, _F_INTMAX32);
142 #else
143 	ret = __wdoscan_u(&strbuf, fmt, ap, 0);
144 #endif
145 	return (ret);
146 }
147