summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-01-03 01:01:11 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-01-03 01:02:25 +0100
commite3eb9612af28762c4efdfcdf7cd493e24335d0f4 (patch)
tree8e71819c5dd5e78593917b58a28eef00cb3643fa
parentcbd6cab28b6b3d03e9625e9d429f8acaa34aa8a5 (diff)
Add implementation for strtok_r
-rw-r--r--libpsx/include/string.h1
-rw-r--r--libpsx/src/libc/string.c46
2 files changed, 46 insertions, 1 deletions
diff --git a/libpsx/include/string.h b/libpsx/include/string.h
index 2c232fa..a5dd114 100644
--- a/libpsx/include/string.h
+++ b/libpsx/include/string.h
@@ -37,6 +37,7 @@ int strspn(const char *s , const char *charset);
int strcspn(const char *s , const char *charset);
char *strsep(char **stringp, const char *delim);
char *strtok(char *str, const char *sep);
+char* strtok_r(char *str, const char *delim, char **nextp);
char *strstr(const char *big , const char *little);
char *strcasestr(const char *big, const char *little);
char *strlwr(char *string);
diff --git a/libpsx/src/libc/string.c b/libpsx/src/libc/string.c
index 2ab2f0b..791e1df 100644
--- a/libpsx/src/libc/string.c
+++ b/libpsx/src/libc/string.c
@@ -3,7 +3,7 @@
*
* Part of the PSXSDK C library
*/
-
+#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -439,6 +439,50 @@ char *strsep(char **stringp, const char *delim)
return old;
}
+/*
+ * public domain strtok_r() by Charlie Gordon
+ *
+ * from comp.lang.c 9/14/2007
+ *
+ * http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
+ *
+ * (Declaration that it's public domain):
+ * http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
+ */
+
+char* strtok_r(
+ char *str,
+ const char *delim,
+ char **nextp)
+{
+ char *ret;
+
+ if (str == NULL)
+ {
+ str = *nextp;
+ }
+
+ str += strspn(str, delim);
+
+ if (*str == '\0')
+ {
+ return NULL;
+ }
+
+ ret = str;
+
+ str += strcspn(str, delim);
+
+ if (*str)
+ {
+ *str++ = '\0';
+ }
+
+ *nextp = str;
+
+ return ret;
+}
+
char *strtok(char *str, const char *sep)
{
int x, y;