chdir_tc.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <sys/stat.h>
  6. #define NEW_CURR_DIR "./chdir_test"
  7. static int chdir_entry(void)
  8. {
  9. __attribute__((unused)) long path_max;
  10. size_t size;
  11. char *buf;
  12. char *ptr;
  13. __attribute__((unused)) char *old_dir;
  14. size = 1024;
  15. for (buf = ptr = NULL; ptr == NULL; size *= 2)
  16. {
  17. if ((buf = realloc(buf, size)) == NULL)
  18. {
  19. printf("error\n");
  20. }
  21. ptr = getcwd(buf, size);
  22. if (ptr == NULL)
  23. {
  24. printf("error\n");
  25. }
  26. else
  27. {
  28. printf("ptr %s\n",ptr);
  29. printf("old curr dir is %s\n", ptr);
  30. }
  31. mkdir(NEW_CURR_DIR, 0x777);
  32. chdir(NEW_CURR_DIR);
  33. ptr = getcwd(buf, size);
  34. if (ptr == NULL)
  35. {
  36. printf("error\n");
  37. }
  38. else
  39. {
  40. printf("new curr dir is %s\n", ptr);
  41. }
  42. }
  43. free(buf);
  44. return 0;
  45. }
  46. #include <utest.h>
  47. static void test_chdir(void)
  48. {
  49. uassert_int_equal(chdir_entry(), 0);
  50. }
  51. static void testcase(void)
  52. {
  53. UTEST_UNIT_RUN(test_chdir);
  54. }
  55. UTEST_TC_EXPORT(testcase, "posix.unistd_h.chdir_tc.c", RT_NULL, RT_NULL, 10);