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