location.c 1.7 KB

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