compare_string_array.c 822 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  4. */
  5. #include "libbb.h"
  6. /* returns the array index of the string */
  7. /* (index of first match is returned, or -1) */
  8. int index_in_str_array(const char * const string_array[], const char *key)
  9. {
  10. int i;
  11. for (i = 0; string_array[i] != 0; i++) {
  12. if (strcmp(string_array[i], key) == 0) {
  13. return i;
  14. }
  15. }
  16. return -1;
  17. }
  18. /* returns the array index of the string, even if it matches only a beginning */
  19. /* (index of first match is returned, or -1) */
  20. int index_in_substr_array(const char * const string_array[], const char *key)
  21. {
  22. int i;
  23. int len = strlen(key);
  24. if (!len)
  25. return -1;
  26. for (i = 0; string_array[i] != 0; i++) {
  27. if (strncmp(string_array[i], key, len) == 0) {
  28. return i;
  29. }
  30. }
  31. return -1;
  32. }