home.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* why return NULL when we've already exit(1)'d with G_fatal_error() ?? */
  33. return NULL;
  34. }
  35. /*!
  36. * \brief Get user's home directory
  37. *
  38. * Returns a pointer to a string which is the full path name of the
  39. * user's home directory.
  40. *
  41. * \return pointer to string
  42. * \return NULL on error
  43. */
  44. const char *G__home(void)
  45. {
  46. static int initialized;
  47. static const char *home = 0;
  48. if (G_is_initialized(&initialized))
  49. return home;
  50. #ifdef __MINGW32__
  51. {
  52. char buf[GPATH_MAX];
  53. /* TODO: we should probably check if the dir exists */
  54. home = getenv("USERPROFILE");
  55. if (!home) {
  56. sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
  57. if (strlen(buf) >= 0)
  58. home = G_store(buf);
  59. }
  60. if (!home)
  61. home = getenv("HOME");
  62. }
  63. #else
  64. home = getenv("HOME");
  65. #endif
  66. G_initialize_done(&initialized);
  67. return home;
  68. }
  69. #ifdef TODO
  70. #include <stdio.h>
  71. #define RCDIR ".grass7"
  72. /*!
  73. * \brief Get user's .grass/ config path directory
  74. *
  75. * Returns a pointer to a string which is the full path name of the
  76. * user's .grass/ config directory in their home directory.
  77. *
  78. * The path is not guaranteed to exist.
  79. (should it be? see possible TODO below)
  80. *
  81. * \return pointer to string
  82. * \return NULL on error
  83. */
  84. const char *G_rc_path(void)
  85. {
  86. /* choose better name for fn? */
  87. /* HB: making a complete bollocks of this, but to express the idea... */
  88. const char *rcpath = 0;
  89. sprintf(rcpath, "%s%c%s", G_home(), HOST_DIRSEP, RCDIR);
  90. #ifdef POSSIBILITY
  91. /* create it if it doesn't exist */
  92. #include <errno.h>
  93. int ret;
  94. ret = G_mkdir(rcpath);
  95. if (ret == -1 && errno != EEXIST)
  96. G_fatal_error(_("Cannot create directory [%s]"), rcpath);
  97. #endif
  98. return G_store(rcpath);
  99. }
  100. #endif