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