19ad39134STim J. Robbins /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
49ad39134STim J. Robbins * Copyright (c) 1998 Softweyr LLC. All rights reserved.
59ad39134STim J. Robbins *
69ad39134STim J. Robbins * strtok_r, from Berkeley strtok
79ad39134STim J. Robbins * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
89ad39134STim J. Robbins *
99ad39134STim J. Robbins * Copyright (c) 1988, 1993
109ad39134STim J. Robbins * The Regents of the University of California. All rights reserved.
119ad39134STim J. Robbins *
129ad39134STim J. Robbins * Redistribution and use in source and binary forms, with or without
139ad39134STim J. Robbins * modification, are permitted provided that the following conditions
149ad39134STim J. Robbins * are met:
159ad39134STim J. Robbins * 1. Redistributions of source code must retain the above copyright
169ad39134STim J. Robbins * notices, this list of conditions and the following disclaimer.
179ad39134STim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright
189ad39134STim J. Robbins * notices, this list of conditions and the following disclaimer in the
199ad39134STim J. Robbins * documentation and/or other materials provided with the distribution.
203fb3b97cSEd Maste * 3. Neither the name of the University nor the names of its contributors
219ad39134STim J. Robbins * may be used to endorse or promote products derived from this software
229ad39134STim J. Robbins * without specific prior written permission.
239ad39134STim J. Robbins *
249ad39134STim J. Robbins * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
259ad39134STim J. Robbins * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
269ad39134STim J. Robbins * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
279ad39134STim J. Robbins * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
289ad39134STim J. Robbins * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
299ad39134STim J. Robbins * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
309ad39134STim J. Robbins * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
319ad39134STim J. Robbins * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
329ad39134STim J. Robbins * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
339ad39134STim J. Robbins * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
349ad39134STim J. Robbins * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
359ad39134STim J. Robbins */
369ad39134STim J. Robbins
379ad39134STim J. Robbins #include <wchar.h>
389ad39134STim J. Robbins
399ad39134STim J. Robbins wchar_t *
wcstok(wchar_t * __restrict s,const wchar_t * __restrict delim,wchar_t ** __restrict last)409ad39134STim J. Robbins wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
419ad39134STim J. Robbins wchar_t ** __restrict last)
429ad39134STim J. Robbins {
4307648c8bSTim J. Robbins const wchar_t *spanp;
4407648c8bSTim J. Robbins wchar_t *tok;
459ad39134STim J. Robbins wchar_t c, sc;
469ad39134STim J. Robbins
479ad39134STim J. Robbins if (s == NULL && (s = *last) == NULL)
489ad39134STim J. Robbins return (NULL);
499ad39134STim J. Robbins
509ad39134STim J. Robbins /*
519ad39134STim J. Robbins * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
529ad39134STim J. Robbins */
539ad39134STim J. Robbins cont:
549ad39134STim J. Robbins c = *s++;
5507648c8bSTim J. Robbins for (spanp = delim; (sc = *spanp++) != L'\0';) {
569ad39134STim J. Robbins if (c == sc)
579ad39134STim J. Robbins goto cont;
589ad39134STim J. Robbins }
599ad39134STim J. Robbins
609ad39134STim J. Robbins if (c == L'\0') { /* no non-delimiter characters */
619ad39134STim J. Robbins *last = NULL;
629ad39134STim J. Robbins return (NULL);
639ad39134STim J. Robbins }
649ad39134STim J. Robbins tok = s - 1;
659ad39134STim J. Robbins
669ad39134STim J. Robbins /*
679ad39134STim J. Robbins * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
689ad39134STim J. Robbins * Note that delim must have one NUL; we stop if we see that, too.
699ad39134STim J. Robbins */
709ad39134STim J. Robbins for (;;) {
719ad39134STim J. Robbins c = *s++;
7207648c8bSTim J. Robbins spanp = delim;
739ad39134STim J. Robbins do {
749ad39134STim J. Robbins if ((sc = *spanp++) == c) {
759ad39134STim J. Robbins if (c == L'\0')
769ad39134STim J. Robbins s = NULL;
779ad39134STim J. Robbins else
789ad39134STim J. Robbins s[-1] = L'\0';
799ad39134STim J. Robbins *last = s;
809ad39134STim J. Robbins return (tok);
819ad39134STim J. Robbins }
829ad39134STim J. Robbins } while (sc != L'\0');
839ad39134STim J. Robbins }
849ad39134STim J. Robbins /* NOTREACHED */
859ad39134STim J. Robbins }
86