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