rglob.c 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* ----- rglob.c ----- */
  2. typedef struct rglob_entry {
  3. char type;
  4. char* full_path;
  5. char* file_name;
  6. // char* dir_name;
  7. } rglob_entry;
  8. typedef struct rglob {
  9. char* pattern;
  10. int len;
  11. int alloc;
  12. rglob_entry* entries;
  13. } rglob;
  14. int rglob_fn(char* full_path, char* file_name, unsigned char type, void* _results) {
  15. rglob* res = (rglob*)_results;
  16. if(0 == fnmatch(res->pattern, file_name, 0)) {
  17. check_alloc(res);
  18. res->entries[res->len].type = type;
  19. res->entries[res->len].full_path = strcache(full_path);
  20. res->entries[res->len].file_name = strcache(file_name);
  21. res->len++;
  22. }
  23. return 0;
  24. }
  25. void recursive_glob(char* base_path, char* pattern, int flags, rglob* results) {
  26. // to pass into recurse_dirs()
  27. results->pattern = pattern;
  28. results->len = 0;
  29. results->alloc = 32;
  30. results->entries = malloc(sizeof(*results->entries) * results->alloc);
  31. recurse_dirs(base_path, rglob_fn, results, -1, flags);
  32. }
  33. /* -END- rglob.c ----- */