| "Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ | 
| Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 2 | /* | 
| Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 3 |  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 
| Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 4 |  */ | 
 | 5 |  | 
| Bernhard Reutner-Fischer | 421d9e5 | 2006-04-03 16:39:31 +0000 | [diff] [blame] | 6 | #include "libbb.h" | 
| Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 7 |  | 
| Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 8 | /* returns the array index of the string */ | 
 | 9 | /* (index of first match is returned, or -1) */ | 
 | 10 | int index_in_str_array(const char * const string_array[], const char *key) | 
| Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 11 | { | 
| "Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 12 | 	int i; | 
| Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 13 |  | 
 | 14 | 	for (i = 0; string_array[i] != 0; i++) { | 
 | 15 | 		if (strcmp(string_array[i], key) == 0) { | 
| "Vladimir N. Oleynik" | cc34344 | 2005-11-26 10:45:26 +0000 | [diff] [blame] | 16 | 			return i; | 
| Glenn L McGrath | b963875 | 2002-12-02 00:01:36 +0000 | [diff] [blame] | 17 | 		} | 
 | 18 | 	} | 
| Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 19 | 	return -1; | 
| Manuel Novoa III  | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | } | 
 | 21 |  | 
| Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 22 | /* returns the array index of the string, even if it matches only a beginning */ | 
 | 23 | /* (index of first match is returned, or -1) */ | 
 | 24 | int index_in_substr_array(const char * const string_array[], const char *key) | 
 | 25 | { | 
 | 26 | 	int i; | 
 | 27 | 	int len = strlen(key); | 
| Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame^] | 28 | 	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 Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 33 | 		} | 
 | 34 | 	} | 
 | 35 | 	return -1; | 
 | 36 | } |