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