find_list_entry.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2002 by Glenn McGrath
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. #include <fnmatch.h>
  8. #include "libbb.h"
  9. #include "bb_archive.h"
  10. /* Find a string in a shell pattern list */
  11. const llist_t* FAST_FUNC find_list_entry(const llist_t *list, const char *filename)
  12. {
  13. while (list) {
  14. if (fnmatch(list->data, filename, 0) == 0) {
  15. return list;
  16. }
  17. list = list->link;
  18. }
  19. return NULL;
  20. }
  21. /* Same, but compares only path components present in pattern
  22. * (extra trailing path components in filename are assumed to match)
  23. */
  24. const llist_t* FAST_FUNC find_list_entry2(const llist_t *list, const char *filename)
  25. {
  26. char buf[PATH_MAX];
  27. int pattern_slash_cnt;
  28. const char *c;
  29. char *d;
  30. while (list) {
  31. c = list->data;
  32. pattern_slash_cnt = 0;
  33. while (*c)
  34. if (*c++ == '/') pattern_slash_cnt++;
  35. c = filename;
  36. d = buf;
  37. /* paranoia is better than buffer overflows */
  38. while (*c && d != buf + sizeof(buf)-1) {
  39. if (*c == '/' && --pattern_slash_cnt < 0)
  40. break;
  41. *d++ = *c++;
  42. }
  43. *d = '\0';
  44. if (fnmatch(list->data, buf, 0) == 0) {
  45. return list;
  46. }
  47. list = list->link;
  48. }
  49. return NULL;
  50. }