Blame lib/strtok_r.c

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