wcstok.c revision 103067
164562Sgshapiro/*- 2112810Sgshapiro * Copyright (c) 1998 Softweyr LLC. All rights reserved. 364562Sgshapiro * 464562Sgshapiro * strtok_r, from Berkeley strtok 564562Sgshapiro * Oct 13, 1998 by Wes Peters <wes@softweyr.com> 664562Sgshapiro * 764562Sgshapiro * Copyright (c) 1988, 1993 864562Sgshapiro * The Regents of the University of California. All rights reserved. 964562Sgshapiro * 1064562Sgshapiro * Redistribution and use in source and binary forms, with or without 1190792Sgshapiro * modification, are permitted provided that the following conditions 1264562Sgshapiro * are met: 13125820Sgshapiro * 1. Redistributions of source code must retain the above copyright 1464562Sgshapiro * notices, this list of conditions and the following disclaimer. 1590792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright 1690792Sgshapiro * notices, this list of conditions and the following disclaimer in the 1790792Sgshapiro * documentation and/or other materials provided with the distribution. 1890792Sgshapiro * 3. All advertising materials mentioning features or use of this software 1964562Sgshapiro * must display the following acknowledgement: 2064562Sgshapiro * This product includes software developed by Softweyr LLC, the 2164562Sgshapiro * University of California, Berkeley, and its contributors. 2264562Sgshapiro * 4. Neither the name of the University nor the names of its contributors 2364562Sgshapiro * may be used to endorse or promote products derived from this software 2464562Sgshapiro * without specific prior written permission. 2564562Sgshapiro * 2690792Sgshapiro * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS 2764562Sgshapiro * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2880785Sgshapiro * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 2990792Sgshapiro * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE 3064562Sgshapiro * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3164562Sgshapiro * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 3264562Sgshapiro * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3364562Sgshapiro * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3464562Sgshapiro * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3564562Sgshapiro * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3664562Sgshapiro * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37125820Sgshapiro */ 38125820Sgshapiro 39125820Sgshapiro#include <sys/cdefs.h> 4064562Sgshapiro__FBSDID("$FreeBSD: head/lib/libc/string/wcstok.c 103067 2002-09-07 08:16:57Z tjr $"); 4164562Sgshapiro 4264562Sgshapiro#include <wchar.h> 4364562Sgshapiro 4464562Sgshapirowchar_t * 4564562Sgshapirowcstok(wchar_t * __restrict s, const wchar_t * __restrict delim, 4664562Sgshapiro wchar_t ** __restrict last) 4764562Sgshapiro{ 4864562Sgshapiro wchar_t *spanp, *tok; 4964562Sgshapiro wchar_t c, sc; 5064562Sgshapiro 51102528Sgshapiro if (s == NULL && (s = *last) == NULL) 52112810Sgshapiro return (NULL); 53112810Sgshapiro 54102528Sgshapiro /* 55102528Sgshapiro * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of). 56102528Sgshapiro */ 57102528Sgshapirocont: 58102528Sgshapiro c = *s++; 59102528Sgshapiro for (spanp = (wchar_t *)delim; (sc = *spanp++) != L'\0';) { 60102528Sgshapiro if (c == sc) 61102528Sgshapiro goto cont; 62102528Sgshapiro } 63112810Sgshapiro 64112810Sgshapiro if (c == L'\0') { /* no non-delimiter characters */ 65112810Sgshapiro *last = NULL; 66112810Sgshapiro return (NULL); 67112810Sgshapiro } 68112810Sgshapiro tok = s - 1; 69112810Sgshapiro 70112810Sgshapiro /* 71112810Sgshapiro * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of). 72112810Sgshapiro * Note that delim must have one NUL; we stop if we see that, too. 73102528Sgshapiro */ 74102528Sgshapiro for (;;) { 75102528Sgshapiro c = *s++; 76102528Sgshapiro spanp = (wchar_t *)delim; 77102528Sgshapiro do { 78102528Sgshapiro if ((sc = *spanp++) == c) { 79102528Sgshapiro if (c == L'\0') 80112810Sgshapiro s = NULL; 8164562Sgshapiro else 8264562Sgshapiro s[-1] = L'\0'; 8364562Sgshapiro *last = s; 8464562Sgshapiro return (tok); 8564562Sgshapiro } 8664562Sgshapiro } while (sc != L'\0'); 87102528Sgshapiro } 8864562Sgshapiro /* NOTREACHED */ 8964562Sgshapiro} 9064562Sgshapiro