tst-nftw.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #define _XOPEN_SOURCE 500
  2. #define _GNU_SOURCE
  3. #include <ftw.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. int result = 0;
  10. static int process_one_entry(const char *fpath, const struct stat *sb,
  11. int typeflag, struct FTW *ftwbuf)
  12. {
  13. struct stat buf;
  14. const char *rel_path = fpath+ftwbuf->base;
  15. printf("Processing %s in working dir %s\n",
  16. rel_path, get_current_dir_name());
  17. if (stat(rel_path, &buf) < 0) {
  18. perror("Oops...relative path does not exist in current directory");
  19. result = 1;
  20. }
  21. }
  22. static int
  23. do_test(void)
  24. {
  25. char *path = "/tmp/stest_dir";
  26. char *subpath = "/tmp/stest_dir/d1";
  27. char *filepath = "/tmp/stest_dir/f1";
  28. char *filesubpath = "/tmp/stest_dir/d1/f2";
  29. if ((mkdir(path, 0700)) < 0)
  30. perror("Creating path");
  31. if ((mkdir(subpath, 0700)) < 0)
  32. perror("Creating subpath");
  33. if ((open(filepath, O_CREAT)) < 0)
  34. perror("Opening filepath");
  35. if ((open(filesubpath, O_CREAT)) < 0)
  36. perror("Opening filesubpath");
  37. if (nftw(path, process_one_entry, 100, (FTW_CHDIR|FTW_DEPTH|FTW_PHYS)) < 0)
  38. perror("ntfw");
  39. unlink(filesubpath);
  40. unlink(filepath);
  41. rmdir(subpath);
  42. rmdir(path);
  43. return result;
  44. }
  45. #define TIMEOUT 5
  46. #define TEST_FUNCTION do_test ()
  47. #include "../test-skeleton.c"