GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
home.c
Go to the documentation of this file.
1
/*!
2
* \file lib/gis/home.c
3
*
4
* \brief GIS Library - Get user's home or config directory.
5
*
6
* (C) 2001-2014 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 <stdlib.h>
15
#include <string.h>
16
#include <grass/gis.h>
17
#include <grass/glocale.h>
18
19
#include "gis_local_proto.h"
20
21
/*!
22
* \brief Get user's home directory
23
*
24
* Returns a pointer to a string which is the full path name of the
25
* user's home directory.
26
*
27
* Calls G_fatal_error() on failure.
28
*
29
* \return pointer to string
30
* \return NULL on error
31
*/
32
const
char
*
G_home
(
void
)
33
{
34
const
char
*home =
G__home
();
35
36
if
(home)
37
return
home;
38
39
G_fatal_error
(_(
"Unable to determine user's home directory"
));
40
41
return
NULL
;
42
}
43
44
/*!
45
* \brief Get user's home directory (internal use only)
46
*
47
* Returns a pointer to a string which is the full path name of the
48
* user's home directory.
49
*
50
* \return pointer to string
51
* \return NULL on error
52
*/
53
const
char
*
G__home
(
void
)
54
{
55
static
int
initialized;
56
static
const
char
*home = 0;
57
58
if
(
G_is_initialized
(&initialized))
59
return
home;
60
61
#ifdef __MINGW32__
62
{
63
char
buf[GPATH_MAX];
64
65
/* TODO: we should probably check if the dir exists */
66
home = getenv(
"USERPROFILE"
);
67
68
if
(!home) {
69
snprintf(buf,
sizeof
(buf),
"%s%s"
, getenv(
"HOMEDRIVE"
),
70
getenv(
"HOMEPATH"
));
71
72
if
(strlen(buf) >= 0)
73
home =
G_store
(buf);
74
}
75
76
if
(!home)
77
home = getenv(
"HOME"
);
78
}
79
#else
80
home = getenv(
"HOME"
);
81
#endif
82
G_initialize_done
(&initialized);
83
return
home;
84
}
85
86
/*!
87
* \brief Get user's config path directory
88
*
89
* Returns a pointer to a string which is the full path name of the
90
* user's GRASS config directory in their home directory.
91
*
92
* The path is not guaranteed to exist.
93
*
94
* \todo should it be? see possible TODO below
95
*
96
* \return pointer to string
97
* \return NULL on error
98
*/
99
const
char
*
G_config_path
(
void
)
100
{
101
static
int
initialized_config;
102
static
const
char
*config_path = 0;
103
char
buf[GPATH_MAX];
104
static
const
char
*config_dir =
NULL
;
105
106
if
(
G_is_initialized
(&initialized_config))
107
return
config_path;
108
109
config_dir = getenv(
"GRASS_CONFIG_DIR"
);
110
if
(!config_dir) {
111
#ifdef __MINGW32__
112
config_dir = getenv(
"APPDATA"
);
113
#else
114
config_dir =
G_home
();
115
#endif
116
}
117
#if defined(__APPLE__)
118
snprintf(buf, GPATH_MAX,
"%s%c%s%c%s"
, config_dir, HOST_DIRSEP,
"Library"
,
119
HOST_DIRSEP, CONFIG_DIR);
120
#else
121
snprintf(buf, GPATH_MAX,
"%s%c%s"
, config_dir, HOST_DIRSEP, CONFIG_DIR);
122
#endif
123
config_path =
G_store
(buf);
124
125
#if 0
126
/* create it if it doesn't exist */
127
#include <errno.h>
128
int
ret;
129
130
ret =
G_mkdir
(rcpath);
131
if
(ret == -1 && errno != EEXIST)
132
G_fatal_error
(_(
"Failed to create directory [%s]"
), rcpath);
133
#endif
134
135
G_initialize_done
(&initialized_config);
136
137
return
config_path;
138
}
NULL
#define NULL
Definition
ccmath.h:32
G_initialize_done
void G_initialize_done(int *p)
Definition
counter.c:77
G_is_initialized
int G_is_initialized(int *p)
Definition
counter.c:60
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition
gis/error.c:159
G__home
const char * G__home(void)
Get user's home directory (internal use only).
Definition
home.c:53
G_home
const char * G_home(void)
Get user's home directory.
Definition
home.c:32
G_config_path
const char * G_config_path(void)
Get user's config path directory.
Definition
home.c:99
G_mkdir
int G_mkdir(const char *path)
Creates a new directory.
Definition
paths.c:27
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition
strings.c:87
gis
home.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0