STRTOK(3POSIX)
โจ strtok, strtok_r โ split string into tokens โจ
okay so bestie, welcome to STRTOK(3POSIX), straight outta the POSIX Programmer's Manual ๐ this is the page where we learn how to chop a string into lil pieces (tokens) using a set of separator bytes. it's giving "string surgery but make it cute" ๐ช๐ธ
๐ PROLOG ๐
heads up, bestie!! this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for the deets on Linux behavior), or โ plot twist โ the interface may not be implemented on Linux at all. ๐
๐ NAME ๐
strtok, strtok_r โ split string into tokens
๐ SYNOPSIS ๐
#include <string.h>
char *strtok(char *restrict s, const char *restrict sep);
char *strtok_r(char *restrict s, const char *restrict sep,
char **restrict state);
๐ DESCRIPTION ๐
okay so for strtok() specifically: the functionality described on this reference page is aligned with the ISO C standard. and like, any conflict between the requirements described here and the ISO C standard? unintentional, fr fr. this volume of POSIX.1โ2017 defers to the ISO C standard. ๐ค
a sequence of calls to strtok() breaks the string pointed to by s into a sequence of tokens, each of which is delimited by a byte from the string pointed to by sep. the first call in the sequence has s as its first argument, and is followed by calls with a null pointer as their first argument. and the separator string pointed to by sep may be different from call to call โ no cap, you can swap it out mid-sequence. ๐
the first call in the sequence searches the string pointed to by s for the first byte that is not contained in the current separator string pointed to by sep. if no such byte is found, then there are no tokens in the string pointed to by s, and strtok() shall return a null pointer. if such a byte is found, it is the start of the first token. โจ
then strtok() searches from there for a byte that is contained in the current separator string. if no such byte is found, the current token extends to the end of the string pointed to by s, and subsequent searches for a token shall return a null pointer. if such a byte is found, it is overwritten by a NUL character, which terminates the current token. strtok() then saves a pointer to the following byte, from which the next search for a token shall start. ๐
each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above. ๐ซถ
the implementation shall behave as if no function defined in this volume of POSIX.1โ2017 calls strtok().
โ ๏ธ the strtok() function need not be thread-safe. be normal about that. ๐ญ
the strtok_r() function shall be equivalent to strtok(), except that strtok_r() shall be thread-safe and the argument state points to a user-provided pointer that allows strtok_r() to maintain state between calls which scan the same string. the application shall ensure that the pointer pointed to by state is unique for each string (s) being processed concurrently by strtok_r() calls. the application need not initialize the pointer pointed to by state to any particular value. and the implementation shall not update the pointer pointed to by state to point (directly or indirectly) to resources โ other than within the string s โ that need to be freed or released by the caller. ๐ง
โญ RETURN VALUE โญ
upon successful completion, strtok() shall return a pointer to the first byte of a token. otherwise, if there is no token, strtok() shall return a null pointer.
the strtok_r() function shall return a pointer to the token found, or a null pointer when no token is found.
๐ธ ERRORS ๐ธ
no errors are defined. iconic, honestly. nothing to see here. ๐
the following sections are informative.
๐ฆ EXAMPLES ๐ฆ
๐ Searching for Word Separators
the following example searches for tokens separated by <space> characters.
#include <string.h>
...
char *token;
char line[] = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok(line, search);
/* Token will point to "TO". */
token = strtok(NULL, search);
๐งบ Find First two Fields in a Buffer
the following example uses strtok() to find two character strings (a key and data associated with that key) separated by any combination of <space>, <tab>, or <newline> characters at the start of the array of characters pointed to by buffer.
#include <string.h>
...
char *buffer;
...
struct element {
char *key;
char *data;
} e;
...
// Load the buffer...
...
// Get the key and its data...
e.key = strtok(buffer, " \t\n");
e.data = strtok(NULL, " \t\n");
// Process the rest of the contents of the buffer...
...
๐ APPLICATION USAGE ๐
quick PSA bestie: note that if sep is the empty string, strtok() and strtok_r() return a pointer to the remainder of the string being tokenized. ๐คฏ
the strtok_r() function is thread-safe and stores its state in a user-supplied buffer instead of possibly using a static data area that may be overwritten by an unrelated call from another thread. that's the whole glow-up. โจ
๐ง RATIONALE ๐ง
the strtok() function searches for a separator string within a larger string. it returns a pointer to the last substring between separator strings. this function uses static storage to keep track of the current string position between calls. the new function, strtok_r(), takes an additional argument, state, to keep track of the current position in the string. ๐ซ
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. the future is a mystery, but this section is not. ๐
๐ SEE ALSO ๐
the Base Definitions volume of POSIX.1โ2017, <string.h>
๐ COPYRIGHT ๐
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! ๐ this one came from IEEE/The Open Group, 2017 โ the STRTOK(3POSIX) vibe check, signed, sealed, delivered. go forth and tokenize responsibly, and remember: strtok_r() is your thread-safe bestie, strtok() is your chaotic one. ๐โจ๐ฆ
๐ฆ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 ยท Other
- debian trixie: manpages-posix-dev 2017a-2 ยท Other
- ubuntu jammy: manpages-posix-dev 2017a-2 ยท Other
- ubuntu noble: manpages-posix-dev 2017a-2 ยท Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms