1*56bb7041Schristos /* Reentrant string tokenizer.  Generic version.
2*56bb7041Schristos    Copyright (C) 1991, 1996-1999, 2001, 2004, 2007, 2009-2020 Free Software
3*56bb7041Schristos    Foundation, Inc.
4*56bb7041Schristos    This file is part of the GNU C Library.
5*56bb7041Schristos 
6*56bb7041Schristos    This program is free software: you can redistribute it and/or modify
7*56bb7041Schristos    it under the terms of the GNU General Public License as published by
8*56bb7041Schristos    the Free Software Foundation; either version 3 of the License, or
9*56bb7041Schristos    (at your option) any later version.
10*56bb7041Schristos 
11*56bb7041Schristos    This program is distributed in the hope that it will be useful,
12*56bb7041Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*56bb7041Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*56bb7041Schristos    GNU General Public License for more details.
15*56bb7041Schristos 
16*56bb7041Schristos    You should have received a copy of the GNU General Public License
17*56bb7041Schristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
18*56bb7041Schristos 
19*56bb7041Schristos #ifdef HAVE_CONFIG_H
20*56bb7041Schristos # include <config.h>
21*56bb7041Schristos #endif
22*56bb7041Schristos 
23*56bb7041Schristos #include <string.h>
24*56bb7041Schristos 
25*56bb7041Schristos #ifdef _LIBC
26*56bb7041Schristos # undef strtok_r
27*56bb7041Schristos # undef __strtok_r
28*56bb7041Schristos #else
29*56bb7041Schristos # define __strtok_r strtok_r
30*56bb7041Schristos # define __rawmemchr strchr
31*56bb7041Schristos #endif
32*56bb7041Schristos 
33*56bb7041Schristos /* Parse S into tokens separated by characters in DELIM.
34*56bb7041Schristos    If S is NULL, the saved pointer in SAVE_PTR is used as
35*56bb7041Schristos    the next starting point.  For example:
36*56bb7041Schristos         char s[] = "-abc-=-def";
37*56bb7041Schristos         char *sp;
38*56bb7041Schristos         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
39*56bb7041Schristos         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
40*56bb7041Schristos         x = strtok_r(NULL, "=", &sp);   // x = NULL
41*56bb7041Schristos                 // s = "abc\0-def\0"
42*56bb7041Schristos */
43*56bb7041Schristos char *
__strtok_r(char * s,const char * delim,char ** save_ptr)44*56bb7041Schristos __strtok_r (char *s, const char *delim, char **save_ptr)
45*56bb7041Schristos {
46*56bb7041Schristos   char *token;
47*56bb7041Schristos 
48*56bb7041Schristos   if (s == NULL)
49*56bb7041Schristos     s = *save_ptr;
50*56bb7041Schristos 
51*56bb7041Schristos   /* Scan leading delimiters.  */
52*56bb7041Schristos   s += strspn (s, delim);
53*56bb7041Schristos   if (*s == '\0')
54*56bb7041Schristos     {
55*56bb7041Schristos       *save_ptr = s;
56*56bb7041Schristos       return NULL;
57*56bb7041Schristos     }
58*56bb7041Schristos 
59*56bb7041Schristos   /* Find the end of the token.  */
60*56bb7041Schristos   token = s;
61*56bb7041Schristos   s = strpbrk (token, delim);
62*56bb7041Schristos   if (s == NULL)
63*56bb7041Schristos     /* This token finishes the string.  */
64*56bb7041Schristos     *save_ptr = __rawmemchr (token, '\0');
65*56bb7041Schristos   else
66*56bb7041Schristos     {
67*56bb7041Schristos       /* Terminate the token and make *SAVE_PTR point past it.  */
68*56bb7041Schristos       *s = '\0';
69*56bb7041Schristos       *save_ptr = s + 1;
70*56bb7041Schristos     }
71*56bb7041Schristos   return token;
72*56bb7041Schristos }
73*56bb7041Schristos #ifdef weak_alias
74*56bb7041Schristos libc_hidden_def (__strtok_r)
75*56bb7041Schristos weak_alias (__strtok_r, strtok_r)
76*56bb7041Schristos #endif
77