Blame stdlib/getsubopt.c

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