GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4e4cb0fe9
gis/mapset.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/mapset.c
3 
4  \brief GIS library - environment routines (mapset)
5 
6  (C) 2001-2009, 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 <string.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <grass/gis.h>
18 #include <grass/glocale.h>
19 
20 #include "gis_local_proto.h"
21 
22 /*!
23  \brief Get current mapset name
24 
25  Returns the name of the current mapset in the current location. This
26  routine is often used when accessing files in the current
27  mapset. See Mapsets for an explanation of mapsets.
28 
29  G_fatal_error() is called on error.
30 
31  \return mapset name
32  */
33 const char *G_mapset(void)
34 {
35  const char *m = G__mapset();
36 
37  if (!m)
38  G_fatal_error(_("MAPSET is not set"));
39 
40  return m;
41 }
42 
43 /*!
44  \brief Get current mapset name (internal use only)
45 
46  See G_mapset().
47 
48  \return pointer mapset name
49  \return NULL on error
50  */
51 const char *G__mapset(void)
52 {
53  return G_getenv_nofatal("MAPSET");
54 }
55 
56 /*!
57  \brief Get current mapset UNIX-like path
58 
59  Allocated buffer should be freed by G_free(). See
60  G__mapset_path().
61 
62  Returns the full UNIX path name of the current mapset. For example,
63  if the user is working in mapset <i>user1</i>, location
64  <i>spearfish</i> in the <i>/home/user/grassdata</i> database
65  directory, this routine will return a string which looks like
66  <i>/home/user/grassdata/spearfish/user1</i>.
67 
68  This function also checks if mapset path is readable by the current
69  user. It calls G_fatal_error() on failure.
70 
71  \return buffer with location path
72  */
73 char *G_mapset_path(void)
74 {
75  char *mapset;
76 
77  mapset = G__mapset_path();
78  if (access(mapset, F_OK) != 0) {
79  perror("access");
80  G_fatal_error(_("MAPSET <%s> not available"), mapset);
81  }
82 
83  return mapset;
84 }
85 
86 /*!
87  \brief Get current mapset UNIX-like path (internal use only)
88 
89  Allocated buffer should be freed by G_free(). See also
90  G_mapset_path().
91 
92  \todo Support also Windows-like path (?)
93 
94  \return buffer with mapset path
95  */
96 char *G__mapset_path(void)
97 {
98  const char *mapset = G__mapset();
99  const char *location = G_location();
100  const char *base = G_gisdbase();
101 
102  char *mapset_path =
103  G_malloc(strlen(base) + strlen(location) + strlen(mapset) + 3);
104 
105  sprintf(mapset_path, "%s/%s/%s", base, location, mapset);
106 
107  return mapset_path;
108 }
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_location(void)
Get current location name.
Definition: location.c:32
const char * G_getenv_nofatal(const char *)
Get environment variable.
Definition: env.c:405
char * G_mapset_path(void)
Get current mapset UNIX-like path.
Definition: gis/mapset.c:73
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
const char * G__mapset(void)
Get current mapset name (internal use only)
Definition: gis/mapset.c:51
char * G__mapset_path(void)
Get current mapset UNIX-like path (internal use only)
Definition: gis/mapset.c:96
#define _(str)
Definition: glocale.h:10