xref: /dragonfly/lib/libc/string/strtok.c (revision 9348a738)
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  * @(#)strtok.c	8.1 (Berkeley) 6/4/93
35  * $FreeBSD: head/lib/libc/string/strtok.c 251069 2013-05-28 20:57:40Z emaste $
36  */
37 
38 #include <stddef.h>
39 #ifdef DEBUG_STRTOK
40 #include <stdio.h>
41 #endif
42 #include <string.h>
43 
44 char	*__strtok_r(char *, const char *, char **);
45 
46 char *
47 __strtok_r(char *s, const char *delim, char **last)
48 {
49 	char *spanp, *tok;
50 	int c, sc;
51 
52 	if (s == NULL && (s = *last) == NULL)
53 		return (NULL);
54 
55 	/*
56 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
57 	 */
58 cont:
59 	c = *s++;
60 	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
61 		if (c == sc)
62 			goto cont;
63 	}
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 					s[-1] = '\0';
84 				*last = s;
85 				return (tok);
86 			}
87 		} while (sc != 0);
88 	}
89 	/* NOTREACHED */
90 }
91 
92 __weak_reference(__strtok_r, strtok_r);
93 
94 char *
95 strtok(char *s, const char *delim)
96 {
97 	static char *last;
98 
99 	return (__strtok_r(s, delim, &last));
100 }
101 
102 #ifdef DEBUG_STRTOK
103 /*
104  * Test the tokenizer.
105  */
106 int
107 main(void)
108 {
109 	char blah[80], test[80];
110 	char *brkb, *brkt, *phrase, *sep, *word;
111 
112 	sep = "\\/:;=-";
113 	phrase = "foo";
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 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
120 
121 	for (word = strtok_r(test, sep, &brkt); word;
122 	    word = strtok_r(NULL, sep, &brkt)) {
123 		strcpy(blah, "blah:blat:blab:blag");
124 
125 		for (phrase = strtok_r(blah, sep, &brkb); phrase;
126 		    phrase = strtok_r(NULL, sep, &brkb))
127 			printf("So far we're at %s:%s\n", word, phrase);
128 	}
129 
130 	return (0);
131 }
132 
133 #endif /* DEBUG_STRTOK */
134