blob: 077a280a2e8d41d18d6ed96c5bd601624d0ec332 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathb9638752002-12-02 00:01:36 +00002/*
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathb9638752002-12-02 00:01:36 +00004 */
5
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +00006#include "libbb.h"
Glenn L McGrathb9638752002-12-02 00:01:36 +00007
Denis Vlasenko5af906e2006-11-05 18:05:09 +00008/* returns the array index of the string */
9/* (index of first match is returned, or -1) */
10int index_in_str_array(const char * const string_array[], const char *key)
Glenn L McGrathb9638752002-12-02 00:01:36 +000011{
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000012 int i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000013
14 for (i = 0; string_array[i] != 0; i++) {
15 if (strcmp(string_array[i], key) == 0) {
"Vladimir N. Oleynik"cc343442005-11-26 10:45:26 +000016 return i;
Glenn L McGrathb9638752002-12-02 00:01:36 +000017 }
18 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000019 return -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000020}
21
Denis Vlasenko5af906e2006-11-05 18:05:09 +000022/* returns the array index of the string, even if it matches only a beginning */
23/* (index of first match is returned, or -1) */
24int index_in_substr_array(const char * const string_array[], const char *key)
25{
26 int i;
27 int len = strlen(key);
Bernhard Reutner-Fischereceecea2007-03-30 14:43:27 +000028 if (len) {
29 for (i = 0; string_array[i] != 0; i++) {
30 if (strncmp(string_array[i], key, len) == 0) {
31 return i;
32 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +000033 }
34 }
35 return -1;
36}