home.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. * \file gis/home.c
  3. *
  4. * \brief GIS Library - Get user's home directory.
  5. *
  6. * (C) 2001-2009 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. * \return pointer to string
  24. * \return NULL on error
  25. */
  26. const char *G_home(void)
  27. {
  28. const char *home = G__home();
  29. if (home)
  30. return home;
  31. G_fatal_error(_("Unable to determine user's home directory"));
  32. return NULL;
  33. }
  34. /*!
  35. * \brief Get user's home directory
  36. *
  37. * Returns a pointer to a string which is the full path name of the
  38. * user's home directory.
  39. *
  40. * \return pointer to string
  41. * \return NULL on error
  42. */
  43. const char *G__home(void)
  44. {
  45. static int initialized;
  46. static const char *home = 0;
  47. if (G_is_initialized(&initialized))
  48. return home;
  49. #ifdef __MINGW32__
  50. {
  51. char buf[GPATH_MAX];
  52. /* TODO: we should probably check if the dir exists */
  53. home = getenv("USERPROFILE");
  54. if (!home) {
  55. sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
  56. if (strlen(buf) >= 0)
  57. home = G_store(buf);
  58. }
  59. if (!home)
  60. home = getenv("HOME");
  61. }
  62. #else
  63. home = getenv("HOME");
  64. #endif
  65. G_initialize_done(&initialized);
  66. return home;
  67. }
  68. #ifdef TODO
  69. #include <stdio.h>
  70. #define RCDIR ".grass7"
  71. /*!
  72. * \brief Get user's .grass/ 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. (should it be? see possible TODO below)
  79. *
  80. * \return pointer to string
  81. * \return NULL on error
  82. */
  83. const char *G_rc_path(void)
  84. {
  85. /* choose better name for fn? */
  86. /* HB: making a complete bollocks of this, but to express the idea... */
  87. const char *rcpath = 0;
  88. sprintf(rcpath, "%s%c%s", G_home(), HOST_DIRSEP, RCDIR);
  89. #ifdef POSSIBILITY
  90. /* create it if it doesn't exist */
  91. #include <errno.h>
  92. int ret;
  93. ret = G_mkdir(rcpath);
  94. if (ret == -1 && errno != EEXIST)
  95. G_fatal_error(_("Failed to create directory [%s]"), rcpath);
  96. #endif
  97. return G_store(rcpath);
  98. }
  99. #endif