1103067Stjr/*-
2103067Stjr * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
3103067Stjr *
4103067Stjr * strtok_r, from Berkeley strtok
5103067Stjr * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
6103067Stjr *
7103067Stjr * Copyright (c) 1988, 1993
8103067Stjr *	The Regents of the University of California.  All rights reserved.
9103067Stjr *
10103067Stjr * Redistribution and use in source and binary forms, with or without
11103067Stjr * modification, are permitted provided that the following conditions
12103067Stjr * are met:
13103067Stjr * 1. Redistributions of source code must retain the above copyright
14103067Stjr *    notices, this list of conditions and the following disclaimer.
15103067Stjr * 2. Redistributions in binary form must reproduce the above copyright
16103067Stjr *    notices, this list of conditions and the following disclaimer in the
17103067Stjr *    documentation and/or other materials provided with the distribution.
18251069Semaste * 3. Neither the name of the University nor the names of its contributors
19103067Stjr *    may be used to endorse or promote products derived from this software
20103067Stjr *    without specific prior written permission.
21103067Stjr *
22103067Stjr * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23103067Stjr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24103067Stjr * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25103067Stjr * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
26103067Stjr * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27103067Stjr * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28103067Stjr * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29103067Stjr * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30103067Stjr * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31103067Stjr * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32103067Stjr * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33103067Stjr */
34103067Stjr
35103067Stjr#include <sys/cdefs.h>
36103067Stjr__FBSDID("$FreeBSD$");
37103067Stjr
38103067Stjr#include <wchar.h>
39103067Stjr
40103067Stjrwchar_t *
41103067Stjrwcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
42103067Stjr    wchar_t ** __restrict last)
43103067Stjr{
44112131Stjr	const wchar_t *spanp;
45112131Stjr	wchar_t *tok;
46103067Stjr	wchar_t c, sc;
47103067Stjr
48103067Stjr	if (s == NULL && (s = *last) == NULL)
49103067Stjr		return (NULL);
50103067Stjr
51103067Stjr	/*
52103067Stjr	 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
53103067Stjr	 */
54103067Stjrcont:
55103067Stjr	c = *s++;
56112131Stjr	for (spanp = delim; (sc = *spanp++) != L'\0';) {
57103067Stjr		if (c == sc)
58103067Stjr			goto cont;
59103067Stjr	}
60103067Stjr
61103067Stjr	if (c == L'\0') {	/* no non-delimiter characters */
62103067Stjr		*last = NULL;
63103067Stjr		return (NULL);
64103067Stjr	}
65103067Stjr	tok = s - 1;
66103067Stjr
67103067Stjr	/*
68103067Stjr	 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
69103067Stjr	 * Note that delim must have one NUL; we stop if we see that, too.
70103067Stjr	 */
71103067Stjr	for (;;) {
72103067Stjr		c = *s++;
73112131Stjr		spanp = delim;
74103067Stjr		do {
75103067Stjr			if ((sc = *spanp++) == c) {
76103067Stjr				if (c == L'\0')
77103067Stjr					s = NULL;
78103067Stjr				else
79103067Stjr					s[-1] = L'\0';
80103067Stjr				*last = s;
81103067Stjr				return (tok);
82103067Stjr			}
83103067Stjr		} while (sc != L'\0');
84103067Stjr	}
85103067Stjr	/* NOTREACHED */
86103067Stjr}
87