chdir.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #define MAX_STRING 4096
  22. // CONSTANT MAX_STRING 4096
  23. char* get_current_dir_name();
  24. #define FALSE 0
  25. // CONSTANT FALSE 0
  26. #define TRUE 1
  27. // CONSTANT TRUE 1
  28. int match(char* a, char* b)
  29. {
  30. int i = -1;
  31. do
  32. {
  33. i = i + 1;
  34. if(a[i] != b[i])
  35. {
  36. return FALSE;
  37. }
  38. } while((0 != a[i]) && (0 !=b[i]));
  39. return TRUE;
  40. }
  41. char* copy_string(char* target, char* source)
  42. {
  43. while(0 != source[0])
  44. {
  45. target[0] = source[0];
  46. target = target + 1;
  47. source = source + 1;
  48. }
  49. return target;
  50. }
  51. char* prepend_string(char* add, char* base)
  52. {
  53. char* ret = calloc(MAX_STRING, sizeof(char));
  54. if(NULL == ret)
  55. {
  56. fputs("calloc failed in prepend_string\n", stderr);
  57. exit(EXIT_FAILURE);
  58. }
  59. copy_string(copy_string(ret, add), base);
  60. return ret;
  61. }
  62. int main()
  63. {
  64. char* current_path = calloc(MAX_STRING, sizeof(char));
  65. getcwd(current_path, MAX_STRING);
  66. char* base_path = calloc(MAX_STRING, sizeof(char));
  67. copy_string(base_path, current_path);
  68. /* Test they all evaluate to the same */
  69. char* current_path_getwd = calloc(MAX_STRING, sizeof(char));
  70. getwd(current_path_getwd);
  71. if(!match(current_path, current_path_getwd))
  72. {
  73. return 1;
  74. }
  75. char* current_path_dir_name = calloc(MAX_STRING, sizeof(char));
  76. current_path_dir_name = get_current_dir_name();
  77. if(!match(current_path, current_path_dir_name))
  78. {
  79. return 2;
  80. }
  81. /* Test chdir works */
  82. int chdir_rc = chdir(prepend_string(base_path, "/test/test0021"));
  83. if(chdir_rc != 0)
  84. {
  85. return 3;
  86. }
  87. getcwd(current_path, MAX_STRING);
  88. if(!match(current_path, prepend_string(base_path, "/test/test0021")))
  89. {
  90. return 4;
  91. }
  92. chdir(prepend_string(current_path, "/../.."));
  93. getcwd(current_path, MAX_STRING);
  94. /* Test fchdir works */
  95. FILE* fchdir_fd = open(prepend_string(base_path, "/test/test0021"), 0, 0);
  96. int fchdir_rc = fchdir(fchdir_fd);
  97. if(fchdir_rc != 0)
  98. {
  99. return 5;
  100. }
  101. getcwd(current_path, MAX_STRING);
  102. if(!match(current_path, prepend_string(base_path, "/test/test0021")))
  103. {
  104. return 6;
  105. }
  106. chdir(prepend_string(current_path, "/../.."));
  107. /* All tests passed */
  108. return 0;
  109. }