xref: /freebsd-14.2/lib/libc/string/strtok.c (revision 1936b2c8)
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 #ifdef DEBUG_STRTOK
45 #include <stdio.h>
46 #endif
47 #include <string.h>
48 
49 char *
50 strtok_r(char *s, const char *delim, char **last)
51 {
52 	int c, sc;
53 	char *spanp, *tok, *w;
54 
55 	if (s == NULL && (s = *last) == NULL)
56 		return (NULL);
57 
58 	/*
59 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
60 	 */
61 cont:
62 	c = *s++;
63 	for (spanp = (char *)delim; (sc = *spanp++) != 0;)
64 		if (c == sc)
65 			goto cont;
66 
67 	if (c == 0) {		/* no non-delimiter characters */
68 		*last = NULL;
69 		return (NULL);
70 	}
71 	tok = s - 1;
72 
73 	/*
74 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
75 	 * Note that delim must have one NUL; we stop if we see that, too.
76 	 */
77 	for (;;) {
78 		c = *s++;
79 		spanp = (char *)delim;
80 		do {
81 			if ((sc = *spanp++) == c) {
82 				if (c == 0)
83 					s = NULL;
84 				else {
85 					w = s - 1;
86 					*w = '\0';
87 				}
88 				*last = s;
89 				return (tok);
90 			}
91 		} while (sc != 0);
92 	}
93 	/* NOTREACHED */
94 }
95 
96 
97 char *
98 strtok(char *s, const char *delim)
99 {
100 	static char *last;
101 
102 	return (strtok_r(s, delim, &last));
103 }
104 
105 
106 #ifdef DEBUG_STRTOK
107 /*
108  * Test the tokenizer.
109  */
110 int
111 main(void)
112 {
113 	char blah[80], test[80];
114 	char *brkb, *brkt, *phrase, *sep, *word;
115 
116 	sep = "\\/:;=-";
117 	phrase = "foo";
118 
119 	printf("String tokenizer test:\n");
120 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
121 	for (word = strtok(test, sep); word; word = strtok(NULL, sep))
122 		printf("Next word is \"%s\".\n", word);
123 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
124 
125 	for (word = strtok_r(test, sep, &brkt); word;
126 	    word = strtok_r(NULL, sep, &brkt)) {
127 		strcpy(blah, "blah:blat:blab:blag");
128 
129 		for (phrase = strtok_r(blah, sep, &brkb); phrase;
130 		    phrase = strtok_r(NULL, sep, &brkb))
131 			printf("So far we're at %s:%s\n", word, phrase);
132 	}
133 
134 	return (0);
135 }
136 
137 #endif /* DEBUG_STRTOK */
138