Blame gl/strtok_r.c

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