xref: /freebsd/lib/libc/string/strtok.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
5  *
6  * strtok_r, from Berkeley strtok
7  * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
8  *
9  * Copyright (c) 1988, 1993
10  *	The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notices, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notices, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
28  * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <stddef.h>
38 #ifdef DEBUG_STRTOK
39 #include <stdio.h>
40 #endif
41 #include <string.h>
42 
43 char	*__strtok_r(char *, const char *, char **);
44 
45 __weak_reference(__strtok_r, strtok_r);
46 
47 char *
48 __strtok_r(char *s, const char *delim, char **last)
49 {
50 	char *spanp, *tok;
51 	int c, sc;
52 
53 	if (s == NULL && (s = *last) == NULL)
54 		return (NULL);
55 
56 	/*
57 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
58 	 */
59 cont:
60 	c = *s++;
61 	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
62 		if (c == sc)
63 			goto cont;
64 	}
65 
66 	if (c == 0) {		/* no non-delimiter characters */
67 		*last = NULL;
68 		return (NULL);
69 	}
70 	tok = s - 1;
71 
72 	/*
73 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
74 	 * Note that delim must have one NUL; we stop if we see that, too.
75 	 */
76 	for (;;) {
77 		c = *s++;
78 		spanp = (char *)delim;
79 		do {
80 			if ((sc = *spanp++) == c) {
81 				if (c == 0)
82 					s = NULL;
83 				else
84 					s[-1] = '\0';
85 				*last = s;
86 				return (tok);
87 			}
88 		} while (sc != 0);
89 	}
90 	/* NOTREACHED */
91 }
92 
93 char *
94 strtok(char *s, const char *delim)
95 {
96 	static char *last;
97 
98 	return (__strtok_r(s, delim, &last));
99 }
100 
101 #ifdef DEBUG_STRTOK
102 /*
103  * Test the tokenizer.
104  */
105 int
106 main(void)
107 {
108 	char blah[80], test[80];
109 	char *brkb, *brkt, *phrase, *sep, *word;
110 
111 	sep = "\\/:;=-";
112 	phrase = "foo";
113 
114 	printf("String tokenizer test:\n");
115 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
116 	for (word = strtok(test, sep); word; word = strtok(NULL, sep))
117 		printf("Next word is \"%s\".\n", word);
118 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
119 
120 	for (word = strtok_r(test, sep, &brkt); word;
121 	    word = strtok_r(NULL, sep, &brkt)) {
122 		strcpy(blah, "blah:blat:blab:blag");
123 
124 		for (phrase = strtok_r(blah, sep, &brkb); phrase;
125 		    phrase = strtok_r(NULL, sep, &brkb))
126 			printf("So far we're at %s:%s\n", word, phrase);
127 	}
128 
129 	return (0);
130 }
131 
132 #endif /* DEBUG_STRTOK */
133