Back to index

glibc  2.9
strtok.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1996,1997,1999,2000,2001,2007
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <string.h>
00021 
00022 
00023 static char *olds;
00024 
00025 #undef strtok
00026 
00027 /* Parse S into tokens separated by characters in DELIM.
00028    If S is NULL, the last string strtok() was called with is
00029    used.  For example:
00030        char s[] = "-abc-=-def";
00031        x = strtok(s, "-");         // x = "abc"
00032        x = strtok(NULL, "-=");            // x = "def"
00033        x = strtok(NULL, "=");             // x = NULL
00034               // s = "abc\0=-def\0"
00035 */
00036 char *
00037 strtok (s, delim)
00038      char *s;
00039      const char *delim;
00040 {
00041   char *token;
00042 
00043   if (s == NULL)
00044     s = olds;
00045 
00046   /* Scan leading delimiters.  */
00047   s += strspn (s, delim);
00048   if (*s == '\0')
00049     {
00050       olds = s;
00051       return NULL;
00052     }
00053 
00054   /* Find the end of the token.  */
00055   token = s;
00056   s = strpbrk (token, delim);
00057   if (s == NULL)
00058     /* This token finishes the string.  */
00059     olds = __rawmemchr (token, '\0');
00060   else
00061     {
00062       /* Terminate the token and make OLDS point past it.  */
00063       *s = '\0';
00064       olds = s + 1;
00065     }
00066   return token;
00067 }