diff options
author | aaron <> | 1999-07-03 15:58:50 +0000 |
---|---|---|
committer | aaron <> | 1999-07-03 15:58:50 +0000 |
commit | ea23c585195e4a1bed519c1bd535733bcfe47bb1 (patch) | |
tree | 631d49e1b6ca4547f06fbd0211cc5a78af9a62de | |
parent | 8813c538a1d0dc6b2173d32778f20b1b27b4164a (diff) | |
download | openbsd-ea23c585195e4a1bed519c1bd535733bcfe47bb1.tar.gz openbsd-ea23c585195e4a1bed519c1bd535733bcfe47bb1.tar.bz2 openbsd-ea23c585195e4a1bed519c1bd535733bcfe47bb1.zip |
add an example
-rw-r--r-- | src/lib/libc/string/strtok.3 | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3 index c8463200bf..d43046ae5c 100644 --- a/src/lib/libc/string/strtok.3 +++ b/src/lib/libc/string/strtok.3 | |||
@@ -33,7 +33,7 @@ | |||
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 | .\" $OpenBSD: strtok.3,v 1.6 1999/06/29 18:01:34 aaron Exp $ | 36 | .\" $OpenBSD: strtok.3,v 1.7 1999/07/03 15:58:50 aaron Exp $ |
37 | .\" | 37 | .\" |
38 | .Dd June 29, 1991 | 38 | .Dd June 29, 1991 |
39 | .Dt STRTOK 3 | 39 | .Dt STRTOK 3 |
@@ -73,10 +73,44 @@ The | |||
73 | .Fn strtok | 73 | .Fn strtok |
74 | function | 74 | function |
75 | returns a pointer to the beginning of each subsequent token in the string, | 75 | returns a pointer to the beginning of each subsequent token in the string, |
76 | after replacing the separator character itself with a | 76 | after replacing the separator character itself with an |
77 | .Dv NUL | 77 | .Tn ASCII NUL |
78 | character. | 78 | character. |
79 | When no more tokens remain, a null pointer is returned. | 79 | When no more tokens remain, a null pointer is returned. |
80 | .Pp | ||
81 | Since | ||
82 | .Fn strtok | ||
83 | modifies the string, | ||
84 | .Fa str | ||
85 | should not point to an area in the initialized data segment. | ||
86 | .Pp | ||
87 | .Sh EXAMPLES | ||
88 | The following will construct an array of pointers to each individual word in | ||
89 | the string | ||
90 | .Va s : | ||
91 | .Bd -literal -offset indent | ||
92 | #define MAXTOKENS 128 | ||
93 | |||
94 | char s[512], *p, *tokens[MAXTOKENS]; | ||
95 | int i = 0; | ||
96 | |||
97 | snprintf(s, sizeof(s), "cat dog horse cow"); | ||
98 | |||
99 | for ((p = strtok(s, " ")); p; (p = strtok(NULL, " ")), i++) { | ||
100 | if (i < MAXTOKENS) | ||
101 | tokens[i] = p; | ||
102 | } | ||
103 | tokens[i] = '\e0'; | ||
104 | .Ed | ||
105 | .Pp | ||
106 | That is, tokens[0] will point to | ||
107 | .Qq cat , | ||
108 | tokens[1] will point to | ||
109 | .Qq dog , | ||
110 | tokens[2] will point to | ||
111 | .Qq horse , | ||
112 | and tokens[3] will point to | ||
113 | .Qq cow . | ||
80 | .Sh SEE ALSO | 114 | .Sh SEE ALSO |
81 | .Xr index 3 , | 115 | .Xr index 3 , |
82 | .Xr memchr 3 , | 116 | .Xr memchr 3 , |
@@ -107,3 +141,4 @@ may return a non-null value. | |||
107 | Since this implementation always alters the next starting point, | 141 | Since this implementation always alters the next starting point, |
108 | such a sequence of calls would always return | 142 | such a sequence of calls would always return |
109 | .Dv NULL . | 143 | .Dv NULL . |
144 | |||