logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

strtok.3p (6387B)


  1. '\" et
  2. .TH STRTOK "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. strtok,
  12. strtok_r
  13. \(em split string into tokens
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <string.h>
  18. .P
  19. char *strtok(char *restrict \fIs\fP, const char *restrict \fIsep\fP);
  20. char *strtok_r(char *restrict \fIs\fP, const char *restrict \fIsep\fP,
  21. char **restrict \fIstate\fP);
  22. .fi
  23. .SH DESCRIPTION
  24. For
  25. \fIstrtok\fR():
  26. The functionality described on this reference page is aligned with the
  27. ISO\ C standard. Any conflict between the requirements described here and the
  28. ISO\ C standard is unintentional. This volume of POSIX.1\(hy2017 defers to the ISO\ C standard.
  29. .P
  30. A sequence of calls to
  31. \fIstrtok\fR()
  32. breaks the string pointed to by
  33. .IR s
  34. into a sequence of tokens, each of which is delimited by a byte from
  35. the string pointed to by
  36. .IR sep .
  37. The first call in the sequence has
  38. .IR s
  39. as its first argument, and is followed by calls with a null pointer as
  40. their first argument. The separator string pointed to by
  41. .IR sep
  42. may be different from call to call.
  43. .P
  44. The first call in the sequence searches the string pointed to by
  45. .IR s
  46. for the first byte that is
  47. .IR not
  48. contained in the current separator string pointed to by
  49. .IR sep .
  50. If no such byte is found, then there are no tokens in the string
  51. pointed to by
  52. .IR s
  53. and
  54. \fIstrtok\fR()
  55. shall return a null pointer. If such a byte is found, it is the
  56. start of the first token.
  57. .P
  58. The
  59. \fIstrtok\fR()
  60. function then searches from there for a byte that
  61. .IR is
  62. contained in the current separator string. If no such byte is found,
  63. the current token extends to the end of the string pointed to by
  64. .IR s ,
  65. and subsequent searches for a token shall return a null pointer. If
  66. such a byte is found, it is overwritten by a NUL character, which
  67. terminates the current token. The
  68. \fIstrtok\fR()
  69. function saves a pointer to the following byte, from which the next
  70. search for a token shall start.
  71. .P
  72. Each subsequent call, with a null pointer as the value of the first
  73. argument, starts searching from the saved pointer and behaves as
  74. described above.
  75. .P
  76. The implementation shall behave as if no function defined in this volume of POSIX.1\(hy2017
  77. calls
  78. \fIstrtok\fR().
  79. .P
  80. The
  81. \fIstrtok\fR()
  82. function need not be thread-safe.
  83. .P
  84. The
  85. \fIstrtok_r\fR()
  86. function shall be equivalent to
  87. \fIstrtok\fR(),
  88. except that
  89. \fIstrtok_r\fR()
  90. shall be thread-safe and the argument
  91. .IR state
  92. points to a user-provided pointer that allows
  93. \fIstrtok_r\fR()
  94. to maintain state between calls which scan the same string. The
  95. application shall ensure that the pointer pointed to by
  96. .IR state
  97. is unique for each string (\c
  98. .IR s )
  99. being processed concurrently by
  100. \fIstrtok_r\fR()
  101. calls. The application need not initialize the pointer pointed to by
  102. .IR state
  103. to any particular value. The implementation shall not update the
  104. pointer pointed to by
  105. .IR state
  106. to point (directly or indirectly) to resources, other than within
  107. the string
  108. .IR s ,
  109. that need to be freed or released by the caller.
  110. .SH "RETURN VALUE"
  111. Upon successful completion,
  112. \fIstrtok\fR()
  113. shall return a pointer to the first byte of a token. Otherwise,
  114. if there is no token,
  115. \fIstrtok\fR()
  116. shall return a null pointer.
  117. .P
  118. The
  119. \fIstrtok_r\fR()
  120. function shall return a pointer to the token found, or a null pointer
  121. when no token is found.
  122. .SH ERRORS
  123. No errors are defined.
  124. .LP
  125. .IR "The following sections are informative."
  126. .SH EXAMPLES
  127. .SS "Searching for Word Separators"
  128. .P
  129. The following example searches for tokens separated by
  130. <space>
  131. characters.
  132. .sp
  133. .RS 4
  134. .nf
  135. #include <string.h>
  136. \&...
  137. char *token;
  138. char line[] = "LINE TO BE SEPARATED";
  139. char *search = " ";
  140. .P
  141. /* Token will point to "LINE". */
  142. token = strtok(line, search);
  143. .P
  144. /* Token will point to "TO". */
  145. token = strtok(NULL, search);
  146. .fi
  147. .P
  148. .RE
  149. .SS "Find First two Fields in a Buffer"
  150. .P
  151. The following example uses
  152. \fIstrtok\fR()
  153. to find two character strings (a key and data associated with that key)
  154. separated by any combination of
  155. <space>,
  156. <tab>,
  157. or
  158. <newline>
  159. characters at the start of the array of characters pointed to by
  160. .IR buffer .
  161. .sp
  162. .RS 4
  163. .nf
  164. #include <string.h>
  165. \&...
  166. char *buffer;
  167. \&...
  168. struct element {
  169. char *key;
  170. char *data;
  171. } e;
  172. \&...
  173. // Load the buffer...
  174. \&...
  175. // Get the key and its data...
  176. e.key = strtok(buffer, " \et\en");
  177. e.data = strtok(NULL, " \et\en");
  178. // Process the rest of the contents of the buffer...
  179. \&...
  180. .fi
  181. .P
  182. .RE
  183. .SH "APPLICATION USAGE"
  184. Note that if
  185. .IR sep
  186. is the empty string,
  187. \fIstrtok\fR()
  188. and
  189. \fIstrtok_r\fR()
  190. return a pointer to the remainder of the string being tokenized.
  191. .P
  192. The
  193. \fIstrtok_r\fR()
  194. function is thread-safe and stores its state in a user-supplied buffer
  195. instead of possibly using a static data area that may be overwritten
  196. by an unrelated call from another thread.
  197. .SH RATIONALE
  198. The
  199. \fIstrtok\fR()
  200. function searches for a separator string within a larger string. It
  201. returns a pointer to the last substring between separator strings.
  202. This function uses static storage to keep track of the current string
  203. position between calls. The new function,
  204. \fIstrtok_r\fR(),
  205. takes an additional argument,
  206. .IR state ,
  207. to keep track of the current position in the string.
  208. .SH "FUTURE DIRECTIONS"
  209. None.
  210. .SH "SEE ALSO"
  211. The Base Definitions volume of POSIX.1\(hy2017,
  212. .IR "\fB<string.h>\fP"
  213. .\"
  214. .SH COPYRIGHT
  215. Portions of this text are reprinted and reproduced in electronic form
  216. from IEEE Std 1003.1-2017, Standard for Information Technology
  217. -- Portable Operating System Interface (POSIX), The Open Group Base
  218. Specifications Issue 7, 2018 Edition,
  219. Copyright (C) 2018 by the Institute of
  220. Electrical and Electronics Engineers, Inc and The Open Group.
  221. In the event of any discrepancy between this version and the original IEEE and
  222. The Open Group Standard, the original IEEE and The Open Group Standard
  223. is the referee document. The original Standard can be obtained online at
  224. http://www.opengroup.org/unix/online.html .
  225. .PP
  226. Any typographical or formatting errors that appear
  227. in this page are most likely
  228. to have been introduced during the conversion of the source files to
  229. man page format. To report such errors, see
  230. https://www.kernel.org/doc/man-pages/reporting_bugs.html .