GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 /*!
69  \brief Get current location UNIX-like path (internal use only)
70 
71  Allocated buffer should be freed by G_free(). See also
72  G_location_path().
73 
74  \todo Support also Windows-like path (?)
75 
76  \return buffer with location path
77  */
78 char *G__location_path(void)
79 {
80  const char *name = G_location();
81  const char *base = G_gisdbase();
82  char *location = G_malloc(strlen(base) + strlen(name) + 2);
83 
84  sprintf(location, "%s%c%s", base, HOST_DIRSEP, name);
85 
86  return location;
87 }
#define G_malloc(n)
Definition: defs/gis.h:112
const char * G_location(void)
Get current location name.
Definition: location.c:32
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
char * G__location_path(void)
Get current location UNIX-like path (internal use only)
Definition: location.c:78
#define HOST_DIRSEP
Definition: gis.h:223
char * G_location_path(void)
Get current location UNIX-like path.
Definition: location.c:54
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:7
const char * G_getenv(const char *)
Get environment variable.
Definition: env.c:353