chdir_tc.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. printf("ptr %s\n",ptr);
  23. if (ptr == NULL)
  24. {
  25. printf("error\n");
  26. }
  27. printf("old curr dir is %s\n", ptr);
  28. mkdir(NEW_CURR_DIR, 0x777);
  29. chdir(NEW_CURR_DIR);
  30. ptr = getcwd(buf, size);
  31. if (ptr == NULL)
  32. {
  33. printf("error\n");
  34. }
  35. printf("new curr dir is %s\n", ptr);
  36. }
  37. free(buf);
  38. return 0;
  39. }
  40. #include <utest.h>
  41. static void test_chdir(void)
  42. {
  43. uassert_int_equal(chdir_entry(), 0);
  44. }
  45. static void testcase(void)
  46. {
  47. UTEST_UNIT_RUN(test_chdir);
  48. }
  49. UTEST_TC_EXPORT(testcase, "posix.unistd_h.chdir_tc.c", RT_NULL, RT_NULL, 10);