]>
git.saurik.com Git - apple/libc.git/blob - string/FreeBSD/strtok.c
2 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
4 * strtok_r, from Berkeley strtok
5 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Softweyr LLC, the
21 * University of California, Berkeley, and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
30 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid
[] = "@(#)strtok.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/lib/libc/string/strtok.c,v 1.9 2002/09/07 02:53:19 tjr Exp $");
51 char *__strtok_r(char *, const char *, char **);
53 __weak_reference(__strtok_r
, strtok_r
);
56 __strtok_r(char *s
, const char *delim
, char **last
)
61 if (s
== NULL
&& (s
= *last
) == NULL
)
65 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
69 for (spanp
= (char *)delim
; (sc
= *spanp
++) != 0;) {
74 if (c
== 0) { /* no non-delimiter characters */
81 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
82 * Note that delim must have one NUL; we stop if we see that, too.
86 spanp
= (char *)delim
;
88 if ((sc
= *spanp
++) == c
) {
102 strtok(char *s
, const char *delim
)
106 return (__strtok_r(s
, delim
, &last
));
111 * Test the tokenizer.
116 char blah
[80], test
[80];
117 char *brkb
, *brkt
, *phrase
, *sep
, *word
;
122 printf("String tokenizer test:\n");
123 strcpy(test
, "This;is.a:test:of=the/string\\tokenizer-function.");
124 for (word
= strtok(test
, sep
); word
; word
= strtok(NULL
, sep
))
125 printf("Next word is \"%s\".\n", word
);
126 strcpy(test
, "This;is.a:test:of=the/string\\tokenizer-function.");
128 for (word
= strtok_r(test
, sep
, &brkt
); word
;
129 word
= strtok_r(NULL
, sep
, &brkt
)) {
130 strcpy(blah
, "blah:blat:blab:blag");
132 for (phrase
= strtok_r(blah
, sep
, &brkb
); phrase
;
133 phrase
= strtok_r(NULL
, sep
, &brkb
))
134 printf("So far we're at %s:%s\n", word
, phrase
);
140 #endif /* DEBUG_STRTOK */