/*
 * 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
 */

#include <string.h>

#ifdef __MINGW32__
# ifndef strtok_r

char* strtok_r(char *str, const char *delim, char **nextp)
{
    char *ret;

    if (str == NULL)
        str = *nextp;

    str += strspn(str, delim);

    if (*str == '\0')
    {
        *nextp = str;
        return NULL;
    }

    ret = str;
    str += strcspn(str, delim);

    if (*str)
        *str++ = '\0';

    *nextp = str;

    return ret;
}

# endif /* !strtok_r */
#endif /* __MINGW32__ */

/*
 * 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
 */

#include <string.h>

#ifdef __MINGW32__
# ifndef strtok_r

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;
}

# endif /* !strtok_r */
#endif /* __MINGW32__ */

--- 
+++ 
@@ -24,7 +24,10 @@
     str += strspn(str, delim);
 
     if (*str == '\0')
+    {
+        *nextp = str;
         return NULL;
+    }
 
     ret = str;
     str += strcspn(str, delim);
Fix possible invalid write on Windows-specific `strtok_r` stub