Blame usbredirparser/strtok_r.c

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