Blame stdlib/getsubopt.c

Packit 6c4009
/* Parse comma separate list into words.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#if !_LIBC
Packit 6c4009
/* This code is written for inclusion in gnu-libc, and uses names in
Packit 6c4009
   the namespace reserved for libc.  If we're compiling in gnulib,
Packit 6c4009
   define those names to be the normal ones instead.  */
Packit 6c4009
# include "strchrnul.h"
Packit 6c4009
# undef __strchrnul
Packit 6c4009
# define __strchrnul strchrnul
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Parse comma separated suboption from *OPTIONP and match against
Packit 6c4009
   strings in TOKENS.  If found return index and set *VALUEP to
Packit 6c4009
   optional value introduced by an equal sign.  If the suboption is
Packit 6c4009
   not part of TOKENS return in *VALUEP beginning of unknown
Packit 6c4009
   suboption.  On exit *OPTIONP is set to the beginning of the next
Packit 6c4009
   token or at the terminating NUL character.  */
Packit 6c4009
int
Packit 6c4009
getsubopt (char **optionp, char *const *tokens, char **valuep)
Packit 6c4009
{
Packit 6c4009
  char *endp, *vstart;
Packit 6c4009
  int cnt;
Packit 6c4009
Packit 6c4009
  if (**optionp == '\0')
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Find end of next token.  */
Packit 6c4009
  endp = __strchrnul (*optionp, ',');
Packit 6c4009
Packit 6c4009
  /* Find start of value.  */
Packit 6c4009
  vstart = memchr (*optionp, '=', endp - *optionp);
Packit 6c4009
  if (vstart == NULL)
Packit 6c4009
    vstart = endp;
Packit 6c4009
Packit 6c4009
  /* Try to match the characters between *OPTIONP and VSTART against
Packit 6c4009
     one of the TOKENS.  */
Packit 6c4009
  for (cnt = 0; tokens[cnt] != NULL; ++cnt)
Packit 6c4009
    if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0
Packit 6c4009
	&& tokens[cnt][vstart - *optionp] == '\0')
Packit 6c4009
      {
Packit 6c4009
	/* We found the current option in TOKENS.  */
Packit 6c4009
	*valuep = vstart != endp ? vstart + 1 : NULL;
Packit 6c4009
Packit 6c4009
	if (*endp != '\0')
Packit 6c4009
	  *endp++ = '\0';
Packit 6c4009
	*optionp = endp;
Packit 6c4009
Packit 6c4009
	return cnt;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* The current suboption does not match any option.  */
Packit 6c4009
  *valuep = *optionp;
Packit 6c4009
Packit 6c4009
  if (*endp != '\0')
Packit 6c4009
    *endp++ = '\0';
Packit 6c4009
  *optionp = endp;
Packit 6c4009
Packit 6c4009
  return -1;
Packit 6c4009
}