xref: /titanic_50/usr/src/lib/libc/port/i18n/wstok.c (revision 711890bc9379ceea66272dc8d4981812224ea86e)
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 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*	Copyright (c) 1986 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 
34 /*	This module is created for NLS on Sep.03.86		*/
35 
36 /*
37  * uses wcspbrk and wcsspn to break string into tokens on
38  * sequentially subsequent calls. returns WNULL when no
39  * non-separator characters remain.
40  * 'subsequent' calls are calls with first argument WNULL.
41  */
42 
43 #pragma weak wstok = _wstok
44 
45 #include "synonyms.h"
46 #include "mtlib.h"
47 #include "mse_int.h"
48 #include <stdlib.h>
49 #include <wchar.h>
50 #include <thread.h>
51 #include "libc.h"
52 #include "tsd.h"
53 
54 #ifndef WNULL
55 #define	WNULL	(wchar_t *)0
56 #endif
57 
58 wchar_t *
59 __wcstok_xpg5(wchar_t *string, const wchar_t *sepset, wchar_t **ptr)
60 {
61 	wchar_t *q, *r;
62 
63 	/* first or subsequent call */
64 	if ((string == WNULL && (string = *ptr) == 0) ||
65 	    (((q = string + wcsspn(string, sepset)) != WNULL) && *q == L'\0'))
66 		return (WNULL);
67 
68 	/* sepset becomes next string after separator */
69 	if ((r = wcspbrk(q, sepset)) == WNULL)	/* move past token */
70 		*ptr = 0;	/* indicate this is last token */
71 	else {
72 		*r = L'\0';
73 		*ptr = r + 1;
74 	}
75 	return (q);
76 }
77 
78 
79 wchar_t *
80 wcstok(wchar_t *string, const wchar_t *sepset)
81 {
82 	wchar_t **lasts = tsdalloc(_T_WCSTOK, sizeof (wchar_t *), NULL);
83 
84 	if (lasts == NULL)
85 		return (NULL);
86 	return (__wcstok_xpg5(string, sepset, lasts));
87 }
88 
89 wchar_t *
90 _wstok(wchar_t *string, const wchar_t *sepset)
91 {
92 	return (wcstok(string, sepset));
93 }
94