home.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. char *G_home(void)
  30. {
  31. 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. char *G__home(void)
  38. {
  39. static char *home = 0;
  40. if (home)
  41. return home;
  42. #ifdef __MINGW32__
  43. {
  44. char buf[GPATH_MAX];
  45. /* TODO: we should probably check if the dir exists */
  46. home = getenv("USERPROFILE");
  47. if (!home) {
  48. sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
  49. if (strlen(buf) >= 0)
  50. home = G_store(buf);
  51. }
  52. if (!home)
  53. home = getenv("HOME");
  54. }
  55. #else
  56. home = getenv("HOME");
  57. #endif
  58. G_debug(2, "G__home home = %s", home);
  59. return home;
  60. }