1*f14fb602SLionel Sambuc /* $NetBSD: wcstok.c,v 1.4 2012/06/25 22:32:46 abs Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1998 Softweyr LLC. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * strtok_r, from Berkeley strtok
72fe8fb19SBen Gras * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
82fe8fb19SBen Gras *
92fe8fb19SBen Gras * Copyright (c) 1988, 1993
102fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
112fe8fb19SBen Gras *
122fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
132fe8fb19SBen Gras * modification, are permitted provided that the following conditions
142fe8fb19SBen Gras * are met:
152fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
162fe8fb19SBen Gras * notices, this list of conditions and the following disclaimer.
172fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
182fe8fb19SBen Gras * notices, this list of conditions and the following disclaimer in the
192fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
202fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software
212fe8fb19SBen Gras * must display the following acknowledgement:
222fe8fb19SBen Gras * This product includes software developed by Softweyr LLC, the
232fe8fb19SBen Gras * University of California, Berkeley, and its contributors.
242fe8fb19SBen Gras * 4. Neither the name of the University nor the names of its contributors
252fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
262fe8fb19SBen Gras * without specific prior written permission.
272fe8fb19SBen Gras *
282fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
292fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302fe8fb19SBen Gras * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
312fe8fb19SBen Gras * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
322fe8fb19SBen Gras * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332fe8fb19SBen Gras * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
342fe8fb19SBen Gras * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
352fe8fb19SBen Gras * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
362fe8fb19SBen Gras * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
372fe8fb19SBen Gras * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
382fe8fb19SBen Gras * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392fe8fb19SBen Gras *
402fe8fb19SBen Gras * Original version ID:
412fe8fb19SBen Gras * FreeBSD: src/lib/libc/string/wcstok.c,v 1.1 2002/09/07 08:16:57 tjr Exp
422fe8fb19SBen Gras */
432fe8fb19SBen Gras
442fe8fb19SBen Gras #include <sys/cdefs.h>
452fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
46*f14fb602SLionel Sambuc __RCSID("$NetBSD: wcstok.c,v 1.4 2012/06/25 22:32:46 abs Exp $");
472fe8fb19SBen Gras #endif
482fe8fb19SBen Gras
492fe8fb19SBen Gras #include <assert.h>
502fe8fb19SBen Gras #include <wchar.h>
512fe8fb19SBen Gras
522fe8fb19SBen Gras wchar_t *
wcstok(wchar_t * __restrict s,const wchar_t * __restrict delim,wchar_t ** __restrict last)53*f14fb602SLionel Sambuc wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
54*f14fb602SLionel Sambuc wchar_t ** __restrict last)
552fe8fb19SBen Gras {
562fe8fb19SBen Gras const wchar_t *spanp;
572fe8fb19SBen Gras wchar_t c, sc;
582fe8fb19SBen Gras wchar_t *tok;
592fe8fb19SBen Gras
602fe8fb19SBen Gras /* s may be NULL */
612fe8fb19SBen Gras _DIAGASSERT(delim != NULL);
622fe8fb19SBen Gras _DIAGASSERT(last != NULL);
632fe8fb19SBen Gras
642fe8fb19SBen Gras if (s == NULL && (s = *last) == NULL)
652fe8fb19SBen Gras return (NULL);
662fe8fb19SBen Gras
672fe8fb19SBen Gras /*
682fe8fb19SBen Gras * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
692fe8fb19SBen Gras */
702fe8fb19SBen Gras cont:
712fe8fb19SBen Gras c = *s++;
722fe8fb19SBen Gras for (spanp = delim; (sc = *spanp++) != L'\0';) {
732fe8fb19SBen Gras if (c == sc)
742fe8fb19SBen Gras goto cont;
752fe8fb19SBen Gras }
762fe8fb19SBen Gras
772fe8fb19SBen Gras if (c == L'\0') { /* no non-delimiter characters */
782fe8fb19SBen Gras *last = NULL;
792fe8fb19SBen Gras return (NULL);
802fe8fb19SBen Gras }
812fe8fb19SBen Gras tok = s - 1;
822fe8fb19SBen Gras
832fe8fb19SBen Gras /*
842fe8fb19SBen Gras * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
852fe8fb19SBen Gras * Note that delim must have one NUL; we stop if we see that, too.
862fe8fb19SBen Gras */
872fe8fb19SBen Gras for (;;) {
882fe8fb19SBen Gras c = *s++;
892fe8fb19SBen Gras spanp = delim;
902fe8fb19SBen Gras do {
912fe8fb19SBen Gras if ((sc = *spanp++) == c) {
922fe8fb19SBen Gras if (c == L'\0')
932fe8fb19SBen Gras s = NULL;
942fe8fb19SBen Gras else
952fe8fb19SBen Gras s[-1] = L'\0';
962fe8fb19SBen Gras *last = s;
972fe8fb19SBen Gras return (tok);
982fe8fb19SBen Gras }
992fe8fb19SBen Gras } while (sc != L'\0');
1002fe8fb19SBen Gras }
1012fe8fb19SBen Gras /* NOTREACHED */
1022fe8fb19SBen Gras }
103