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