Commit | Line | Data |
---|---|---|
716024cd | 1 | /*- |
984263bc MD |
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: | |
984263bc MD |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notices, this list of conditions and the following disclaimer. | |
984263bc MD |
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. | |
dc71b7ab | 18 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
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. | |
1de703da | 33 | * |
716024cd | 34 | * @(#)strtok.c 8.1 (Berkeley) 6/4/93 |
0d5acd74 | 35 | * $FreeBSD: head/lib/libc/string/strtok.c 251069 2013-05-28 20:57:40Z emaste $ |
984263bc MD |
36 | */ |
37 | ||
984263bc | 38 | #include <stddef.h> |
716024cd PA |
39 | #ifdef DEBUG_STRTOK |
40 | #include <stdio.h> | |
41 | #endif | |
984263bc MD |
42 | #include <string.h> |
43 | ||
619a518a | 44 | #ifdef _STANDALONE |
b8efa4bd MD |
45 | #define TLS_ATTRIBUTE |
46 | #define __thread | |
47 | #else | |
48 | #include "libc_private.h" | |
49 | #endif | |
50 | ||
716024cd PA |
51 | char *__strtok_r(char *, const char *, char **); |
52 | ||
984263bc | 53 | char * |
d33005aa SW |
54 | __strtok_r(char * __restrict s, const char * __restrict delim, |
55 | char ** __restrict last) | |
984263bc | 56 | { |
0d5acd74 | 57 | char *spanp, *tok; |
716024cd | 58 | int c, sc; |
716024cd PA |
59 | |
60 | if (s == NULL && (s = *last) == NULL) | |
61 | return (NULL); | |
62 | ||
63 | /* | |
64 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
65 | */ | |
984263bc | 66 | cont: |
984263bc | 67 | c = *s++; |
0d5acd74 | 68 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
716024cd PA |
69 | if (c == sc) |
70 | goto cont; | |
984263bc | 71 | } |
984263bc | 72 | |
716024cd PA |
73 | if (c == 0) { /* no non-delimiter characters */ |
74 | *last = NULL; | |
75 | return (NULL); | |
76 | } | |
77 | tok = s - 1; | |
78 | ||
79 | /* | |
80 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | |
81 | * Note that delim must have one NUL; we stop if we see that, too. | |
82 | */ | |
83 | for (;;) { | |
84 | c = *s++; | |
0d5acd74 | 85 | spanp = (char *)delim; |
716024cd PA |
86 | do { |
87 | if ((sc = *spanp++) == c) { | |
88 | if (c == 0) | |
89 | s = NULL; | |
90 | else | |
91 | s[-1] = '\0'; | |
92 | *last = s; | |
93 | return (tok); | |
94 | } | |
95 | } while (sc != 0); | |
96 | } | |
97 | /* NOTREACHED */ | |
98 | } | |
984263bc | 99 | |
0c4f129d | 100 | __weak_reference(__strtok_r, strtok_r); |
101 | ||
b8efa4bd MD |
102 | /* |
103 | * Even though strtok() is documented as not being thread-safe, | |
104 | * programs are getting so complex these days that more and more | |
105 | * code assumes thread-safety throughout libc. So make strtok() | |
106 | * thread-safe as well. | |
107 | */ | |
984263bc MD |
108 | char * |
109 | strtok(char *s, const char *delim) | |
110 | { | |
b8efa4bd | 111 | static __thread char *last TLS_ATTRIBUTE; |
984263bc | 112 | |
716024cd | 113 | return (__strtok_r(s, delim, &last)); |
984263bc MD |
114 | } |
115 | ||
716024cd | 116 | #ifdef DEBUG_STRTOK |
984263bc MD |
117 | /* |
118 | * Test the tokenizer. | |
119 | */ | |
120 | int | |
716024cd | 121 | main(void) |
984263bc | 122 | { |
716024cd PA |
123 | char blah[80], test[80]; |
124 | char *brkb, *brkt, *phrase, *sep, *word; | |
984263bc | 125 | |
716024cd PA |
126 | sep = "\\/:;=-"; |
127 | phrase = "foo"; | |
984263bc | 128 | |
716024cd PA |
129 | printf("String tokenizer test:\n"); |
130 | strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); | |
131 | for (word = strtok(test, sep); word; word = strtok(NULL, sep)) | |
132 | printf("Next word is \"%s\".\n", word); | |
133 | strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); | |
984263bc | 134 | |
716024cd PA |
135 | for (word = strtok_r(test, sep, &brkt); word; |
136 | word = strtok_r(NULL, sep, &brkt)) { | |
137 | strcpy(blah, "blah:blat:blab:blag"); | |
984263bc | 138 | |
716024cd PA |
139 | for (phrase = strtok_r(blah, sep, &brkb); phrase; |
140 | phrase = strtok_r(NULL, sep, &brkb)) | |
141 | printf("So far we're at %s:%s\n", word, phrase); | |
984263bc | 142 | } |
984263bc | 143 | |
716024cd | 144 | return (0); |
984263bc MD |
145 | } |
146 | ||
147 | #endif /* DEBUG_STRTOK */ |