home.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*!
  2. * \file lib/gis/home.c
  3. *
  4. * \brief GIS Library - Get user's home or config directory.
  5. *
  6. * (C) 2001-2014 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. * \brief Get user's home directory
  19. *
  20. * Returns a pointer to a string which is the full path name of the
  21. * user's home directory.
  22. *
  23. * Calls G_fatal_error() on failure.
  24. *
  25. * \return pointer to string
  26. * \return NULL on error
  27. */
  28. const char *G_home(void)
  29. {
  30. const char *home = G__home();
  31. if (home)
  32. return home;
  33. G_fatal_error(_("Unable to determine user's home directory"));
  34. return NULL;
  35. }
  36. /*!
  37. * \brief Get user's home directory (internal use only)
  38. *
  39. * Returns a pointer to a string which is the full path name of the
  40. * user's home directory.
  41. *
  42. * \return pointer to string
  43. * \return NULL on error
  44. */
  45. const char *G__home(void)
  46. {
  47. static int initialized;
  48. static const char *home = 0;
  49. if (G_is_initialized(&initialized))
  50. return home;
  51. #ifdef __MINGW32__
  52. {
  53. char buf[GPATH_MAX];
  54. /* TODO: we should probably check if the dir exists */
  55. home = getenv("USERPROFILE");
  56. if (!home) {
  57. sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
  58. if (strlen(buf) >= 0)
  59. home = G_store(buf);
  60. }
  61. if (!home)
  62. home = getenv("HOME");
  63. }
  64. #else
  65. home = getenv("HOME");
  66. #endif
  67. G_initialize_done(&initialized);
  68. return home;
  69. }
  70. /*!
  71. * \brief Get user's config path directory
  72. *
  73. * Returns a pointer to a string which is the full path name of the
  74. * user's GRASS config directory in their home directory.
  75. *
  76. * The path is not guaranteed to exist.
  77. *
  78. * \todo should it be? see possible TODO below
  79. *
  80. * \return pointer to string
  81. * \return NULL on error
  82. */
  83. const char *G_config_path(void)
  84. {
  85. static int initialized_config;
  86. static const char *config_path = 0;
  87. char buf[GPATH_MAX];
  88. if (G_is_initialized(&initialized_config))
  89. return config_path;
  90. #ifdef __MINGW32__
  91. sprintf(buf, "%s%c%s", getenv("APPDATA"), HOST_DIRSEP, CONFIG_DIR);
  92. #else
  93. sprintf(buf, "%s%c%s", G_home(), HOST_DIRSEP, CONFIG_DIR);
  94. #endif
  95. config_path = G_store(buf);
  96. #if 0
  97. /* create it if it doesn't exist */
  98. #include <errno.h>
  99. int ret;
  100. ret = G_mkdir(rcpath);
  101. if (ret == -1 && errno != EEXIST)
  102. G_fatal_error(_("Failed to create directory [%s]"), rcpath);
  103. #endif
  104. G_initialize_done(&initialized_config);
  105. return config_path;
  106. }