strtok_r: useful, trivial but was missing from the C library
authorAlan Cox <alan@linux.intel.com>
Sat, 7 Jul 2018 14:02:16 +0000 (15:02 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 7 Jul 2018 14:02:16 +0000 (15:02 +0100)
Library/include/string.h
Library/libs/Makefile.z80
Library/libs/strtok_r.c [new file with mode: 0644]

index 27ecf38..32747e0 100644 (file)
@@ -43,11 +43,12 @@ extern char *rindex(const char *__s, int __c);
 extern void bcopy(const void *__src, void *__dst, size_t __n);
 extern void bzero(void *__dst, size_t __n);
 
-/* Other common BSD functions */
+/* Other common string functions */
 extern char *strpbrk(const char *__s, const char *__accept);
 extern char *strsep(char **__stringp, const char *__delim);
 extern char *strstr(const char *__haystack, const char *__needle);
 extern char *strtok(char *__str, const char *__delim);
+extern char *strtok_r(char *__str, const char *__delim, char **__olds);
 extern size_t strcspn(const char *__s, const char *__reject);
 extern size_t strspn(const char *__s, const char *__accept);
 
index e2defda..7050632 100644 (file)
@@ -44,7 +44,7 @@ SRC_C += setlocale.c setvbuf.c settimeofday.c sgetl.c sleep.c sprintf.c
 SRC_C += sputl.c stat.c stdio0.c stime.c
 SRC_C += strcasecmp.c strcasestr.c strdup.c stricmp.c strlcpy.c strncasecmp.c
 SRC_C += strnlen.c strnicmp.c strsep.c strxfrm.c strcoll.c strsignal.c
-SRC_C += strtod.c strtol.c swab.c system.c telldir.c
+SRC_C += strtod.c strtok_r.c strtol.c swab.c system.c telldir.c
 SRC_C += tfind.c time.c tmpfile.c tmpnam.c tsearch.c ttyname.c
 SRC_C += tzset.c umount.c ungetc.c usleep.c utent.c utimes.c utsname.c
 SRC_C += vfprintf.c vprintf.c wait.c xitoa.c pathconf.c
diff --git a/Library/libs/strtok_r.c b/Library/libs/strtok_r.c
new file mode 100644 (file)
index 0000000..a6cc35c
--- /dev/null
@@ -0,0 +1,66 @@
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <string.h>
+
+
+/* Parse S into tokens separated by characters in DELIM.
+   If S is NULL, the last string strtok() was called with is
+   used.  For example:
+       char s[] = "-abc=-def";
+       x = strtok(s, "-");             // x = "abc"
+       x = strtok(NULL, "=-");         // x = "def"
+       x = strtok(NULL, "=");          // x = NULL
+               // s = "abc\0-def\0"
+*/
+char *strtok_r(char *s, const char *delim, char **olds)
+{
+  char *token;
+
+  if (s == 0)
+    {
+      if (*olds == 0)
+       {
+         return 0;
+       }
+      else
+       s = *olds;
+    }
+
+  /* Scan leading delimiters.  */
+  s += strspn(s, delim);
+  if (*s == '\0')
+    {
+      *olds = 0;
+      return 0;
+    }
+
+  /* Find the end of the token.  */
+  token = s;
+  s = strpbrk(token, delim);
+  if (s == 0)
+    /* This token finishes the string.  */
+    *olds = 0;
+  else
+    {
+      /* Terminate the token and make OLDS point past it.  */
+      *s = '\0';
+      *olds = s + 1;
+    }
+  return token;
+}