Git init
[external/curl.git] / lib / strtok.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifndef HAVE_STRTOK_R
26 #include <stddef.h>
27 #include <string.h>
28
29 #include "strtok.h"
30
31 char *
32 Curl_strtok_r(char *ptr, const char *sep, char **end)
33 {
34   if(!ptr)
35     /* we got NULL input so then we get our last position instead */
36     ptr = *end;
37
38   /* pass all letters that are including in the separator string */
39   while(*ptr && strchr(sep, *ptr))
40     ++ptr;
41
42   if(*ptr) {
43     /* so this is where the next piece of string starts */
44     char *start = ptr;
45
46     /* set the end pointer to the first byte after the start */
47     *end = start + 1;
48
49     /* scan through the string to find where it ends, it ends on a
50        null byte or a character that exists in the separator string */
51     while(**end && !strchr(sep, **end))
52       ++*end;
53
54     if(**end) {
55       /* the end is not a null byte */
56       **end = '\0';  /* zero terminate it! */
57       ++*end;        /* advance the last pointer to beyond the null byte */
58     }
59
60     return start; /* return the position where the string starts */
61   }
62
63   /* we ended up on a null byte, there are no more strings to find! */
64   return NULL;
65 }
66
67 #endif /* this was only compiled if strtok_r wasn't present */