find_list_entry.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 tarball for details.
  6. */
  7. #include <fnmatch.h>
  8. #include <stdlib.h>
  9. #include "unarchive.h"
  10. /* Find a string in a shell pattern list */
  11. const llist_t *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 *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 that 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. }