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