GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  sprintf(buf, "%s%s", getenv("HOMEDRIVE"), getenv("HOMEPATH"));
70 
71  if (strlen(buf) >= 0)
72  home = G_store(buf);
73  }
74 
75  if (!home)
76  home = getenv("HOME");
77  }
78 #else
79  home = getenv("HOME");
80 #endif
81  G_initialize_done(&initialized);
82  return home;
83 }
84 
85 /*!
86  * \brief Get user's config path directory
87  *
88  * Returns a pointer to a string which is the full path name of the
89  * user's GRASS config directory in their home directory.
90  *
91  * The path is not guaranteed to exist.
92  *
93  * \todo should it be? see possible TODO below
94  *
95  * \return pointer to string
96  * \return NULL on error
97  */
98 const char *G_config_path(void)
99 {
100  static int initialized_config;
101  static const char *config_path = 0;
102  char buf[GPATH_MAX];
103 
104  if (G_is_initialized(&initialized_config))
105  return config_path;
106 
107 #ifdef __MINGW32__
108  sprintf(buf, "%s%c%s", getenv("APPDATA"), HOST_DIRSEP, CONFIG_DIR);
109 #else
110  sprintf(buf, "%s%c%s", G_home(), HOST_DIRSEP, CONFIG_DIR);
111 #endif
112  config_path = G_store(buf);
113 
114 #if 0
115  /* create it if it doesn't exist */
116 #include <errno.h>
117  int ret;
118  ret = G_mkdir(rcpath);
119  if (ret == -1 && errno != EEXIST)
120  G_fatal_error(_("Failed to create directory [%s]"), rcpath);
121 #endif
122 
123  G_initialize_done(&initialized_config);
124 
125  return config_path;
126 }
const char * G__home(void)
Get user&#39;s home directory (internal use only)
Definition: home.c:53
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *)
Definition: counter.c:76
#define HOST_DIRSEP
Definition: gis.h:213
#define CONFIG_DIR
Definition: gis.h:129
#define GPATH_MAX
Definition: gis.h:170
const char * G_config_path(void)
Get user&#39;s config path directory.
Definition: home.c:98
int G_is_initialized(int *)
Definition: counter.c:59
int G_mkdir(const char *)
Creates a new directory.
Definition: paths.c:27
#define _(str)
Definition: glocale.h:10
const char * G_home(void)
Get user&#39;s home directory.
Definition: home.c:32
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
char * getenv()