1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<strtok>>, <<strtok_r>>, <<strsep>>---get next token from a string
20
21 INDEX
22 strtok
23
24 INDEX
25 strtok_r
26
27 INDEX
28 strsep
29
30 SYNOPSIS
31 #include <string.h>
32 char *strtok(char *restrict <[source]>,
33 const char *restrict <[delimiters]>);
34 char *strtok_r(char *restrict <[source]>,
35 const char *restrict <[delimiters]>,
36 char **<[lasts]>);
37 char *strsep(char **<[source_ptr]>, const char *<[delimiters]>);
38
39 DESCRIPTION
40 The <<strtok>> function is used to isolate sequential tokens in a
41 null-terminated string, <<*<[source]>>>. These tokens are delimited
42 in the string by at least one of the characters in <<*<[delimiters]>>>.
43 The first time that <<strtok>> is called, <<*<[source]>>> should be
44 specified; subsequent calls, wishing to obtain further tokens from
45 the same string, should pass a null pointer instead. The separator
46 string, <<*<[delimiters]>>>, must be supplied each time and may
47 change between calls.
48
49 The <<strtok>> function returns a pointer to the beginning of each
50 subsequent token in the string, after replacing the separator
51 character itself with a null character. When no more tokens remain,
52 a null pointer is returned.
53
54 The <<strtok_r>> function has the same behavior as <<strtok>>, except
55 a pointer to placeholder <<*<[lasts]>>> must be supplied by the caller.
56
57 The <<strsep>> function is similar in behavior to <<strtok>>, except
58 a pointer to the string pointer must be supplied <<<[source_ptr]>>> and
59 the function does not skip leading delimiters. When the string starts
60 with a delimiter, the delimiter is changed to the null character and
61 the empty string is returned. Like <<strtok_r>> and <<strtok>>, the
62 <<*<[source_ptr]>>> is updated to the next character following the
63 last delimiter found or NULL if the end of string is reached with
64 no more delimiters.
65
66 RETURNS
67 <<strtok>>, <<strtok_r>>, and <<strsep>> all return a pointer to the
68 next token, or <<NULL>> if no more tokens can be found. For
69 <<strsep>>, a token may be the empty string.
70
71 NOTES
72 <<strtok>> is unsafe for multi-threaded applications. <<strtok_r>>
73 and <<strsep>> are thread-safe and should be used instead.
74
75 PORTABILITY
76 <<strtok>> is ANSI C.
77 <<strtok_r>> is POSIX.
78 <<strsep>> is a BSD extension.
79
80 <<strtok>>, <<strtok_r>>, and <<strsep>> require no supporting OS subroutines.
81
82 QUICKREF
83 strtok ansi impure
84 */
85
86 /* undef STRICT_ANSI so that strtok_r prototype will be defined */
87 #undef __STRICT_ANSI__
88 #include <string.h>
89 #include <stdlib.h>
90 #include <_ansi.h>
91
92 #ifndef _REENT_ONLY
93
94 static NEWLIB_THREAD_LOCAL char *_strtok_last;
95
96 extern char *__strtok_r (char *, const char *, char **, int);
97
98 char *
strtok(register char * __restrict s,register const char * __restrict delim)99 strtok (register char *__restrict s,
100 register const char *__restrict delim)
101 {
102 return __strtok_r (s, delim, &_strtok_last, 1);
103 }
104 #endif
105