/*-----------------------------------------------------------------------*
 * filename - strtok.c
 *
 * function(s)
 *        strtok - searches one string for tokens, which are
 *                 separated by delimiters defined in a second string
 *-----------------------------------------------------------------------*/

/*
 *      C/C++ Run Time Library - Version 6.0
 *
 *      Copyright (c) 1987, 1993 by Borland International
 *      All Rights Reserved.
 *
 */

#ifdef _MT
#include <_thread.h>
#endif
#include <string.h>

/*---------------------------------------------------------------------*

Name            strtok - searches one string for tokens, which are
                         separated by delimiters defined in a second string

Usage           char *strtok(char *str1, const char *str2);

Prototype in    string.h

Description     strtok considers the string str1 to consist of a sequence of
                zero or more text tokens, separated by spans of one or more
                characters from the separator string str2.

                The first call to strtok returns a pointer to the first
                character of the first token in str1 and writes a null character
                into str1 immediately following the returned token. Subsequent
                calls with NULL for the first argument will work through the
                string str1 in this way until no tokens remain.

                The separator string, str2, may be different from call to
                call.

Return value    pointer to the scanned token.  When no tokens remain in str1,
                strtok returns a NULL pointer.

*---------------------------------------------------------------------*/

char * _RTLENTRYF _EXPFUNC strtok(char *s1, const char *s2)
{
    register const char *sp;
    char *tok;
    char **save;
#ifndef _MT
    static char *token;
#endif

    /* Get address of saved token pointer
     */
#ifdef _MT
    save = &_thread_data()->thread_token;
#else
    save = &token;
#endif

    if (s1 == NULL)
        s1 = *save;

    /* First skip separators
     */
    while (*s1)
    {
        for (sp = s2; *sp; sp++)
            if (*sp == *s1)
                break;
        if (*sp == 0)
            break;
        s1++;
    }
    if (*s1 == 0)
    {
        *save = s1;
        return (0);
    }
    tok = s1;
    while (*s1)
    {
        for (sp = s2; *sp; sp++)
            if (*sp == *s1)
            {
                *s1++ = 0;
                *save = s1;
                return (tok);
            }
        s1++;
    }
    *save = s1;
    return (tok);
}
