|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // strtok.cxx |
|
|
4 // |
|
2
|
5 // ISO standard strtok() routine |
|
0
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour |
|
|
34 // Date: 1999-01-19 |
|
|
35 // Purpose: Provide ISO C strtok() and POSIX strtok_r() routines |
|
0
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 // |
|
|
43 // This code is based on original code with the following copyright: |
|
|
44 // |
|
|
45 /* |
|
|
46 * Copyright (c) 1988 Regents of the University of California. |
|
|
47 * All rights reserved. |
|
|
48 * |
|
|
49 * Redistribution and use in source and binary forms, with or without |
|
|
50 * modification, are permitted provided that the following conditions |
|
|
51 * are met: |
|
|
52 * 1. Redistributions of source code must retain the above copyright |
|
|
53 * notice, this list of conditions and the following disclaimer. |
|
|
54 * 2. Redistributions in binary form must reproduce the above copyright |
|
|
55 * notice, this list of conditions and the following disclaimer in the |
|
|
56 * documentation and/or other materials provided with the distribution. |
|
|
57 * 3. All advertising materials mentioning features or use of this software |
|
|
58 * must display the following acknowledgement: |
|
|
59 * This product includes software developed by the University of |
|
|
60 * California, Berkeley and its contributors. |
|
|
61 * 4. Neither the name of the University nor the names of its contributors |
|
|
62 * may be used to endorse or promote products derived from this software |
|
|
63 * without specific prior written permission. |
|
|
64 * |
|
|
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
|
|
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
|
|
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
|
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
|
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
75 * SUCH DAMAGE. |
|
|
76 */ |
|
|
77 |
|
|
78 |
|
|
79 // CONFIGURATION |
|
|
80 |
|
|
81 #include <pkgconf/libc.h> // Configuration header |
|
|
82 |
|
|
83 // INCLUDES |
|
|
84 |
|
|
85 #include <cyg/infra/cyg_type.h> // Common type definitions |
|
|
86 #include <cyg/infra/cyg_trac.h> // Tracing support |
|
|
87 #include <cyg/infra/cyg_ass.h> // Assertion support |
|
|
88 #include <string.h> // Header for this file |
|
|
89 #include <stddef.h> // Compiler definitions such as size_t, NULL etc. |
|
|
90 #include "clibincl/stringsupp.hxx" // Useful string function support and |
|
|
91 // prototypes |
|
2
|
92 |
|
|
93 #ifdef CYGSEM_LIBC_PER_THREAD_STRTOK |
|
|
94 # include <pkgconf/kernel.h> // kernel configuration |
|
|
95 # include <cyg/kernel/thread.hxx> // per-thread data |
|
|
96 # include <cyg/kernel/thread.inl> // per-thread data |
|
|
97 # include <cyg/kernel/mutex.hxx> // mutexes |
|
|
98 #endif |
|
0
|
99 |
|
|
100 // EXPORTED SYMBOLS |
|
|
101 |
|
|
102 externC char * |
|
2
|
103 strtok( char *s1, const char *s2 ) CYGBLD_ATTRIB_WEAK_ALIAS(_strtok); |
|
0
|
104 |
|
|
105 externC char * |
|
|
106 strtok_r( char *s1, const char *s2, char **lasts ) \ |
|
2
|
107 CYGBLD_ATTRIB_WEAK_ALIAS(_strtok_r); |
|
0
|
108 |
|
|
109 // TRACE |
|
|
110 |
|
|
111 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_STRTOK_TRACE_LEVEL) |
|
|
112 static int strtok_trace = CYGNUM_LIBC_STRTOK_TRACE_LEVEL; |
|
|
113 # define TL1 (0 < strtok_trace) |
|
|
114 #else |
|
|
115 # define TL1 (0) |
|
|
116 #endif |
|
|
117 |
|
2
|
118 // STATICS |
|
|
119 |
|
|
120 #ifdef CYGSEM_LIBC_PER_THREAD_STRTOK |
|
|
121 static cyg_ucount32 strtok_data_index=CYGNUM_KERNEL_THREADS_DATA_MAX; |
|
|
122 static Cyg_Mutex strtok_data_mutex CYG_INIT_PRIORITY(LIBC); |
|
|
123 #else |
|
|
124 static char *cyg_libc_strtok_last; |
|
|
125 #endif |
|
0
|
126 |
|
|
127 // FUNCTIONS |
|
|
128 |
|
|
129 char * |
|
|
130 _strtok( char *s1, const char *s2 ) |
|
|
131 { |
|
|
132 char **lasts; |
|
|
133 char *retval; |
|
|
134 |
|
|
135 CYG_REPORT_FUNCNAMETYPE( "_strtok", "returning %08x" ); |
|
|
136 CYG_REPORT_FUNCARG2( "s1=%08x, s2=%08x", s1, s2 ); |
|
|
137 |
|
|
138 if (s1 != NULL) |
|
|
139 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); |
|
|
140 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); |
|
|
141 |
|
2
|
142 #ifdef CYGSEM_LIBC_PER_THREAD_STRTOK |
|
|
143 Cyg_Thread *self = Cyg_Thread::self(); |
|
|
144 |
|
|
145 // Get a per-thread data slot if we haven't got one already |
|
|
146 // Do a simple test before locking and retrying test, as this is a |
|
|
147 // rare situation |
|
|
148 if (CYGNUM_KERNEL_THREADS_DATA_MAX==strtok_data_index) { |
|
|
149 strtok_data_mutex.lock(); |
|
|
150 if (CYGNUM_KERNEL_THREADS_DATA_MAX==strtok_data_index) { |
|
0
|
151 |
|
2
|
152 // the kernel just throws an assert if this doesn't work |
|
|
153 // FIXME: Should use real CDL to pre-allocate a slot at compile |
|
|
154 // time to ensure there are enough slots |
|
|
155 strtok_data_index = self->new_data_index(); |
|
|
156 |
|
|
157 } |
|
|
158 strtok_data_mutex.unlock(); |
|
|
159 } // if |
|
|
160 |
|
|
161 // we have a valid index now |
|
|
162 |
|
|
163 lasts = (char **)self->get_data_ptr(strtok_data_index); |
|
|
164 #else |
|
|
165 lasts = &cyg_libc_strtok_last; |
|
|
166 #endif |
|
0
|
167 |
|
|
168 CYG_TRACE2( TL1, "Retrieved strtok_last address %08x containing %s", |
|
|
169 lasts, *lasts ); |
|
|
170 |
|
|
171 retval = _strtok_r( s1, s2, lasts ); |
|
|
172 |
|
|
173 CYG_REPORT_RETVAL( retval ); |
|
|
174 |
|
|
175 return retval; |
|
|
176 } // _strtok() |
|
|
177 |
|
|
178 char * |
|
|
179 _strtok_r( char *s1, const char *s2, char **lasts ) |
|
|
180 { |
|
|
181 char *spanp; |
|
|
182 int c, sc; |
|
|
183 char *tok; |
|
|
184 |
|
|
185 CYG_REPORT_FUNCNAMETYPE( "_strtok_r", "returning %08x" ); |
|
|
186 CYG_REPORT_FUNCARG3( "s1=%08x, s2=%08x, lasts=%08x", s1, s2, lasts ); |
|
|
187 |
|
|
188 if (s1 != NULL) |
|
|
189 CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); |
|
|
190 CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); |
|
|
191 CYG_CHECK_DATA_PTR( lasts, "lasts is not a valid pointer!" ); |
|
|
192 |
|
|
193 |
|
|
194 if (s1 == NULL && (s1 = *lasts) == NULL) |
|
|
195 { |
|
|
196 CYG_REPORT_RETVAL( NULL ); |
|
|
197 return NULL; |
|
|
198 } // if |
|
|
199 |
|
|
200 // |
|
|
201 // Skip (span) leading delimiters (s += strspn(s, delim), sort of). |
|
|
202 // |
|
|
203 cont: |
|
|
204 c = *s1++; |
|
|
205 for (spanp = (char *)s2; (sc = *spanp++) != 0;) { |
|
|
206 if (c == sc) |
|
|
207 goto cont; |
|
|
208 } // for |
|
|
209 |
|
|
210 if (c == 0) { // no non-delimiter characters |
|
|
211 *lasts = NULL; |
|
|
212 |
|
|
213 CYG_REPORT_RETVAL( NULL ); |
|
|
214 return NULL; |
|
|
215 } // if |
|
|
216 tok = s1 - 1; |
|
|
217 |
|
|
218 // |
|
|
219 // Scan token (scan for delimiters: s += strcspn(s, delim), sort of). |
|
|
220 // Note that delim must have one NUL; we stop if we see that, too. |
|
|
221 // |
|
|
222 for (;;) { |
|
|
223 c = *s1++; |
|
|
224 spanp = (char *)s2; |
|
|
225 do { |
|
|
226 if ((sc = *spanp++) == c) { |
|
|
227 if (c == 0) |
|
|
228 s1 = NULL; |
|
|
229 else |
|
|
230 s1[-1] = 0; |
|
|
231 *lasts = s1; |
|
|
232 |
|
|
233 CYG_REPORT_RETVAL( tok ); |
|
|
234 |
|
|
235 return (tok); |
|
|
236 } // if |
|
|
237 } while (sc != 0); |
|
|
238 } // for |
|
|
239 // NOTREACHED |
|
|
240 } // _strtok_r() |
|
|
241 |
|
|
242 // EOF strtok.cxx |