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