GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2001-2009, 2012 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <string.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <grass/gis.h>
16#include <grass/glocale.h>
17
18#include "gis_local_proto.h"
19
20/*!
21 \brief Get current mapset name
22
23 Returns the name of the current mapset in the current location. This
24 routine is often used when accessing files in the current
25 mapset. See Mapsets for an explanation of mapsets.
26
27 G_fatal_error() is called on error.
28
29 \return mapset name
30 */
31const char *G_mapset(void)
32{
33 const char *m = G__mapset();
34
35 if (!m)
36 G_fatal_error(_("MAPSET is not set"));
37
38 return m;
39}
40
41/*!
42 \brief Get current mapset name (internal use only)
43
44 See G_mapset().
45
46 \return pointer mapset name
47 \return NULL on error
48 */
49const char *G__mapset(void)
50{
51 return G_getenv_nofatal("MAPSET");
52}
53
54/*!
55 \brief Get current mapset UNIX-like path
56
57 Allocated buffer should be freed by G_free(). See
58 G__mapset_path().
59
60 Returns the full UNIX path name of the current mapset. For example,
61 if the user is working in mapset <i>user1</i>, location
62 <i>spearfish</i> in the <i>/home/user/grassdata</i> database
63 directory, this routine will return a string which looks like
64 <i>/home/user/grassdata/spearfish/user1</i>.
65
66 This function also checks if mapset path is readable by the current
67 user. It calls G_fatal_error() on failure.
68
69 \return buffer with location path
70 */
71char *G_mapset_path(void)
72{
73 char *mapset;
74
75 mapset = G__mapset_path();
76 if (access(mapset, F_OK) != 0) {
77 perror("access");
78 G_fatal_error(_("MAPSET <%s> not available"), mapset);
79 }
80
81 return mapset;
82}
83
84/*!
85 \brief Get current mapset UNIX-like path (internal use only)
86
87 Allocated buffer should be freed by G_free(). See also
88 G_mapset_path().
89
90 \todo Support also Windows-like path (?)
91
92 \return buffer with mapset path
93 */
94char *G__mapset_path(void)
95{
96 const char *mapset = G__mapset();
97 const char *location = G_location();
98 const char *base = G_gisdbase();
99 size_t bufsize = strlen(base) + strlen(location) + strlen(mapset) + 3;
100 char *mapset_path = G_malloc(bufsize);
101
102 snprintf(mapset_path, bufsize, "%s/%s/%s", base, location, mapset);
103
104 return mapset_path;
105}
const char * G_getenv_nofatal(const char *)
Get environment variable.
Definition env.c:403
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
const char * G_gisdbase(void)
Get name of top level database directory.
Definition gisdbase.c:22
const char * G_location(void)
Get current location name.
Definition location.c:30
char * G__mapset_path(void)
Get current mapset UNIX-like path (internal use only)
Definition gis/mapset.c:94
char * G_mapset_path(void)
Get current mapset UNIX-like path.
Definition gis/mapset.c:71
const char * G__mapset(void)
Get current mapset name (internal use only)
Definition gis/mapset.c:49
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
#define _(str)
Definition glocale.h:10
#define access
Definition unistd.h:7
#define F_OK
Definition unistd.h:22