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