home.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. ****************************************************************
  3. * char *
  4. * G_home ()
  5. *
  6. * returns char pointer to home directory for user
  7. * dies if can't determine
  8. *
  9. * char *
  10. * G__home()
  11. *
  12. * returns char pointer to home directory for user
  13. * NULL if can't determine
  14. *
  15. ***************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. /*!
  21. * \brief user's home directory
  22. *
  23. * Returns a pointer to a string
  24. * which is the full path name of the user's home directory.
  25. *
  26. * \param ~
  27. * \return char *
  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. const char *G__home(void)
  38. {
  39. static int initialized;
  40. static const char *home = 0;
  41. if (G_is_initialized(&initialized))
  42. return home;
  43. #ifdef __MINGW32__
  44. {
  45. char buf[GPATH_MAX];
  46. /* TODO: we should probably check if the dir exists */
  47. home = getenv("USERPROFILE");
  48. if (!home) {
  49. sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
  50. if (strlen(buf) >= 0)
  51. home = G_store(buf);
  52. }
  53. if (!home)
  54. home = getenv("HOME");
  55. }
  56. #else
  57. home = getenv("HOME");
  58. #endif
  59. G_debug(2, "G__home home = %s", home);
  60. G_initialize_done(&initialized);
  61. return home;
  62. }