GRASS 8 Programmer's Manual 8.6.0dev(2026)-f6f2c534ea
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 */
32const 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 */
53const 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 */
99const 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
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
118 config_path = G_store(buf);
119
120#if 0
121 /* create it if it doesn't exist */
122#include <errno.h>
123 int ret;
124
125 ret = G_mkdir(rcpath);
126 if (ret == -1 && errno != EEXIST)
127 G_fatal_error(_("Failed to create directory [%s]"), rcpath);
128#endif
129
131
132 return config_path;
133}
#define NULL
Definition ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_is_initialized(int *)
Definition counter.c:60
void G_initialize_done(int *)
Definition counter.c:77
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
int G_mkdir(const char *)
Creates a new directory.
Definition paths.c:27
#define GPATH_MAX
Definition gis.h:199
#define HOST_DIRSEP
Definition gis.h:240
#define CONFIG_DIR
Definition gis.h:142
#define _(str)
Definition glocale.h:10
const char * G__home(void)
Get user's home directory (internal use only)
Definition home.c:53
const char * G_home(void)
Get user's home directory.
Definition home.c:32
const char * G_config_path(void)
Get user's config path directory.
Definition home.c:99