1*9e66e6d7Sabs /* $NetBSD: wcstok.c,v 1.4 2012/06/25 22:32:46 abs Exp $ */
2326ed368Stshiozak
3326ed368Stshiozak /*-
4326ed368Stshiozak * Copyright (c) 1998 Softweyr LLC. All rights reserved.
5326ed368Stshiozak *
6326ed368Stshiozak * strtok_r, from Berkeley strtok
7326ed368Stshiozak * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
8326ed368Stshiozak *
9326ed368Stshiozak * Copyright (c) 1988, 1993
10326ed368Stshiozak * The Regents of the University of California. All rights reserved.
11326ed368Stshiozak *
12326ed368Stshiozak * Redistribution and use in source and binary forms, with or without
13326ed368Stshiozak * modification, are permitted provided that the following conditions
14326ed368Stshiozak * are met:
15326ed368Stshiozak * 1. Redistributions of source code must retain the above copyright
16326ed368Stshiozak * notices, this list of conditions and the following disclaimer.
17326ed368Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
18326ed368Stshiozak * notices, this list of conditions and the following disclaimer in the
19326ed368Stshiozak * documentation and/or other materials provided with the distribution.
20326ed368Stshiozak * 3. All advertising materials mentioning features or use of this software
21326ed368Stshiozak * must display the following acknowledgement:
22326ed368Stshiozak * This product includes software developed by Softweyr LLC, the
23326ed368Stshiozak * University of California, Berkeley, and its contributors.
24326ed368Stshiozak * 4. Neither the name of the University nor the names of its contributors
25326ed368Stshiozak * may be used to endorse or promote products derived from this software
26326ed368Stshiozak * without specific prior written permission.
27326ed368Stshiozak *
28326ed368Stshiozak * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
29326ed368Stshiozak * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30326ed368Stshiozak * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31326ed368Stshiozak * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
32326ed368Stshiozak * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33326ed368Stshiozak * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
34326ed368Stshiozak * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35326ed368Stshiozak * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36326ed368Stshiozak * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37326ed368Stshiozak * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38326ed368Stshiozak * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39e16e5eebStshiozak *
40e16e5eebStshiozak * Original version ID:
41e16e5eebStshiozak * FreeBSD: src/lib/libc/string/wcstok.c,v 1.1 2002/09/07 08:16:57 tjr Exp
42326ed368Stshiozak */
43326ed368Stshiozak
44326ed368Stshiozak #include <sys/cdefs.h>
45326ed368Stshiozak #if defined(LIBC_SCCS) && !defined(lint)
46*9e66e6d7Sabs __RCSID("$NetBSD: wcstok.c,v 1.4 2012/06/25 22:32:46 abs Exp $");
47326ed368Stshiozak #endif
48326ed368Stshiozak
49326ed368Stshiozak #include <assert.h>
50326ed368Stshiozak #include <wchar.h>
51326ed368Stshiozak
52326ed368Stshiozak wchar_t *
wcstok(wchar_t * __restrict s,const wchar_t * __restrict delim,wchar_t ** __restrict last)53*9e66e6d7Sabs wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
54*9e66e6d7Sabs wchar_t ** __restrict last)
55326ed368Stshiozak {
56326ed368Stshiozak const wchar_t *spanp;
57326ed368Stshiozak wchar_t c, sc;
58326ed368Stshiozak wchar_t *tok;
59326ed368Stshiozak
60326ed368Stshiozak /* s may be NULL */
618adfd9b7Stshiozak _DIAGASSERT(delim != NULL);
62326ed368Stshiozak _DIAGASSERT(last != NULL);
63326ed368Stshiozak
64326ed368Stshiozak if (s == NULL && (s = *last) == NULL)
65326ed368Stshiozak return (NULL);
66326ed368Stshiozak
67326ed368Stshiozak /*
68326ed368Stshiozak * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
69326ed368Stshiozak */
70326ed368Stshiozak cont:
71326ed368Stshiozak c = *s++;
72326ed368Stshiozak for (spanp = delim; (sc = *spanp++) != L'\0';) {
73326ed368Stshiozak if (c == sc)
74326ed368Stshiozak goto cont;
75326ed368Stshiozak }
76326ed368Stshiozak
77326ed368Stshiozak if (c == L'\0') { /* no non-delimiter characters */
78326ed368Stshiozak *last = NULL;
79326ed368Stshiozak return (NULL);
80326ed368Stshiozak }
81326ed368Stshiozak tok = s - 1;
82326ed368Stshiozak
83326ed368Stshiozak /*
84326ed368Stshiozak * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
85326ed368Stshiozak * Note that delim must have one NUL; we stop if we see that, too.
86326ed368Stshiozak */
87326ed368Stshiozak for (;;) {
88326ed368Stshiozak c = *s++;
89326ed368Stshiozak spanp = delim;
90326ed368Stshiozak do {
91326ed368Stshiozak if ((sc = *spanp++) == c) {
92326ed368Stshiozak if (c == L'\0')
93326ed368Stshiozak s = NULL;
94326ed368Stshiozak else
95326ed368Stshiozak s[-1] = L'\0';
96326ed368Stshiozak *last = s;
97326ed368Stshiozak return (tok);
98326ed368Stshiozak }
99326ed368Stshiozak } while (sc != L'\0');
100326ed368Stshiozak }
101326ed368Stshiozak /* NOTREACHED */
102326ed368Stshiozak }
103