xref: /illumos-gate/usr/src/lib/libc/port/stdio/vwscanf.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
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 #include "lint.h"
28 #include "file64.h"
29 #include <mtlib.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <thread.h>
34 #include <synch.h>
35 #include <wchar.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <alloca.h>
39 #include "mse.h"
40 #include "stdiom.h"
41 #include "libc.h"
42 
43 int
44 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
45 _vwscanf_c89(const wchar_t *fmt, va_list ap)
46 #else
47 vwscanf(const wchar_t *fmt, va_list ap)
48 #endif
49 {
50 	rmutex_t	*lk;
51 	int	ret;
52 
53 	FLOCKFILE(lk, stdin);
54 
55 	if (GET_NO_MODE(stdin))
56 		_setorientation(stdin, _WC_MODE);
57 
58 #ifdef _C89_INTMAX32
59 	ret = __wdoscan_u(stdin, fmt, ap, _F_INTMAX32);
60 #else
61 	ret = __wdoscan_u(stdin, fmt, ap, 0);
62 #endif
63 	FUNLOCKFILE(lk);
64 	return (ret);
65 }
66 
67 int
68 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
69 _vfwscanf_c89(FILE *iop, const wchar_t *fmt, va_list ap)
70 #else
71 vfwscanf(FILE *iop, const wchar_t *fmt, va_list ap)
72 #endif
73 {
74 	rmutex_t	*lk;
75 	int	ret;
76 
77 	FLOCKFILE(lk, iop);
78 
79 	if (GET_NO_MODE(iop))
80 		_setorientation(iop, _WC_MODE);
81 
82 
83 #ifdef _C89_INTMAX32
84 	ret = __wdoscan_u(iop, fmt, ap, _F_INTMAX32);
85 #else
86 	ret = __wdoscan_u(iop, fmt, ap, 0);
87 #endif
88 	FUNLOCKFILE(lk);
89 	return (ret);
90 }
91 
92 int
93 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
94 _vswscanf_c89(const wchar_t *wstr, const wchar_t *fmt, va_list ap)
95 #else
96 vswscanf(const wchar_t *wstr, const wchar_t *fmt, va_list ap)
97 #endif
98 {
99 	FILE	strbuf;
100 	size_t	wlen, clen;
101 	char	*tmp_buf;
102 	int	ret;
103 
104 	/*
105 	 * The dummy FILE * created for swscanf has the _IOWRT
106 	 * flag set to distinguish it from wscanf and fwscanf
107 	 * invocations.
108 	 */
109 
110 	clen = wcstombs(NULL, wstr, 0);
111 	if (clen == (size_t)-1) {
112 		errno = EILSEQ;
113 		return (EOF);
114 	}
115 	tmp_buf = alloca(sizeof (char) * (clen + 1));
116 	if (tmp_buf == NULL)
117 		return (EOF);
118 	wlen = wcstombs(tmp_buf, wstr, clen + 1);
119 	if (wlen == (size_t)-1) {
120 		errno = EILSEQ;
121 		return (EOF);
122 	}
123 
124 	strbuf._flag = _IOREAD | _IOWRT;
125 	strbuf._ptr = strbuf._base = (unsigned char *)tmp_buf;
126 	strbuf._cnt = strlen(tmp_buf);
127 	SET_FILE(&strbuf, _NFILE);
128 
129 	/* Probably the following is not required. */
130 	/* _setorientation(&strbuf, _WC_MODE); */
131 
132 #ifdef _C89_INTMAX32
133 	ret = __wdoscan_u(&strbuf, fmt, ap, _F_INTMAX32);
134 #else
135 	ret = __wdoscan_u(&strbuf, fmt, ap, 0);
136 #endif
137 	return (ret);
138 }
139