location.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*!
  2. \file lib/gis/location.c
  3. \brief GIS library - environment routines (location)
  4. (C) 2001-2008, 2012 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. #include "gis_local_proto.h"
  16. /*!
  17. \brief Get current location name
  18. Returns the name of the current database location. This routine
  19. should be used by modules that need to display the current location
  20. to the user. See Locations for an explanation of locations.
  21. \return location name
  22. */
  23. const char *G_location(void)
  24. {
  25. return G_getenv("LOCATION_NAME");
  26. }
  27. /*!
  28. \brief Get current location UNIX-like path
  29. Allocated buffer should be freed by G_free(). See
  30. G__location_path().
  31. Returns the full UNIX path name of the current database
  32. location. For example, if the user is working in location
  33. <i>spearfish</i> in the <i>/home/user/grassdata</i> database
  34. directory, this routine will return a string which looks like
  35. <i>/home/user/grassdata/spearfish</i>.
  36. This function also checks if location path is readable by the
  37. current user. It calls G_fatal_error() on failure.
  38. \return buffer with location path
  39. */
  40. char *G_location_path(void)
  41. {
  42. char *location;
  43. location = G__location_path();
  44. if (access(location, F_OK) != 0) {
  45. perror("access");
  46. G_fatal_error(_("LOCATION <%s> not available"), location);
  47. }
  48. return location;
  49. }
  50. /*!
  51. \brief Get current location UNIX-like path (internal use only)
  52. Allocated buffer should be freed by G_free(). See also
  53. G_location_path().
  54. \todo Support also Windows-like path (?)
  55. \return buffer with location path
  56. */
  57. char *G__location_path(void)
  58. {
  59. const char *name = G_location();
  60. const char *base = G_gisdbase();
  61. char *location = G_malloc(strlen(base) + strlen(name) + 2);
  62. sprintf(location, "%s%c%s", base, HOST_DIRSEP, name);
  63. return location;
  64. }