tst-nftw.c 1.3 KB

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