blob: d15578ca3605200b3954c54b16fb8f42bd6ab2d1 [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);
28 if (!len)
29 return -1;
30
31 for (i = 0; string_array[i] != 0; i++) {
32 if (strncmp(string_array[i], key, len) == 0) {
33 return i;
34 }
35 }
36 return -1;
37}