Back to index

glibc  2.9
getsubopt.c
Go to the documentation of this file.
00001 /* Parse comma separate list into words.
00002    Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 #if !_LIBC
00025 /* This code is written for inclusion in gnu-libc, and uses names in
00026    the namespace reserved for libc.  If we're compiling in gnulib,
00027    define those names to be the normal ones instead.  */
00028 # include "strchrnul.h"
00029 # undef __strchrnul
00030 # define __strchrnul strchrnul
00031 #endif
00032 
00033 /* Parse comma separated suboption from *OPTIONP and match against
00034    strings in TOKENS.  If found return index and set *VALUEP to
00035    optional value introduced by an equal sign.  If the suboption is
00036    not part of TOKENS return in *VALUEP beginning of unknown
00037    suboption.  On exit *OPTIONP is set to the beginning of the next
00038    token or at the terminating NUL character.  */
00039 int
00040 getsubopt (char **optionp, char *const *tokens, char **valuep)
00041 {
00042   char *endp, *vstart;
00043   int cnt;
00044 
00045   if (**optionp == '\0')
00046     return -1;
00047 
00048   /* Find end of next token.  */
00049   endp = __strchrnul (*optionp, ',');
00050 
00051   /* Find start of value.  */
00052   vstart = memchr (*optionp, '=', endp - *optionp);
00053   if (vstart == NULL)
00054     vstart = endp;
00055 
00056   /* Try to match the characters between *OPTIONP and VSTART against
00057      one of the TOKENS.  */
00058   for (cnt = 0; tokens[cnt] != NULL; ++cnt)
00059     if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0
00060        && tokens[cnt][vstart - *optionp] == '\0')
00061       {
00062        /* We found the current option in TOKENS.  */
00063        *valuep = vstart != endp ? vstart + 1 : NULL;
00064 
00065        if (*endp != '\0')
00066          *endp++ = '\0';
00067        *optionp = endp;
00068 
00069        return cnt;
00070       }
00071 
00072   /* The current suboption does not match any option.  */
00073   *valuep = *optionp;
00074 
00075   if (*endp != '\0')
00076     *endp++ = '\0';
00077   *optionp = endp;
00078 
00079   return -1;
00080 }