xref: /titanic_41/usr/src/lib/libc/port/i18n/wsscanf.c (revision 5e2c3ae0c70f6eb4a42ae670882aabac983cb5f1)
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 /*	Copyright (c) 1986 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include  "lint.h"
33 #include "file64.h"
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <widec.h>
38 #include <string.h>
39 #include "libc.h"
40 #include "stdiom.h"
41 
42 /*
43  * 	wsscanf -- this function will read wchar_t characters from
44  *		    wchar_t string according to the conversion format.
45  *		    Note that the performance degrades if the intermediate
46  *		    result of conversion exceeds 1024 bytes due to the
47  *		    use of malloc() on each call.
48  *		    We should implement wchar_t version of doscan()
49  *		    for better performance.
50  */
51 #define	MAXINSTR	1024
52 
53 int
54 wsscanf(wchar_t *string, const char *format, ...)
55 {
56 	va_list		ap;
57 	size_t		i;
58 	char		stackbuf[MAXINSTR];
59 	char		*tempstring = stackbuf;
60 	size_t		malloced = 0;
61 	int		j;
62 
63 	i = wcstombs(tempstring, string, MAXINSTR);
64 	if (i == (size_t)-1)
65 		return (-1);
66 
67 	if (i == MAXINSTR) { /* The buffer was too small.  Malloc it. */
68 		tempstring = malloc(malloced = MB_CUR_MAX*wcslen(string)+1);
69 		if (tempstring == 0)
70 			return (-1);
71 		i = wcstombs(tempstring, string, malloced); /* Try again. */
72 		if (i == (size_t)-1) {
73 			free(tempstring);
74 			return (-1);
75 		}
76 	}
77 
78 	va_start(ap, format);
79 	j = vsscanf(tempstring, format, ap);
80 	va_end(ap);
81 	if (malloced) free(tempstring);
82 	return (j);
83 }
84