Blame gl/strtok_r.c

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