]>
Commit | Line | Data |
---|---|---|
9385eb3d | 1 | /*- |
5b2abdfb A |
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: | |
5b2abdfb A |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notices, this list of conditions and the following disclaimer. | |
5b2abdfb A |
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. | |
5b2abdfb A |
18 | * 4. 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 | ||
9385eb3d A |
35 | #if defined(LIBC_SCCS) && !defined(lint) |
36 | static char sccsid[] = "@(#)strtok.c 8.1 (Berkeley) 6/4/93"; | |
37 | #endif /* LIBC_SCCS and not lint */ | |
38 | #include <sys/cdefs.h> | |
1f2f436a | 39 | __FBSDID("$FreeBSD: src/lib/libc/string/strtok.c,v 1.10 2007/12/12 18:33:06 wes Exp $"); |
5b2abdfb A |
40 | |
41 | #include <stddef.h> | |
9385eb3d A |
42 | #ifdef DEBUG_STRTOK |
43 | #include <stdio.h> | |
44 | #endif | |
5b2abdfb A |
45 | #include <string.h> |
46 | ||
9385eb3d A |
47 | char *__strtok_r(char *, const char *, char **); |
48 | ||
49 | __weak_reference(__strtok_r, strtok_r); | |
50 | ||
5b2abdfb | 51 | char * |
9385eb3d | 52 | __strtok_r(char *s, const char *delim, char **last) |
5b2abdfb | 53 | { |
9385eb3d A |
54 | char *spanp, *tok; |
55 | int c, sc; | |
56 | ||
57 | if (s == NULL && (s = *last) == NULL) | |
58 | return (NULL); | |
59 | ||
60 | /* | |
61 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
62 | */ | |
5b2abdfb | 63 | cont: |
5b2abdfb | 64 | c = *s++; |
9385eb3d A |
65 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
66 | if (c == sc) | |
67 | goto cont; | |
5b2abdfb | 68 | } |
5b2abdfb | 69 | |
9385eb3d A |
70 | if (c == 0) { /* no non-delimiter characters */ |
71 | *last = NULL; | |
72 | return (NULL); | |
73 | } | |
74 | tok = s - 1; | |
75 | ||
76 | /* | |
77 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | |
78 | * Note that delim must have one NUL; we stop if we see that, too. | |
79 | */ | |
80 | for (;;) { | |
81 | c = *s++; | |
82 | spanp = (char *)delim; | |
83 | do { | |
84 | if ((sc = *spanp++) == c) { | |
85 | if (c == 0) | |
86 | s = NULL; | |
87 | else | |
88 | s[-1] = '\0'; | |
89 | *last = s; | |
90 | return (tok); | |
91 | } | |
92 | } while (sc != 0); | |
93 | } | |
94 | /* NOTREACHED */ | |
95 | } | |
5b2abdfb A |
96 | |
97 | char * | |
98 | strtok(char *s, const char *delim) | |
99 | { | |
9385eb3d | 100 | static char *last; |
5b2abdfb | 101 | |
9385eb3d | 102 | return (__strtok_r(s, delim, &last)); |
5b2abdfb A |
103 | } |
104 | ||
9385eb3d | 105 | #ifdef DEBUG_STRTOK |
5b2abdfb A |
106 | /* |
107 | * Test the tokenizer. | |
108 | */ | |
109 | int | |
9385eb3d | 110 | main(void) |
5b2abdfb | 111 | { |
9385eb3d A |
112 | char blah[80], test[80]; |
113 | char *brkb, *brkt, *phrase, *sep, *word; | |
5b2abdfb | 114 | |
9385eb3d A |
115 | sep = "\\/:;=-"; |
116 | phrase = "foo"; | |
5b2abdfb | 117 | |
9385eb3d A |
118 | printf("String tokenizer test:\n"); |
119 | strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); | |
120 | for (word = strtok(test, sep); word; word = strtok(NULL, sep)) | |
121 | printf("Next word is \"%s\".\n", word); | |
122 | strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); | |
5b2abdfb | 123 | |
9385eb3d A |
124 | for (word = strtok_r(test, sep, &brkt); word; |
125 | word = strtok_r(NULL, sep, &brkt)) { | |
126 | strcpy(blah, "blah:blat:blab:blag"); | |
5b2abdfb | 127 | |
9385eb3d A |
128 | for (phrase = strtok_r(blah, sep, &brkb); phrase; |
129 | phrase = strtok_r(NULL, sep, &brkb)) | |
130 | printf("So far we're at %s:%s\n", word, phrase); | |
5b2abdfb | 131 | } |
5b2abdfb | 132 | |
9385eb3d | 133 | return (0); |
5b2abdfb A |
134 | } |
135 | ||
136 | #endif /* DEBUG_STRTOK */ |