1*c2c925deSespie /* $OpenBSD: wcstok.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */
2b689ee27Sespie /* $NetBSD: wcstok.c,v 1.3 2003/07/10 08:50:48 tshiozak Exp $ */
3b689ee27Sespie
4b689ee27Sespie /*-
5b689ee27Sespie * Copyright (c) 1998 Softweyr LLC. All rights reserved.
6b689ee27Sespie *
7b689ee27Sespie * strtok_r, from Berkeley strtok
8b689ee27Sespie * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
9b689ee27Sespie *
10b689ee27Sespie * Copyright (c) 1988, 1993
11b689ee27Sespie * The Regents of the University of California. All rights reserved.
12b689ee27Sespie *
13b689ee27Sespie * Redistribution and use in source and binary forms, with or without
14b689ee27Sespie * modification, are permitted provided that the following conditions
15b689ee27Sespie * are met:
16b689ee27Sespie * 1. Redistributions of source code must retain the above copyright
17b689ee27Sespie * notices, this list of conditions and the following disclaimer.
18b689ee27Sespie * 2. Redistributions in binary form must reproduce the above copyright
19b689ee27Sespie * notices, this list of conditions and the following disclaimer in the
20b689ee27Sespie * documentation and/or other materials provided with the distribution.
21b689ee27Sespie * 3. All advertising materials mentioning features or use of this software
22b689ee27Sespie * must display the following acknowledgement:
23b689ee27Sespie * This product includes software developed by Softweyr LLC, the
24b689ee27Sespie * University of California, Berkeley, and its contributors.
25b689ee27Sespie * 4. Neither the name of the University nor the names of its contributors
26b689ee27Sespie * may be used to endorse or promote products derived from this software
27b689ee27Sespie * without specific prior written permission.
28b689ee27Sespie *
29b689ee27Sespie * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
30b689ee27Sespie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31b689ee27Sespie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32b689ee27Sespie * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
33b689ee27Sespie * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34b689ee27Sespie * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
35b689ee27Sespie * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36b689ee27Sespie * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37b689ee27Sespie * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38b689ee27Sespie * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39b689ee27Sespie * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40b689ee27Sespie *
41b689ee27Sespie * Original version ID:
42b689ee27Sespie * FreeBSD: src/lib/libc/string/wcstok.c,v 1.1 2002/09/07 08:16:57 tjr Exp
43b689ee27Sespie */
44b689ee27Sespie
45b689ee27Sespie #include <wchar.h>
46b689ee27Sespie
47b689ee27Sespie wchar_t *
wcstok(wchar_t * __restrict s,const wchar_t * __restrict delim,wchar_t ** __restrict last)481c031cf6Sespie wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
491c031cf6Sespie wchar_t ** __restrict last)
50b689ee27Sespie {
51b689ee27Sespie const wchar_t *spanp;
52b689ee27Sespie wchar_t c, sc;
53b689ee27Sespie wchar_t *tok;
54b689ee27Sespie
55b689ee27Sespie if (s == NULL && (s = *last) == NULL)
56b689ee27Sespie return (NULL);
57b689ee27Sespie
58b689ee27Sespie /*
59b689ee27Sespie * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
60b689ee27Sespie */
61b689ee27Sespie cont:
62b689ee27Sespie c = *s++;
63b689ee27Sespie for (spanp = delim; (sc = *spanp++) != L'\0';) {
64b689ee27Sespie if (c == sc)
65b689ee27Sespie goto cont;
66b689ee27Sespie }
67b689ee27Sespie
68b689ee27Sespie if (c == L'\0') { /* no non-delimiter characters */
69b689ee27Sespie *last = NULL;
70b689ee27Sespie return (NULL);
71b689ee27Sespie }
72b689ee27Sespie tok = s - 1;
73b689ee27Sespie
74b689ee27Sespie /*
75b689ee27Sespie * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
76b689ee27Sespie * Note that delim must have one NUL; we stop if we see that, too.
77b689ee27Sespie */
78b689ee27Sespie for (;;) {
79b689ee27Sespie c = *s++;
80b689ee27Sespie spanp = delim;
81b689ee27Sespie do {
82b689ee27Sespie if ((sc = *spanp++) == c) {
83b689ee27Sespie if (c == L'\0')
84b689ee27Sespie s = NULL;
85b689ee27Sespie else
86b689ee27Sespie s[-1] = L'\0';
87b689ee27Sespie *last = s;
88b689ee27Sespie return (tok);
89b689ee27Sespie }
90b689ee27Sespie } while (sc != L'\0');
91b689ee27Sespie }
92b689ee27Sespie /* NOTREACHED */
93b689ee27Sespie }
94