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