aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/compare_string_array.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
index 43c59e8e0..599449fe0 100644
--- a/libbb/compare_string_array.c
+++ b/libbb/compare_string_array.c
@@ -53,19 +53,24 @@ int FAST_FUNC index_in_substr_array(const char *const string_array[], const char
53 53
54int FAST_FUNC index_in_substrings(const char *strings, const char *key) 54int FAST_FUNC index_in_substrings(const char *strings, const char *key)
55{ 55{
56 int len = strlen(key); 56 int matched_idx = -1;
57 const int len = strlen(key);
57 58
58 if (len) { 59 if (len) {
59 int idx = 0; 60 int idx = 0;
60 while (*strings) { 61 while (*strings) {
61 if (strncmp(strings, key, len) == 0) { 62 if (strncmp(strings, key, len) == 0) {
62 return idx; 63 if (strings[len] == '\0')
64 return idx; /* exact match */
65 if (matched_idx >= 0)
66 return -1; /* ambiguous match */
67 matched_idx = idx;
63 } 68 }
64 strings += strlen(strings) + 1; /* skip NUL */ 69 strings += strlen(strings) + 1; /* skip NUL */
65 idx++; 70 idx++;
66 } 71 }
67 } 72 }
68 return -1; 73 return matched_idx;
69} 74}
70 75
71const char* FAST_FUNC nth_string(const char *strings, int n) 76const char* FAST_FUNC nth_string(const char *strings, int n)
@@ -76,3 +81,15 @@ const char* FAST_FUNC nth_string(const char *strings, int n)
76 } 81 }
77 return strings; 82 return strings;
78} 83}
84
85#ifdef UNUSED_SO_FAR /* only brctl.c needs it yet */
86/* Returns 0 for no, 1 for yes or a negative value on error. */
87smallint FAST_FUNC yesno(const char *str)
88{
89 static const char no_yes[] ALIGN1 =
90 "0\0" "off\0" "no\0"
91 "1\0" "on\0" "yes\0";
92 int ret = index_in_substrings(no_yes, str);
93 return ret / 3;
94}
95#endif