| 1 | /* |
| 2 | * Android c-library does not have getsubopt, |
| 3 | * so code lifted from uClibc |
| 4 | * http://git.uclibc.org/uClibc/tree/libc/unistd/getsubopt.c |
| 5 | */ |
| 6 | |
| 7 | /* Parse comma separate list into words. |
| 8 | Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc. |
| 9 | This file is part of the GNU C Library. |
| 10 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
| 11 | The GNU C Library is free software; you can redistribute it and/or |
| 12 | modify it under the terms of the GNU Lesser General Public |
| 13 | License as published by the Free Software Foundation; either |
| 14 | version 2.1 of the License, or (at your option) any later version. |
| 15 | The GNU C Library is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | Lesser General Public License for more details. |
| 19 | You should have received a copy of the GNU Lesser General Public |
| 20 | License along with the GNU C Library; if not, write to the Free |
| 21 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 22 | 02111-1307 USA. */ |
| 23 | |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | char *strchrnul(const char *s, int c) |
| 29 | { |
| 30 | char *result; |
| 31 | |
| 32 | result = strchr( s, c ); |
| 33 | |
| 34 | if( !result ) |
| 35 | { |
| 36 | result = (char *)s + strlen( s ); |
| 37 | } |
| 38 | |
| 39 | return( result ); |
| 40 | } |
| 41 | |
| 42 | /* Parse comma separated suboption from *OPTIONP and match against |
| 43 | strings in TOKENS. If found return index and set *VALUEP to |
| 44 | optional value introduced by an equal sign. If the suboption is |
| 45 | not part of TOKENS return in *VALUEP beginning of unknown |
| 46 | suboption. On exit *OPTIONP is set to the beginning of the next |
| 47 | token or at the terminating NUL character. */ |
| 48 | int |
| 49 | getsubopt (char **optionp, char *const *tokens, char **valuep) |
| 50 | { |
| 51 | char *endp, *vstart; |
| 52 | int cnt; |
| 53 | |
| 54 | if (**optionp == '\0') |
| 55 | return -1; |
| 56 | |
| 57 | /* Find end of next token. */ |
| 58 | endp = strchrnul (*optionp, ','); |
| 59 | |
| 60 | /* Find start of value. */ |
| 61 | vstart = memchr (*optionp, '=', endp - *optionp); |
| 62 | if (vstart == NULL) |
| 63 | vstart = endp; |
| 64 | |
| 65 | /* Try to match the characters between *OPTIONP and VSTART against |
| 66 | one of the TOKENS. */ |
| 67 | for (cnt = 0; tokens[cnt] != NULL; ++cnt) |
| 68 | if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0 |
| 69 | && tokens[cnt][vstart - *optionp] == '\0') |
| 70 | { |
| 71 | /* We found the current option in TOKENS. */ |
| 72 | *valuep = vstart != endp ? vstart + 1 : NULL; |
| 73 | |
| 74 | if (*endp != '\0') |
| 75 | *endp++ = '\0'; |
| 76 | *optionp = endp; |
| 77 | |
| 78 | return cnt; |
| 79 | } |
| 80 | |
| 81 | /* The current suboption does not match any option. */ |
| 82 | *valuep = *optionp; |
| 83 | |
| 84 | if (*endp != '\0') |
| 85 | *endp++ = '\0'; |
| 86 | *optionp = endp; |
| 87 | |
| 88 | return -1; |
| 89 | } |