summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strtok.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/strtok.3')
-rw-r--r--src/lib/libc/string/strtok.384
1 files changed, 64 insertions, 20 deletions
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3
index 644bd10aed..b8dad8eff0 100644
--- a/src/lib/libc/string/strtok.3
+++ b/src/lib/libc/string/strtok.3
@@ -33,29 +33,30 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strtok.3 5.8 (Berkeley) 6/29/91 36.\" $OpenBSD: strtok.3,v 1.15 2001/08/06 10:42:26 mpech Exp $
37.\" $Id: strtok.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRTOK 3 39.Dt STRTOK 3
41.Os BSD 3 40.Os
42.Sh NAME 41.Sh NAME
43.Nm strtok , 42.Nm strtok ,
44.Nm strsep 43.Nm strtok_r
45.Nd string token operations 44.Nd string token operations
46.Sh SYNOPSIS 45.Sh SYNOPSIS
47.Fd #include <string.h> 46.Fd #include <string.h>
48.Ft char * 47.Ft char *
49.Fn strtok "char *str" "const char *sep" 48.Fn strtok "char *str" "const char *sep"
49.Ft char *
50.Fn strtok_r "char *str" "const char *sep" "char **last"
50.Sh DESCRIPTION 51.Sh DESCRIPTION
51.Bf -symbolic 52.Bf -symbolic
52This interface is obsoleted by strsep(3). 53This interface is obsoleted by
54.Xr strsep 3 .
53.Ef 55.Ef
54.Pp 56.Pp
55The 57The
56.Fn strtok 58.Fn strtok
57function 59function is used to isolate sequential tokens in a null-terminated string,
58is used to isolate sequential tokens in a null-terminated string,
59.Fa str . 60.Fa str .
60These tokens are separated in the string by at least one of the 61These tokens are separated in the string by at least one of the
61characters in 62characters in
@@ -71,17 +72,65 @@ The separator string,
71must be supplied each time, and may change between calls. 72must be supplied each time, and may change between calls.
72.Pp 73.Pp
73The 74The
75.Fn strtok_r
76function is a version of
74.Fn strtok 77.Fn strtok
75function 78that takes an explicit context argument and is reentrant.
76returns a pointer to the beginning of each subsequent token in the string, 79.Pp
77after replacing the separator character itself with a 80The
78.Dv NUL 81.Fn strtok
82and
83.Fn strtok_r
84functions return a pointer to the beginning of each subsequent token
85in the string, after replacing the separator character itself with an
86.Tn ASCII NUL
79character. 87character.
80When no more tokens remain, a null pointer is returned. 88When no more tokens remain, a null pointer is returned.
89.Pp
90Since
91.Fn strtok
92and
93.Fn strtok_r
94modify the string,
95.Fa str
96should not point to an area in the initialized data segment.
97.Sh EXAMPLES
98The following will construct an array of pointers to each individual word in
99the string
100.Va s :
101.Bd -literal -offset indent
102#define MAXTOKENS 128
103
104char s[512], *p, *tokens[MAXTOKENS];
105char *last;
106int i = 0;
107
108snprintf(s, sizeof(s), "cat dog horse cow");
109
110for ((p = strtok_r(s, " ", &last)); p;
111 (p = strtok_r(NULL, " ", &last)), i++) {
112 if (i < MAXTOKENS - 1)
113 tokens[i] = p;
114}
115tokens[i] = NULL;
116.Ed
117.Pp
118That is,
119.Li tokens[0]
120will point to
121.Qq cat ,
122.Li tokens[1]
123will point to
124.Qq dog ,
125.Li tokens[2]
126will point to
127.Qq horse ,
128and
129.Li tokens[3]
130will point to
131.Qq cow .
81.Sh SEE ALSO 132.Sh SEE ALSO
82.Xr index 3 ,
83.Xr memchr 3 , 133.Xr memchr 3 ,
84.Xr rindex 3 ,
85.Xr strchr 3 , 134.Xr strchr 3 ,
86.Xr strcspn 3 , 135.Xr strcspn 3 ,
87.Xr strpbrk 3 , 136.Xr strpbrk 3 ,
@@ -92,21 +141,16 @@ When no more tokens remain, a null pointer is returned.
92.Sh STANDARDS 141.Sh STANDARDS
93The 142The
94.Fn strtok 143.Fn strtok
95function 144function conforms to
96conforms to
97.St -ansiC . 145.St -ansiC .
98.Sh BUGS 146.Sh BUGS
99There is no way to get tokens from multiple strings simultaneously.
100.Pp
101The System V 147The System V
102.Fn strtok , 148.Fn strtok ,
103if handed a string containing only delimiter characters, 149if handed a string containing only delimiter characters,
104will not alter the next starting point, so that a call to 150will not alter the next starting point, so that a call to
105.Fn strtok 151.Fn strtok
106with a different (or empty) delimiter string 152with a different (or empty) delimiter string
107may return a 153may return a non-null value.
108.Pf non- Dv NULL
109value.
110Since this implementation always alters the next starting point, 154Since this implementation always alters the next starting point,
111such a sequence of calls would always return 155such a sequence of calls would always return
112.Dv NULL . 156.Dv NULL .