Blame lib/strtok_r.c

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