master
 1/*-
 2 * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
 3 *
 4 * strtok_r, from Berkeley strtok
 5 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
 6 *
 7 * Copyright (c) 1988, 1993
 8 *	The Regents of the University of California.  All rights reserved.
 9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notices, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notices, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
26 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#define _POSIX_SOURCE 1
36#include <string.h>
37
38/*
39 * strtok_r documentation:
40 * http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html
41 *
42 * Implementation:
43 * http://svnweb.freebsd.org/base/head/lib/libc/string/strtok.c?view=co
44 *
45 * strtok_r cannot completely be implemented by strtok because of the internal state.
46 * It breaks when used on 2 strings where they are scanned A, B then A, B.
47 * Thread-safety is not the issue.
48 *
49 * Sample strtok implemenatation, note the internal state:
50 * char *strtok(char *s, const char *d) { static char *t; return strtok_r(s,d,t); }
51 *
52 */
53
54char *
55strtok_r(char * __restrict s, const char * __restrict delim, char ** __restrict last)
56{
57	char *spanp, *tok;
58	int c, sc;
59
60	if (s == NULL && (s = *last) == NULL)
61		return (NULL);
62
63	/*
64	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
65	 */
66cont:
67	c = *s++;
68	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
69		if (c == sc)
70			goto cont;
71	}
72
73	if (c == 0) {		/* no non-delimiter characters */
74		*last = NULL;
75		return (NULL);
76	}
77	tok = s - 1;
78
79	/*
80	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
81	 * Note that delim must have one NUL; we stop if we see that, too.
82	 */
83	for (;;) {
84		c = *s++;
85		spanp = (char *)delim;
86		do {
87			if ((sc = *spanp++) == c) {
88				if (c == 0)
89					s = NULL;
90				else
91					s[-1] = '\0';
92				*last = s;
93				return (tok);
94			}
95		} while (sc != 0);
96	}
97	/* NOTREACHED */
98}