chdir.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. int match(char* a, char* b);
  24. char* get_current_dir_name();
  25. void file_print(char* s, FILE* f);
  26. char* copy_string(char* target, char* source)
  27. {
  28. while(0 != source[0])
  29. {
  30. target[0] = source[0];
  31. target = target + 1;
  32. source = source + 1;
  33. }
  34. return target;
  35. }
  36. char* prepend_string(char* add, char* base)
  37. {
  38. char* ret = calloc(MAX_STRING, sizeof(char));
  39. if(NULL == ret)
  40. {
  41. file_print("calloc failed in prepend_string\n", stderr);
  42. exit(EXIT_FAILURE);
  43. }
  44. copy_string(copy_string(ret, add), base);
  45. return ret;
  46. }
  47. int main()
  48. {
  49. char* current_path = calloc(MAX_STRING, sizeof(char));
  50. getcwd(current_path, MAX_STRING);
  51. char* base_path = calloc(MAX_STRING, sizeof(char));
  52. copy_string(base_path, current_path);
  53. /* Test they all evaluate to the same */
  54. char* current_path_getwd = calloc(MAX_STRING, sizeof(char));
  55. getwd(current_path_getwd);
  56. if(!match(current_path, current_path_getwd))
  57. {
  58. return 1;
  59. }
  60. char* current_path_dir_name = calloc(MAX_STRING, sizeof(char));
  61. current_path_dir_name = get_current_dir_name();
  62. if(!match(current_path, current_path_dir_name))
  63. {
  64. return 2;
  65. }
  66. /* Test chdir works */
  67. int chdir_rc = chdir(prepend_string(base_path, "/test/test0021"));
  68. if(chdir_rc != 0)
  69. {
  70. return 3;
  71. }
  72. getcwd(current_path, MAX_STRING);
  73. if(!match(current_path, prepend_string(base_path, "/test/test0021")))
  74. {
  75. return 4;
  76. }
  77. chdir(prepend_string(current_path, "/../.."));
  78. getcwd(current_path, MAX_STRING);
  79. /* Test fchdir works */
  80. FILE* fchdir_fd = open(prepend_string(base_path, "/test/test0021"), 0, 0);
  81. int fchdir_rc = fchdir(fchdir_fd);
  82. if(fchdir_rc != 0)
  83. {
  84. return 5;
  85. }
  86. getcwd(current_path, MAX_STRING);
  87. if(!match(current_path, prepend_string(base_path, "/test/test0021")))
  88. {
  89. return 6;
  90. }
  91. chdir(prepend_string(current_path, "/../.."));
  92. /* All tests passed */
  93. return 0;
  94. }