GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 * SPDX-FileCopyrightText: 2001-2014 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <stdlib.h>
13#include <string.h>
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17#include "gis_local_proto.h"
18
19/*!
20 * \brief Get user's home directory
21 *
22 * Returns a pointer to a string which is the full path name of the
23 * user's home directory.
24 *
25 * Calls G_fatal_error() on failure.
26 *
27 * \return pointer to string
28 * \return NULL on error
29 */
30const char *G_home(void)
31{
32 const char *home = G__home();
33
34 if (home)
35 return home;
36
37 G_fatal_error(_("Unable to determine user's home directory"));
38
39 return NULL;
40}
41
42/*!
43 * \brief Get user's home directory (internal use only)
44 *
45 * Returns a pointer to a string which is the full path name of the
46 * user's home directory.
47 *
48 * \return pointer to string
49 * \return NULL on error
50 */
51const char *G__home(void)
52{
53 static int initialized;
54 static const char *home = 0;
55
56 if (G_is_initialized(&initialized))
57 return home;
58
59#ifdef __MINGW32__
60 {
61 char buf[GPATH_MAX];
62
63 /* TODO: we should probably check if the dir exists */
64 home = getenv("USERPROFILE");
65
66 if (!home) {
67 snprintf(buf, sizeof(buf), "%s%s", getenv("HOMEDRIVE"),
68 getenv("HOMEPATH"));
69
70 if (strlen(buf) >= 0)
71 home = G_store(buf);
72 }
73
74 if (!home)
75 home = getenv("HOME");
76 }
77#else
78 home = getenv("HOME");
79#endif
80 G_initialize_done(&initialized);
81 return home;
82}
83
84/*!
85 * \brief Get user's config path directory
86 *
87 * Returns a pointer to a string which is the full path name of the
88 * user's GRASS config directory in their home directory.
89 *
90 * The path is not guaranteed to exist.
91 *
92 * \todo should it be? see possible TODO below
93 *
94 * \return pointer to string
95 * \return NULL on error
96 */
97const char *G_config_path(void)
98{
99 static int initialized_config;
100 static const char *config_path = 0;
101 char buf[GPATH_MAX];
102 static const char *config_dir = NULL;
103
105 return config_path;
106
107 config_dir = getenv("GRASS_CONFIG_DIR");
108 if (!config_dir) {
109#ifdef __MINGW32__
110 config_dir = getenv("APPDATA");
111#else
112 config_dir = G_home();
113#endif
114 }
115#if defined(__APPLE__)
116 snprintf(buf, GPATH_MAX, "%s%c%s%c%s", config_dir, HOST_DIRSEP, "Library",
118#else
120#endif
121 config_path = G_store(buf);
122
123#if 0
124 /* create it if it doesn't exist */
125#include <errno.h>
126 int ret;
127
128 ret = G_mkdir(rcpath);
129 if (ret == -1 && errno != EEXIST)
130 G_fatal_error(_("Failed to create directory [%s]"), rcpath);
131#endif
132
134
135 return config_path;
136}
#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:85
int G_mkdir(const char *)
Creates a new directory.
Definition paths.c:27
#define GPATH_MAX
Definition gis.h:196
#define HOST_DIRSEP
Definition gis.h:237
#define CONFIG_DIR
Definition gis.h:139
#define _(str)
Definition glocale.h:10
const char * G__home(void)
Get user's home directory (internal use only)
Definition home.c:51
const char * G_home(void)
Get user's home directory.
Definition home.c:30
const char * G_config_path(void)
Get user's config path directory.
Definition home.c:97