diff --git a/src/sasl.c b/src/sasl.c index 90583ea..5ae5a7f 100644 --- a/src/sasl.c +++ b/src/sasl.c @@ -24,10 +24,17 @@ #include "sha1.h" #include "scram.h" #include "rand.h" +#include "util.h" -#ifdef _WIN32 -#define strtok_r strtok_s -#endif +/* strtok_s() has appeared in visual studio 2005. + Use own implementation for older versions. */ +#ifdef _MSC_VER +# if (_MSC_VER >= 1400) +# define strtok_r strtok_s +# else +# define strtok_r xmpp_strtok_r +# endif +#endif /* _MSC_VER */ /** generate authentication string for the SASL PLAIN mechanism */ diff --git a/src/util.c b/src/util.c index d1d63a4..049ba86 100644 --- a/src/util.c +++ b/src/util.c @@ -55,6 +55,28 @@ char *xmpp_strdup(const xmpp_ctx_t * const ctx, const char * const s) return copy; } +/** strtok_r(3) implementation. + * This function has appeared in POSIX.1-2001, but not in C standard. + * For example, visual studio older than 2005 doesn't provide strtok_r() + * nor strtok_s(). + */ +char *xmpp_strtok_r(char *s, const char *delim, char **saveptr) +{ + size_t len; + + s = s ? s : *saveptr; + len = strspn(s, delim); + s += len; + if (*s == '\0') + return NULL; + + len = strcspn(s, delim); + *saveptr = s[len] == '\0' ? &s[len] : &s[len + 1]; + s[len] = '\0'; + + return s; +} + /** Return an integer based time stamp. * This function uses gettimeofday or timeGetTime (on Win32 platforms) to * compute an integer based time stamp. This is used internally by the diff --git a/src/util.h b/src/util.h index 4bc00e1..1a7baf7 100644 --- a/src/util.h +++ b/src/util.h @@ -18,6 +18,9 @@ #include "ostypes.h" +/* string functions */ +char *xmpp_strtok_r(char *s, const char *delim, char **saveptr); + /* timing functions */ uint64_t time_stamp(void); uint64_t time_elapsed(uint64_t t1, uint64_t t2);