paths.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <grass/gis.h>
  5. /**
  6. * \brief Creates a new directory
  7. *
  8. * Creates a new directory with permissions 0777 (on Unix) or
  9. * default permissions(?) on Windows.
  10. *
  11. * \param path String containing path of directory to be created
  12. *
  13. * \return Return value from system mkdir() function
  14. **/
  15. int G_mkdir(const char *path)
  16. {
  17. #ifdef __MINGW32__
  18. return mkdir(path);
  19. #else
  20. return mkdir(path, 0777);
  21. #endif
  22. }
  23. /**
  24. * \brief Checks if a specified character is a valid directory
  25. * separator character on the host system
  26. *
  27. * \param c Character to check
  28. *
  29. * \return 1 if c is a directory separator character, 0 if not
  30. **/
  31. int G_is_dirsep(char c)
  32. {
  33. if (c == GRASS_DIRSEP || c == HOST_DIRSEP)
  34. return 1;
  35. else
  36. return 0;
  37. }
  38. /**
  39. * \brief Checks if a specified path looks like an absolute
  40. * path on the host system
  41. *
  42. * \param path String containing path to check
  43. *
  44. * \return 1 if path looks like an absolute path, 0 if not
  45. **/
  46. int G_is_absolute_path(const char *path)
  47. {
  48. if (G_is_dirsep(path[0])
  49. #ifdef __MINGW32__
  50. || (isalpha(path[0]) && (path[1] == ':') && G_is_dirsep(path[2]))
  51. #endif
  52. )
  53. return 1;
  54. else
  55. return 0;
  56. }
  57. /**
  58. * \brief Converts directory separator characters in a string to the
  59. * native host separator character (/ on Unix, \ on Windows)
  60. *
  61. * \param path String to be converted
  62. *
  63. * \return Pointer to the string
  64. **/
  65. char *G_convert_dirseps_to_host(char *path)
  66. {
  67. char *i;
  68. for (i = path; *i; i++) {
  69. if (*i == GRASS_DIRSEP)
  70. *i = HOST_DIRSEP;
  71. }
  72. return path;
  73. }
  74. /**
  75. * \brief Converts directory separator characters in a string from the
  76. * native host character to the GRASS separator character (/)
  77. *
  78. *
  79. * \param path String to be converted
  80. *
  81. * \return Pointer to the string
  82. **/
  83. char *G_convert_dirseps_from_host(char *path)
  84. {
  85. char *i;
  86. for (i = path; *i; i++) {
  87. if (*i == HOST_DIRSEP)
  88. *i = GRASS_DIRSEP;
  89. }
  90. return path;
  91. }
  92. /**
  93. * \brief Get file status
  94. *
  95. * Returns information about the specified file.
  96. *
  97. * \param file_name file name
  98. * \param stat
  99. *
  100. * \return Return value from system lstat function
  101. **/
  102. int G_stat(const char *file_name, struct stat *buf)
  103. {
  104. return stat(file_name, buf);
  105. }
  106. /**
  107. * \brief Get file status
  108. *
  109. * Returns information about the specified file.
  110. *
  111. * \param file_name file name
  112. * \param stat in the case of a symbolic link, the link itself is
  113. * stat-ed, not the file that it refers to
  114. *
  115. * \return Return value from system lstat function
  116. **/
  117. int G_lstat(const char *file_name, struct stat *buf)
  118. {
  119. #ifdef __MINGW32__
  120. return stat(file_name, buf);
  121. #else
  122. return lstat(file_name, buf);
  123. #endif
  124. }