home.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "gis_local_proto.h"
  18. /*!
  19. * \brief Get user's home directory
  20. *
  21. * Returns a pointer to a string which is the full path name of the
  22. * user's home directory.
  23. *
  24. * Calls G_fatal_error() on failure.
  25. *
  26. * \return pointer to string
  27. * \return NULL on error
  28. */
  29. const char *G_home(void)
  30. {
  31. const char *home = G__home();
  32. if (home)
  33. return home;
  34. G_fatal_error(_("Unable to determine user's home directory"));
  35. return NULL;
  36. }
  37. /*!
  38. * \brief Get user's home directory (internal use only)
  39. *
  40. * Returns a pointer to a string which is the full path name of the
  41. * user's home directory.
  42. *
  43. * \return pointer to string
  44. * \return NULL on error
  45. */
  46. const char *G__home(void)
  47. {
  48. static int initialized;
  49. static const char *home = 0;
  50. if (G_is_initialized(&initialized))
  51. return home;
  52. #ifdef __MINGW32__
  53. {
  54. char buf[GPATH_MAX];
  55. /* TODO: we should probably check if the dir exists */
  56. home = getenv("USERPROFILE");
  57. if (!home) {
  58. sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
  59. if (strlen(buf) >= 0)
  60. home = G_store(buf);
  61. }
  62. if (!home)
  63. home = getenv("HOME");
  64. }
  65. #else
  66. home = getenv("HOME");
  67. #endif
  68. G_initialize_done(&initialized);
  69. return home;
  70. }
  71. /*!
  72. * \brief Get user's config path directory
  73. *
  74. * Returns a pointer to a string which is the full path name of the
  75. * user's GRASS config directory in their home directory.
  76. *
  77. * The path is not guaranteed to exist.
  78. *
  79. * \todo should it be? see possible TODO below
  80. *
  81. * \return pointer to string
  82. * \return NULL on error
  83. */
  84. const char *G_config_path(void)
  85. {
  86. static int initialized_config;
  87. static const char *config_path = 0;
  88. char buf[GPATH_MAX];
  89. if (G_is_initialized(&initialized_config))
  90. return config_path;
  91. #ifdef __MINGW32__
  92. sprintf(buf, "%s%c%s", getenv("APPDATA"), HOST_DIRSEP, CONFIG_DIR);
  93. #else
  94. sprintf(buf, "%s%c%s", G_home(), HOST_DIRSEP, CONFIG_DIR);
  95. #endif
  96. config_path = G_store(buf);
  97. #if 0
  98. /* create it if it doesn't exist */
  99. #include <errno.h>
  100. int ret;
  101. ret = G_mkdir(rcpath);
  102. if (ret == -1 && errno != EEXIST)
  103. G_fatal_error(_("Failed to create directory [%s]"), rcpath);
  104. #endif
  105. G_initialize_done(&initialized_config);
  106. return config_path;
  107. }