scandir.c 765 B

12345678910111213141516171819202122232425262728293031
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "test_helpers.h"
  6. int filter(const struct dirent* dirent) {
  7. return strstr(dirent->d_name, "3") == NULL;
  8. }
  9. int main(void) {
  10. struct dirent** array;
  11. int len = scandir("example_dir/", &array, filter, alphasort);
  12. ERROR_IF(scandir, len, == -1);
  13. UNEXP_IF(scandir, len, < 0);
  14. for(int i = 0; i < len; i += 1) {
  15. // TODO: Redox does not yet provide . or .. - so filter them out
  16. // in order to make output match on all systems
  17. if (
  18. strcmp(array[i]->d_name, ".") != 0 &&
  19. strcmp(array[i]->d_name, "..") != 0
  20. ) {
  21. puts(array[i]->d_name);
  22. }
  23. free(array[i]);
  24. }
  25. free(array);
  26. }