Back to index

glibc  2.9
strsep.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 93, 96, 97, 98, 99, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <string.h>
00020 
00021 #undef __strsep
00022 #undef strsep
00023 
00024 char *
00025 __strsep (char **stringp, const char *delim)
00026 {
00027   char *begin, *end;
00028 
00029   begin = *stringp;
00030   if (begin == NULL)
00031     return NULL;
00032 
00033   /* A frequent case is when the delimiter string contains only one
00034      character.  Here we don't need to call the expensive `strpbrk'
00035      function and instead work using `strchr'.  */
00036   if (delim[0] == '\0' || delim[1] == '\0')
00037     {
00038       char ch = delim[0];
00039 
00040       if (ch == '\0')
00041        end = NULL;
00042       else
00043        {
00044          if (*begin == ch)
00045            end = begin;
00046          else if (*begin == '\0')
00047            end = NULL;
00048          else
00049            end = strchr (begin + 1, ch);
00050        }
00051     }
00052   else
00053     /* Find the end of the token.  */
00054     end = strpbrk (begin, delim);
00055 
00056   if (end)
00057     {
00058       /* Terminate the token and set *STRINGP past NUL character.  */
00059       *end++ = '\0';
00060       *stringp = end;
00061     }
00062   else
00063     /* No more delimiters; this is the last token.  */
00064     *stringp = NULL;
00065 
00066   return begin;
00067 }
00068 weak_alias (__strsep, strsep)
00069 strong_alias (__strsep, __strsep_g)
00070 libc_hidden_def (__strsep_g)