1.\" Copyright (c) 1988, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" $OpenBSD: strtok.3,v 1.19 2007/05/31 19:19:32 jmc Exp $ 33.\" 34.Dd $Mdocdate: May 31 2007 $ 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.Fd #include <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 76The 77.Fn strtok 78and 79.Fn strtok_r 80functions return a pointer to the beginning of each subsequent token 81in the string, after replacing the separator character itself with an 82.Tn ASCII NUL 83character. 84When no more tokens remain, a null pointer is returned. 85.Pp 86Since 87.Fn strtok 88and 89.Fn strtok_r 90modify the string, 91.Fa str 92should not point to an area in the initialized data segment. 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.Sh STANDARDS 138The 139.Fn strtok 140function conforms to 141.St -ansiC . 142.Sh BUGS 143The System V 144.Fn strtok , 145if handed a string containing only delimiter characters, 146will not alter the next starting point, so that a call to 147.Fn strtok 148with a different (or empty) delimiter string 149may return a non-null value. 150Since this implementation always alters the next starting point, 151such a sequence of calls would always return 152.Dv NULL . 153