1.\" $OpenBSD: strtok.3,v 1.21 2013/06/05 03:39:23 tedu Exp $ 2.\" 3.\" Copyright (c) 1988, 1991 The Regents of the University of California. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the American National Standards Committee X3, on Information 8.\" Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.Dd $Mdocdate: June 5 2013 $ 35.Dt STRTOK 3 36.Os 37.Sh NAME 38.Nm strtok , 39.Nm strtok_r 40.Nd string token operations 41.Sh SYNOPSIS 42.In string.h 43.Ft char * 44.Fn strtok "char *str" "const char *sep" 45.Ft char * 46.Fn strtok_r "char *str" "const char *sep" "char **last" 47.Sh DESCRIPTION 48.Bf -symbolic 49This interface is obsoleted by 50.Xr strsep 3 . 51.Ef 52.Pp 53The 54.Fn strtok 55function is used to isolate sequential tokens in a NUL-terminated string, 56.Fa str . 57These tokens are separated in the string by at least one of the 58characters in 59.Fa sep . 60The first time that 61.Fn strtok 62is called, 63.Fa str 64should be specified; subsequent calls, wishing to obtain further tokens 65from the same string, should pass a null pointer instead. 66The separator string, 67.Fa sep , 68must be supplied each time, and may change between calls. 69.Pp 70The 71.Fn strtok_r 72function is a version of 73.Fn strtok 74that takes an explicit context argument and is reentrant. 75.Pp 76Since 77.Fn strtok 78and 79.Fn strtok_r 80modify the string, 81.Fa str 82should not point to an area in the initialized data segment. 83.Sh RETURN VALUES 84The 85.Fn strtok 86and 87.Fn strtok_r 88functions return a pointer to the beginning of each subsequent token 89in the string, after replacing the separator character itself with an 90.Tn ASCII NUL 91character. 92When no more tokens remain, a null pointer is returned. 93.Sh EXAMPLES 94The following will construct an array of pointers to each individual word in 95the string 96.Va s : 97.Bd -literal -offset indent 98#define MAXTOKENS 128 99 100char s[512], *p, *tokens[MAXTOKENS]; 101char *last; 102int i = 0; 103 104snprintf(s, sizeof(s), "cat dog horse cow"); 105 106for ((p = strtok_r(s, " ", &last)); p; 107 (p = strtok_r(NULL, " ", &last))) { 108 if (i < MAXTOKENS - 1) 109 tokens[i++] = p; 110} 111tokens[i] = NULL; 112.Ed 113.Pp 114That is, 115.Li tokens[0] 116will point to 117.Qq cat , 118.Li tokens[1] 119will point to 120.Qq dog , 121.Li tokens[2] 122will point to 123.Qq horse , 124and 125.Li tokens[3] 126will point to 127.Qq cow . 128.Sh SEE ALSO 129.Xr memchr 3 , 130.Xr strchr 3 , 131.Xr strcspn 3 , 132.Xr strpbrk 3 , 133.Xr strrchr 3 , 134.Xr strsep 3 , 135.Xr strspn 3 , 136.Xr strstr 3 , 137.Xr wcstok 3 138.Sh STANDARDS 139The 140.Fn strtok 141function conforms to 142.St -ansiC . 143.Sh HISTORY 144The 145.Fn strtok 146function first appeared in 147.At III 148and was reimplemented for 149.Bx 4.3 Tahoe . 150The 151.Fn strtok_r 152function first appeared in 153.Nx 1.3 154and was reimplemented for 155.Ox 2.7 . 156.Sh BUGS 157The System V 158.Fn strtok , 159if handed a string containing only delimiter characters, 160will not alter the next starting point, so that a call to 161.Fn strtok 162with a different (or empty) delimiter string 163may return a non-null value. 164Since this implementation always alters the next starting point, 165such a sequence of calls would always return 166.Dv NULL . 167