strtok_r.c 1.68 KB
Newer Older
Clement Chesnin's avatar
Clement Chesnin committed
1 2 3
/*****************************************************************************
 * strtok_r.c: POSIX strtok_r() replacement
 *****************************************************************************
4
 * Copyright © 1998-2009 VLC authors and VideoLAN
Clement Chesnin's avatar
Clement Chesnin committed
5
 *
6 7 8
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Clement Chesnin's avatar
Clement Chesnin committed
9 10 11 12
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
Clement Chesnin's avatar
Clement Chesnin committed
15
 *
16 17 18
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Clement Chesnin's avatar
Clement Chesnin committed
19 20 21 22 23 24 25 26 27 28
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>

char *strtok_r(char *s, const char *delim, char **save_ptr)
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
29
    char *token;
Clement Chesnin's avatar
Clement Chesnin committed
30

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
31 32
    if (s == NULL)
        s = *save_ptr;
Clement Chesnin's avatar
Clement Chesnin committed
33

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
34 35 36 37
    /* Scan leading delimiters. */
    s += strspn (s, delim);
    if (*s == '\0')
        return NULL;
Clement Chesnin's avatar
Clement Chesnin committed
38

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
39 40 41 42 43 44 45 46 47 48 49 50 51
    /* Find the end of the token. */
    token = s;
    s = strpbrk (token, delim);
    if (s == NULL)
        /* This token finishes the string. */
        *save_ptr = strchr (token, '\0');
    else
    {
        /* Terminate the token and make *SAVE_PTR point past it. */
        *s = '\0';
        *save_ptr = s + 1;
    }
    return token;
Clement Chesnin's avatar
Clement Chesnin committed
52
}