1.\" $OpenBSD: strtok.3,v 1.24 2024/12/11 23:28:20 jsg 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: December 11 2024 $ 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 90a NUL character. 91When no more tokens remain, a null pointer is returned. 92.Sh EXAMPLES 93The following will construct an array of pointers to each individual word in 94the string 95.Va s : 96.Bd -literal -offset indent 97#define MAXTOKENS 128 98 99char s[512], *p, *tokens[MAXTOKENS]; 100char *last; 101int i = 0; 102 103snprintf(s, sizeof(s), "cat dog horse cow"); 104 105for ((p = strtok_r(s, " ", &last)); p; 106 (p = strtok_r(NULL, " ", &last))) { 107 if (i < MAXTOKENS - 1) 108 tokens[i++] = p; 109} 110tokens[i] = NULL; 111.Ed 112.Pp 113That is, 114.Li tokens[0] 115will point to 116.Qq cat , 117.Li tokens[1] 118will point to 119.Qq dog , 120.Li tokens[2] 121will point to 122.Qq horse , 123and 124.Li tokens[3] 125will point to 126.Qq cow . 127.Sh SEE ALSO 128.Xr memchr 3 , 129.Xr strchr 3 , 130.Xr strcspn 3 , 131.Xr strpbrk 3 , 132.Xr strrchr 3 , 133.Xr strsep 3 , 134.Xr strspn 3 , 135.Xr strstr 3 , 136.Xr wcstok 3 137.Sh STANDARDS 138The 139.Fn strtok 140function conforms to 141.St -ansiC . 142The 143.Fn strtok_r 144function conforms to 145.St -p1003.1-2001 . 146.Sh HISTORY 147The 148.Fn strtok 149function first appeared in 150.At III 151and was reimplemented for 152.Bx 4.3 . 153The 154.Fn strtok_r 155function first appeared in 156.Nx 1.3 157and was reimplemented for 158.Ox 2.7 . 159.Sh BUGS 160The System V 161.Fn strtok , 162if handed a string containing only delimiter characters, 163will not alter the next starting point, so that a call to 164.Fn strtok 165with a different (or empty) delimiter string 166may return a non-null value. 167Since this implementation always alters the next starting point, 168such a sequence of calls would always return 169.Dv NULL . 170