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