Add thread-safe strtok_r() function.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9192 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -6,27 +6,38 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
static char *___strtok = NULL;
|
static char *___strtok = NULL;
|
||||||
|
|
||||||
char *
|
char *
|
||||||
strtok(char *s, char const *ct)
|
strtok_r(char *s, char const *ct, char **save_ptr)
|
||||||
{
|
{
|
||||||
char *sbegin, *send;
|
char *sbegin, *send;
|
||||||
|
|
||||||
sbegin = s ? s : ___strtok;
|
if (!s && !save_ptr)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
sbegin = s ? s : *save_ptr;
|
||||||
if (!sbegin) {
|
if (!sbegin) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
sbegin += strspn(sbegin,ct);
|
sbegin += strspn(sbegin,ct);
|
||||||
if (*sbegin == '\0') {
|
if (*sbegin == '\0') {
|
||||||
___strtok = NULL;
|
if (save_ptr)
|
||||||
|
*save_ptr = NULL;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
send = strpbrk(sbegin, ct);
|
send = strpbrk(sbegin, ct);
|
||||||
if (send && *send != '\0')
|
if (send && *send != '\0')
|
||||||
*send++ = '\0';
|
*send++ = '\0';
|
||||||
___strtok = send;
|
if (save_ptr)
|
||||||
|
*save_ptr = send;
|
||||||
|
|
||||||
return sbegin;
|
return sbegin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
strtok(char *s, char const *ct)
|
||||||
|
{
|
||||||
|
return strtok_r(s, ct, &___strtok);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user