GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
home.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <grass/gis.h>
17 #include <grass/glocale.h>
18 
19 #include "local_proto.h"
20 
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 
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 
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's home directory (internal use only)
Definition: home.c:53
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:27
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int G_is_initialized(int *p)
Definition: counter.c:59
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *p)
Definition: counter.c:76
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
const char * G_config_path(void)
Get user's config path directory.
Definition: home.c:98
const char * G_home(void)
Get user's home directory.
Definition: home.c:32