xref: /illumos-gate/usr/src/lib/libc/port/stdio/vwscanf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 "synonyms.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 	strbuf._file = _NFILE;
136 	/* Probably the following is not required. */
137 	/* _setorientation(&strbuf, _WC_MODE); */
138 
139 #ifdef _C89_INTMAX32
140 	ret = __wdoscan_u(&strbuf, fmt, ap, _F_INTMAX32);
141 #else
142 	ret = __wdoscan_u(&strbuf, fmt, ap, 0);
143 #endif
144 	return (ret);
145 }
146