paths.c 2.7 KB

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