e9c143e5887df731a2051668d7e47ede5979b6ec
[gnulib.git] / lib / strtok_r.c
1 /* Reentrant string tokenizer.  Generic version.
2    Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <string.h>
24
25 #undef strtok_r
26 #undef __strtok_r
27
28 #ifndef _LIBC
29 /* Get specification.  */
30 # include "strtok_r.h"
31 # define __strtok_r strtok_r
32 # define __rawmemchr strchr
33 #endif
34
35 /* Parse S into tokens separated by characters in DELIM.
36    If S is NULL, the saved pointer in SAVE_PTR is used as
37    the next starting point.  For example:
38         char s[] = "-abc-=-def";
39         char *sp;
40         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
41         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
42         x = strtok_r(NULL, "=", &sp);   // x = NULL
43                 // s = "abc\0-def\0"
44
45    For the POSIX documentation for this function, see:
46    http://www.opengroup.org/onlinepubs/009695399/functions/strtok.html
47
48    Caveat: It modifies the original string.
49    Caveat: These functions cannot be used on constant strings.
50    Caveat: The identity of the delimiting character is lost.
51    Caveat: It doesn't work with multibyte strings unless all of the delimiter
52            characters are ASCII characters < 0x30.
53
54    See also strsep().
55 */
56 char *
57 __strtok_r (char *s, const char *delim, char **save_ptr)
58 {
59   char *token;
60
61   if (s == NULL)
62     s = *save_ptr;
63
64   /* Scan leading delimiters.  */
65   s += strspn (s, delim);
66   if (*s == '\0')
67     {
68       *save_ptr = s;
69       return NULL;
70     }
71
72   /* Find the end of the token.  */
73   token = s;
74   s = strpbrk (token, delim);
75   if (s == NULL)
76     /* This token finishes the string.  */
77     *save_ptr = __rawmemchr (token, '\0');
78   else
79     {
80       /* Terminate the token and make *SAVE_PTR point past it.  */
81       *s = '\0';
82       *save_ptr = s + 1;
83     }
84   return token;
85 }
86 #ifdef weak_alias
87 libc_hidden_def (__strtok_r)
88 weak_alias (__strtok_r, strtok_r)
89 #endif