busybox/libbb/compare_string_array.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   4 */
   5
   6#include "libbb.h"
   7
   8/* returns the array index of the string */
   9/* (index of first match is returned, or -1) */
  10int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key)
  11{
  12        int i;
  13
  14        for (i = 0; string_array[i] != 0; i++) {
  15                if (strcmp(string_array[i], key) == 0) {
  16                        return i;
  17                }
  18        }
  19        return -1;
  20}
  21
  22int FAST_FUNC index_in_strings(const char *strings, const char *key)
  23{
  24        int idx = 0;
  25
  26        while (*strings) {
  27                if (strcmp(strings, key) == 0) {
  28                        return idx;
  29                }
  30                strings += strlen(strings) + 1; /* skip NUL */
  31                idx++;
  32        }
  33        return -1;
  34}
  35
  36/* returns the array index of the string, even if it matches only a beginning */
  37/* (index of first match is returned, or -1) */
  38#ifdef UNUSED
  39int FAST_FUNC index_in_substr_array(const char *const string_array[], const char *key)
  40{
  41        int i;
  42        int len = strlen(key);
  43        if (len) {
  44                for (i = 0; string_array[i] != 0; i++) {
  45                        if (strncmp(string_array[i], key, len) == 0) {
  46                                return i;
  47                        }
  48                }
  49        }
  50        return -1;
  51}
  52#endif
  53
  54int FAST_FUNC index_in_substrings(const char *strings, const char *key)
  55{
  56        int matched_idx = -1;
  57        const int len = strlen(key);
  58
  59        if (len) {
  60                int idx = 0;
  61                while (*strings) {
  62                        if (strncmp(strings, key, len) == 0) {
  63                                if (strings[len] == '\0')
  64                                        return idx; /* exact match */
  65                                if (matched_idx >= 0)
  66                                        return -1; /* ambiguous match */
  67                                matched_idx = idx;
  68                        }
  69                        strings += strlen(strings) + 1; /* skip NUL */
  70                        idx++;
  71                }
  72        }
  73        return matched_idx;
  74}
  75
  76const char* FAST_FUNC nth_string(const char *strings, int n)
  77{
  78        while (n) {
  79                n--;
  80                strings += strlen(strings) + 1;
  81        }
  82        return strings;
  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
  96