]>
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 | * 3. All advertising materials mentioning features or use of this software |
19 | * must display the following acknowledgement: | |
5b2abdfb A |
20 | * This product includes software developed by Softweyr LLC, the |
21 | * University of California, Berkeley, and its contributors. | |
5b2abdfb A |
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. | |
25 | * | |
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. | |
37 | */ | |
38 | ||
9385eb3d A |
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 $"); | |
5b2abdfb A |
44 | |
45 | #include <stddef.h> | |
9385eb3d A |
46 | #ifdef DEBUG_STRTOK |
47 | #include <stdio.h> | |
48 | #endif | |
5b2abdfb A |
49 | #include <string.h> |
50 | ||
9385eb3d A |
51 | char *__strtok_r(char *, const char *, char **); |
52 | ||
53 | __weak_reference(__strtok_r, strtok_r); | |
54 | ||
5b2abdfb | 55 | char * |
9385eb3d | 56 | __strtok_r(char *s, const char *delim, char **last) |
5b2abdfb | 57 | { |
9385eb3d A |
58 | char *spanp, *tok; |
59 | int c, sc; | |
60 | ||
61 | if (s == NULL && (s = *last) == NULL) | |
62 | return (NULL); | |
63 | ||
64 | /* | |
65 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
66 | */ | |
5b2abdfb | 67 | cont: |
5b2abdfb | 68 | c = *s++; |
9385eb3d A |
69 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
70 | if (c == sc) | |
71 | goto cont; | |
5b2abdfb | 72 | } |
5b2abdfb | 73 | |
9385eb3d A |
74 | if (c == 0) { /* no non-delimiter characters */ |
75 | *last = NULL; | |
76 | return (NULL); | |
77 | } | |
78 | tok = s - 1; | |
79 | ||
80 | /* | |
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. | |
83 | */ | |
84 | for (;;) { | |
85 | c = *s++; | |
86 | spanp = (char *)delim; | |
87 | do { | |
88 | if ((sc = *spanp++) == c) { | |
89 | if (c == 0) | |
90 | s = NULL; | |
91 | else | |
92 | s[-1] = '\0'; | |
93 | *last = s; | |
94 | return (tok); | |
95 | } | |
96 | } while (sc != 0); | |
97 | } | |
98 | /* NOTREACHED */ | |
99 | } | |
5b2abdfb A |
100 | |
101 | char * | |
102 | strtok(char *s, const char *delim) | |
103 | { | |
9385eb3d | 104 | static char *last; |
5b2abdfb | 105 | |
9385eb3d | 106 | return (__strtok_r(s, delim, &last)); |
5b2abdfb A |
107 | } |
108 | ||
9385eb3d | 109 | #ifdef DEBUG_STRTOK |
5b2abdfb A |
110 | /* |
111 | * Test the tokenizer. | |
112 | */ | |
113 | int | |
9385eb3d | 114 | main(void) |
5b2abdfb | 115 | { |
9385eb3d A |
116 | char blah[80], test[80]; |
117 | char *brkb, *brkt, *phrase, *sep, *word; | |
5b2abdfb | 118 | |
9385eb3d A |
119 | sep = "\\/:;=-"; |
120 | phrase = "foo"; | |
5b2abdfb | 121 | |
9385eb3d A |
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."); | |
5b2abdfb | 127 | |
9385eb3d A |
128 | for (word = strtok_r(test, sep, &brkt); word; |
129 | word = strtok_r(NULL, sep, &brkt)) { | |
130 | strcpy(blah, "blah:blat:blab:blag"); | |
5b2abdfb | 131 | |
9385eb3d A |
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); | |
5b2abdfb | 135 | } |
5b2abdfb | 136 | |
9385eb3d | 137 | return (0); |
5b2abdfb A |
138 | } |
139 | ||
140 | #endif /* DEBUG_STRTOK */ |