Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / string / strtok.c
blob2a11c9196bb4dfe3f503d79832adcb7f43ba769f
1 /*
2 FUNCTION
3 <<strtok>>, <<strtok_r>>, <<strsep>>---get next token from a string
5 INDEX
6 strtok
8 INDEX
9 strtok_r
11 INDEX
12 strsep
14 SYNOPSIS
15 #include <string.h>
16 char *strtok(char *restrict <[source]>,
17 const char *restrict <[delimiters]>);
18 char *strtok_r(char *restrict <[source]>,
19 const char *restrict <[delimiters]>,
20 char **<[lasts]>);
21 char *strsep(char **<[source_ptr]>, const char *<[delimiters]>);
23 DESCRIPTION
24 The <<strtok>> function is used to isolate sequential tokens in a
25 null-terminated string, <<*<[source]>>>. These tokens are delimited
26 in the string by at least one of the characters in <<*<[delimiters]>>>.
27 The first time that <<strtok>> is called, <<*<[source]>>> should be
28 specified; subsequent calls, wishing to obtain further tokens from
29 the same string, should pass a null pointer instead. The separator
30 string, <<*<[delimiters]>>>, must be supplied each time and may
31 change between calls.
33 The <<strtok>> function returns a pointer to the beginning of each
34 subsequent token in the string, after replacing the separator
35 character itself with a null character. When no more tokens remain,
36 a null pointer is returned.
38 The <<strtok_r>> function has the same behavior as <<strtok>>, except
39 a pointer to placeholder <<*<[lasts]>>> must be supplied by the caller.
41 The <<strsep>> function is similar in behavior to <<strtok>>, except
42 a pointer to the string pointer must be supplied <<<[source_ptr]>>> and
43 the function does not skip leading delimiters. When the string starts
44 with a delimiter, the delimiter is changed to the null character and
45 the empty string is returned. Like <<strtok_r>> and <<strtok>>, the
46 <<*<[source_ptr]>>> is updated to the next character following the
47 last delimiter found or NULL if the end of string is reached with
48 no more delimiters.
50 RETURNS
51 <<strtok>>, <<strtok_r>>, and <<strsep>> all return a pointer to the
52 next token, or <<NULL>> if no more tokens can be found. For
53 <<strsep>>, a token may be the empty string.
55 NOTES
56 <<strtok>> is unsafe for multi-threaded applications. <<strtok_r>>
57 and <<strsep>> are thread-safe and should be used instead.
59 PORTABILITY
60 <<strtok>> is ANSI C.
61 <<strtok_r>> is POSIX.
62 <<strsep>> is a BSD extension.
64 <<strtok>>, <<strtok_r>>, and <<strsep>> require no supporting OS subroutines.
66 QUICKREF
67 strtok ansi impure
70 /* undef STRICT_ANSI so that strtok_r prototype will be defined */
71 #undef __STRICT_ANSI__
72 #include <string.h>
73 #include <stdlib.h>
74 #include <_ansi.h>
75 #include <reent.h>
77 #ifdef _REENT_THREAD_LOCAL
78 _Thread_local char *_tls_strtok_last;
79 #endif
81 #ifndef _REENT_ONLY
83 extern char *__strtok_r (char *, const char *, char **, int);
85 char *
86 strtok (register char *__restrict s,
87 register const char *__restrict delim)
89 struct _reent *reent = _REENT;
91 _REENT_CHECK_MISC(reent);
92 return __strtok_r (s, delim, &(_REENT_STRTOK_LAST(reent)), 1);
94 #endif