GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
location.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/location.c
3 
4  \brief GIS library - environment routines (location)
5 
6  (C) 2001-2008, 2012 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 
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 
21 #include "gis_local_proto.h"
22 
23 /*!
24  \brief Get current location name
25 
26  Returns the name of the current database location. This routine
27  should be used by modules that need to display the current location
28  to the user. See Locations for an explanation of locations.
29 
30  \return location name
31  */
32 const char *G_location(void)
33 {
34  return G_getenv("LOCATION_NAME");
35 }
36 
37 /*!
38  \brief Get current location UNIX-like path
39 
40  Allocated buffer should be freed by G_free(). See
41  G__location_path().
42 
43  Returns the full UNIX path name of the current database
44  location. For example, if the user is working in location
45  <i>spearfish</i> in the <i>/home/user/grassdata</i> database
46  directory, this routine will return a string which looks like
47  <i>/home/user/grassdata/spearfish</i>.
48 
49  This function also checks if location path is readable by the
50  current user. It calls G_fatal_error() on failure.
51 
52  \return buffer with location path
53  */
54 char *G_location_path(void)
55 {
56  char *location;
57 
58  location = G__location_path();
59  if (access(location, F_OK) != 0) {
60  perror("access");
61  G_fatal_error(_("LOCATION <%s> not available"), location);
62  }
63 
64  return location;
65 }
66 
67 /*!
68  \brief Get current location UNIX-like path (internal use only)
69 
70  Allocated buffer should be freed by G_free(). See also
71  G_location_path().
72 
73  \todo Support also Windows-like path (?)
74 
75  \return buffer with location path
76  */
77 char *G__location_path(void)
78 {
79  const char *name = G_location();
80  const char *base = G_gisdbase();
81  char *location = G_malloc(strlen(base) + strlen(name) + 2);
82 
83  sprintf(location, "%s%c%s", base, HOST_DIRSEP, name);
84 
85  return location;
86 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
const char * G_gisdbase(void)
Get name of top level database directory.
Definition: gisdbase.c:26
#define G_malloc(n)
Definition: defs/gis.h:94
const char * G_getenv(const char *)
Get environment variable.
Definition: env.c:360
#define HOST_DIRSEP
Definition: gis.h:235
#define _(str)
Definition: glocale.h:10
char * G__location_path(void)
Get current location UNIX-like path (internal use only)
Definition: location.c:77
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
const char * G_location(void)
Get current location name.
Definition: location.c:32
const char * name
Definition: named_colr.c:6