Back to index

nagios-plugins  1.4.16
strsep.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2007, 2009, 2010 Free Software Foundation, Inc.
00002 
00003    Written by Yoann Vandoorselaere <yoann@prelude-ids.org>.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3, or (at your option)
00008    any later version.
00009 
00010    This program 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
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 /* Specification.  */
00024 #include <string.h>
00025 
00026 char *
00027 strsep (char **stringp, const char *delim)
00028 {
00029   char *start = *stringp;
00030   char *ptr;
00031 
00032   if (start == NULL)
00033     return NULL;
00034 
00035   /* Optimize the case of no delimiters.  */
00036   if (delim[0] == '\0')
00037     {
00038       *stringp = NULL;
00039       return start;
00040     }
00041 
00042   /* Optimize the case of one delimiter.  */
00043   if (delim[1] == '\0')
00044     ptr = strchr (start, delim[0]);
00045   else
00046     /* The general case.  */
00047     ptr = strpbrk (start, delim);
00048   if (ptr == NULL)
00049     {
00050       *stringp = NULL;
00051       return start;
00052     }
00053 
00054   *ptr = '\0';
00055   *stringp = ptr + 1;
00056 
00057   return start;
00058 }