]> git.saurik.com Git - apple/libc.git/blame - string/strtok.c
Libc-262.tar.gz
[apple/libc.git] / string / strtok.c
CommitLineData
5b2abdfb
A
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 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notices, this list of conditions and the following disclaimer.
16 *
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 *
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 *
24 * This product includes software developed by Softweyr LLC, the
25 * University of California, Berkeley, and its contributors.
26 *
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
34 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
35 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44#ifndef lint
45static const char rcsid[] =
46 "$FreeBSD: src/lib/libc/string/strtok.c,v 1.2.6.1 2001/07/09 23:30:07 obrien Exp $";
47#endif
48
49#include <stddef.h>
50#include <string.h>
51
52char *
53strtok_r(char *s, const char *delim, char **last)
54{
55 char *spanp;
56 int c, sc;
57 char *tok;
58
59 if (s == NULL && (s = *last) == NULL)
60 {
61 return NULL;
62 }
63
64 /*
65 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
66 */
67cont:
68 c = *s++;
69 for (spanp = (char *)delim; (sc = *spanp++) != 0; )
70 {
71 if (c == sc)
72 {
73 goto cont;
74 }
75 }
76
77 if (c == 0) /* no non-delimiter characters */
78 {
79 *last = NULL;
80 return NULL;
81 }
82 tok = s - 1;
83
84 /*
85 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
86 * Note that delim must have one NUL; we stop if we see that, too.
87 */
88 for (;;)
89 {
90 c = *s++;
91 spanp = (char *)delim;
92 do
93 {
94 if ((sc = *spanp++) == c)
95 {
96 if (c == 0)
97 {
98 s = NULL;
99 }
100 else
101 {
102 char *w = s - 1;
103 *w = '\0';
104 }
105 *last = s;
106 return tok;
107 }
108 }
109 while (sc != 0);
110 }
111 /* NOTREACHED */
112}
113
114
115char *
116strtok(char *s, const char *delim)
117{
118 static char *last;
119
120 return strtok_r(s, delim, &last);
121}
122
123
124#if defined(DEBUG_STRTOK)
125
126/*
127 * Test the tokenizer.
128 */
129int
130main()
131{
132 char test[80], blah[80];
133 char *sep = "\\/:;=-";
134 char *word, *phrase, *brkt, *brkb;
135
136 printf("String tokenizer test:\n");
137
138 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
139
140 for (word = strtok(test, sep);
141 word;
142 word = strtok(NULL, sep))
143 {
144 printf("Next word is \"%s\".\n", word);
145 }
146
147 phrase = "foo";
148
149 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
150
151 for (word = strtok_r(test, sep, &brkt);
152 word;
153 word = strtok_r(NULL, sep, &brkt))
154 {
155 strcpy(blah, "blah:blat:blab:blag");
156
157 for (phrase = strtok_r(blah, sep, &brkb);
158 phrase;
159 phrase = strtok_r(NULL, sep, &brkb))
160 {
161 printf("So far we're at %s:%s\n", word, phrase);
162 }
163 }
164
165 return 0;
166}
167
168#endif /* DEBUG_STRTOK */