Blame gl/strtok_r.c

Packit Service 4684c1
/* Reentrant string tokenizer.  Generic version.
Packit Service 4684c1
   Copyright (C) 1991, 1996-1999, 2001, 2004, 2007, 2009-2020 Free Software
Packit Service 4684c1
   Foundation, Inc.
Packit Service 4684c1
   This file is part of the GNU C Library.
Packit Service 4684c1
Packit Service 4684c1
   This program is free software: you can redistribute it and/or modify
Packit Service 4684c1
   it under the terms of the GNU Lesser General Public License as published by
Packit Service 4684c1
   the Free Software Foundation; either version 2.1 of the License, or
Packit Service 4684c1
   (at your option) any later version.
Packit Service 4684c1
Packit Service 4684c1
   This program is distributed in the hope that it will be useful,
Packit Service 4684c1
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 4684c1
   GNU Lesser General Public License for more details.
Packit Service 4684c1
Packit Service 4684c1
   You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 4684c1
Packit Service 4684c1
#ifdef HAVE_CONFIG_H
Packit Service 4684c1
# include <config.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#include <string.h>
Packit Service 4684c1
Packit Service 4684c1
#ifdef _LIBC
Packit Service 4684c1
# undef strtok_r
Packit Service 4684c1
# undef __strtok_r
Packit Service 4684c1
#else
Packit Service 4684c1
# define __strtok_r strtok_r
Packit Service 4684c1
# define __rawmemchr strchr
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
/* Parse S into tokens separated by characters in DELIM.
Packit Service 4684c1
   If S is NULL, the saved pointer in SAVE_PTR is used as
Packit Service 4684c1
   the next starting point.  For example:
Packit Service 4684c1
        char s[] = "-abc-=-def";
Packit Service 4684c1
        char *sp;
Packit Service 4684c1
        x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
Packit Service 4684c1
        x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
Packit Service 4684c1
        x = strtok_r(NULL, "=", &sp);   // x = NULL
Packit Service 4684c1
                // s = "abc\0-def\0"
Packit Service 4684c1
*/
Packit Service 4684c1
char *
Packit Service 4684c1
__strtok_r (char *s, const char *delim, char **save_ptr)
Packit Service 4684c1
{
Packit Service 4684c1
  char *token;
Packit Service 4684c1
Packit Service 4684c1
  if (s == NULL)
Packit Service 4684c1
    s = *save_ptr;
Packit Service 4684c1
Packit Service 4684c1
  /* Scan leading delimiters.  */
Packit Service 4684c1
  s += strspn (s, delim);
Packit Service 4684c1
  if (*s == '\0')
Packit Service 4684c1
    {
Packit Service 4684c1
      *save_ptr = s;
Packit Service 4684c1
      return NULL;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
  /* Find the end of the token.  */
Packit Service 4684c1
  token = s;
Packit Service 4684c1
  s = strpbrk (token, delim);
Packit Service 4684c1
  if (s == NULL)
Packit Service 4684c1
    /* This token finishes the string.  */
Packit Service 4684c1
    *save_ptr = __rawmemchr (token, '\0');
Packit Service 4684c1
  else
Packit Service 4684c1
    {
Packit Service 4684c1
      /* Terminate the token and make *SAVE_PTR point past it.  */
Packit Service 4684c1
      *s = '\0';
Packit Service 4684c1
      *save_ptr = s + 1;
Packit Service 4684c1
    }
Packit Service 4684c1
  return token;
Packit Service 4684c1
}
Packit Service 4684c1
#ifdef weak_alias
Packit Service 4684c1
libc_hidden_def (__strtok_r)
Packit Service 4684c1
weak_alias (__strtok_r, strtok_r)
Packit Service 4684c1
#endif