GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
user_config.c
Go to the documentation of this file.
1 
2 /*!
3  * \file lib/gis/user_config.c
4  *
5  * \brief GIS Library - Routines related to user's GRASS configuration, tmp, and
6  * miscellaneous files.
7  *
8  * Functions related to the user's GRASS configuration, tmp, and
9  * miscellaneous files. Provides a set of routines for creating and
10  * accessing elements within the user's "rc" directory. The
11  * directory is in $HOME/.grass.<br>
12  *
13  * <b>NOTE:</b> As of 2001-03-25 this file is not hooked up. It is
14  * provided as a candidate for handling $HOME/.grass files and
15  * subdirectories. There may be more functionality desired (such as
16  * deletion routines, directory globs).<br>
17  *
18  * (C) 2001-2014 by the GRASS Development Team
19  *
20  * This program is free software under the GNU General Public License
21  * (>=v2). Read the file COPYING that comes with GRASS for details.
22  *
23  * \author Eric G Miller - egm2 at jps net
24  *
25  * \date 2007-04-14
26  */
27 
28 #include <grass/config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <unistd.h>
33 #include <string.h>
34 #ifndef __MINGW32__
35 #include <pwd.h>
36 #endif
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <errno.h>
40 #include <grass/gis.h>
41 
42 
43 /**************************************************************************
44  * _make_toplevel(): make user's toplevel config directory if it doesn't
45  * already exist. Adjust perms to 1700. Returns the toplevel directory
46  * path [caller must G_free ()] on success, or NULL on failure
47  *************************************************************************/
48 
49 #ifndef __MINGW32__ /* TODO */
50 static char *_make_toplevel(void)
51 {
52  size_t len;
53  int status;
54 
55 #ifdef __MINGW32__
56  char *defaulthomedir = "c:";
57  char *homedir = getenv("HOME");
58 #else
59  uid_t me;
60  struct passwd *my_passwd;
61 #endif
62  struct stat buf;
63  char *path;
64 
65  errno = 0;
66 
67  /* Query whatever database to get user's home dir */
68 #ifdef __MINGW32__
69  if (NULL == homedir) {
70  homedir = defaulthomedir;
71  }
72 
73  len = strlen(homedir) + 8; /* + "/.grass\0" */
74  if (NULL == (path = G_calloc(1, len))) {
75  return NULL;
76  }
77  sprintf(path, "%s%s", homedir, "/.grass");
78 #else
79  me = getuid();
80  my_passwd = getpwuid(me);
81  if (my_passwd == NULL)
82  return NULL;
83 
84  len = strlen(my_passwd->pw_dir) + 8; /* + "/.grass\0" */
85  if (NULL == (path = G_calloc(1, len)))
86  return NULL;
87 
88  sprintf(path, "%s%s", my_passwd->pw_dir, "/.grass");
89 #endif
90 
91  status = G_lstat(path, &buf);
92 
93  /* If errno == ENOENT, the directory doesn't exist */
94  if (status != 0) {
95  if (errno == ENOENT) {
96  status = G_mkdir(path);
97 
98  if (status != 0) { /* mkdir failed */
99  G_free(path);
100  return NULL;
101  }
102 
103  /* override umask settings, if possible */
104  chmod(path, S_IRWXU);
105 
106  /* otherwise mkdir succeeded, we're done here */
107  return path;
108  }
109 
110  /* other errors should not be defined ??? give up */
111  G_free(path);
112  return NULL;
113  }
114  /* implicit else */
115 
116  /* Examine the stat "buf" */
117  /* It better be a directory */
118  if (!S_ISDIR(buf.st_mode)) { /* File, link, something else */
119  errno = ENOTDIR; /* element is not a directory, but should be */
120  G_free(path);
121  return NULL;
122  }
123 
124  /* No read/write/execute ??? */
125  if (!((S_IRUSR & buf.st_mode) &&
126  (S_IWUSR & buf.st_mode) && (S_IXUSR & buf.st_mode)
127  )
128  ) {
129  errno = EACCES; /* Permissions error */
130  G_free(path);
131  return NULL;
132  }
133 
134  /* We'll assume that if the user grants greater permissions
135  * than we would, that they know what they're doing
136  * -- so we're done here...
137  */
138 
139  return path;
140 }
141 
142 
143 /**************************************************************************
144  * _elem_count_split: Does a couple things:
145  * 1) Counts the number of elements in "elems"
146  * 2) Replaces occurrences of '/' with '\0'
147  * 3) Checks that no element begins with a '.'
148  * 4) Checks there are no '//'
149  *
150  * Therefore, THE STRING THAT IS PASSED IN IS MODIFIED
151  * Returns 0 if there are no elements, or an element
152  * beginning with a '.' or containing a '//' is found.
153  *************************************************************************/
154 static int _elem_count_split(char *elems)
155 {
156  int i;
157  size_t len;
158  char *begin, *end;
159 
160  /* Some basic assertions */
161  assert(elems != NULL);
162  assert((len = strlen(elems)) > 0);
163  assert(*elems != '/');
164 
165  begin = elems;
166  for (i = 0; begin != NULL && len > begin - elems; i++) {
167  /* check '.' condition */
168  if (*begin == '.')
169  return 0;
170  end = strchr(begin, '/');
171  /* check '//' condition */
172  if (end != NULL && end == begin)
173  return 0;
174  /* okay, change '/' into '\0' */
175  begin = end;
176  if (begin != NULL) {
177  *begin = '\0'; /* begin points at '/', change it */
178  begin++; /* increment begin to next char */
179  }
180  }
181 
182  /* That's it */
183  return i;
184 }
185 
186 
187 /**************************************************************************
188  * _make_sublevels(): creates subelements as necessary from the passed
189  * "elems" string. It returns the full path if successful or NULL
190  * if it fails. "elems" must not be NULL, zero length, or have any
191  * elements that begin with a '.' or any occurrences of '//'.
192  *************************************************************************/
193 static char *_make_sublevels(const char *elems)
194 {
195  int i, status;
196  char *cp, *path, *top, *ptr;
197  struct stat buf;
198 
199  /* Get top level path */
200  if (NULL == (top = _make_toplevel()))
201  return NULL;
202 
203  /* Make a copy of elems */
204  if (NULL == (cp = G_store(elems))) {
205  G_free(top);
206  return NULL;
207  }
208 
209  /* Do element count, sanity checking and "splitting" */
210  if ((i = _elem_count_split(cp)) < 1) {
211  G_free(cp);
212  G_free(top);
213  return NULL;
214  }
215 
216  /* Allocate our path to be large enough */
217  if ((path = G_calloc(1, strlen(top) + strlen(elems) + 2)) == NULL) {
218  G_free(top);
219  G_free(cp);
220  return NULL;
221  }
222 
223  /* Now loop along adding directories if they don't exist
224  * make sure the thing is a directory as well.
225  * If there was a trailing '/' in the original "elem", it doesn't
226  * make it into the returned path.
227  */
228  for (; i > 0; i--) {
229  sprintf(path, "%s/%s", top, cp);
230  errno = 0;
231  status = G_lstat(path, &buf);
232  if (status != 0) {
233  /* the element doesn't exist */
234  status = G_mkdir(path);
235  if (status != 0) {
236  /* Some kind of problem... */
237  G_free(top);
238  G_free(cp);
239  return NULL;
240  }
241  /* override umask settings, if possible */
242  chmod(path, S_IRWXU);
243  }
244  else {
245  /* Examine the stat "buf" */
246  /* It better be a directory */
247  if (!S_ISDIR(buf.st_mode)) { /* File, link, something else */
248  errno = ENOTDIR; /* element is not a directory, but should be */
249  G_free(path);
250  return NULL;
251  }
252 
253  /* No read/write/execute ??? */
254  if (!((S_IRUSR & buf.st_mode) &&
255  (S_IWUSR & buf.st_mode) && (S_IXUSR & buf.st_mode)
256  )
257  ) {
258  errno = EACCES; /* Permissions error */
259  G_free(path);
260  return NULL;
261  }
262 
263  /* okay continue ... */
264  }
265 
266  ptr = strchr(cp, '\0');
267  *ptr = '/';
268  }
269 
270  /* All done, free memory */
271  G_free(top);
272  G_free(cp);
273 
274  return path;
275 }
276 
277 
278 /**
279  * \brief Returns path to <b>element</b> and <b>item</b>.
280  *
281  * Either <b>element</b> or <b>item</b> can be NULL, but not both. If
282  * <b>element</b> is NULL, then the file is assumed to live at the top
283  * level. If file is NULL, then it is assumed the caller is not
284  * interested in the file. If the element or rc dir do not exist, they
285  * are created. However, the file is never checked for.
286  *
287  * \param[in] element
288  * \param[in] item
289  * \return Pointer to string path
290  */
291 
292 char *G_rc_path(const char *element, const char *item)
293 {
294  size_t len;
295  char *path, *ptr;
296 
297  assert(!(element == NULL && item == NULL));
298 
299  /* Simple item in top-level */
300  if (element == NULL) {
301  path = _make_toplevel();
302  }
303  else if (item == NULL) {
304  return _make_sublevels(element);
305  }
306  else {
307  path = _make_sublevels(element);
308  }
309 
310 
311  assert(*item != '.');
312  assert(path != NULL);
313  ptr = strchr(item, '/'); /* should not have slashes */
314  assert(ptr == NULL);
315  len = strlen(path) + strlen(item) + 2;
316  if ((ptr = G_realloc(path, len)) == NULL) {
317  G_free(path);
318  return NULL;
319  }
320  path = ptr;
321  ptr = strchr(path, '\0');
322  sprintf(ptr, "/%s", item);
323 
324  return path;
325 } /* G_rc_path */
326 
327 
328 /* vim: set softtabstop=4 shiftwidth=4 expandtab: */
329 #endif
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define NULL
Definition: ccmath.h:32
#define G_calloc(m, n)
Definition: defs/gis.h:113
Definition: lidar.h:86
#define assert(condition)
Definition: lz4.c:324
Definition: path.h:16
int G_mkdir(const char *)
Creates a new directory.
Definition: paths.c:27
#define G_realloc(p, n)
Definition: defs/gis.h:114
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
char * G_rc_path(const char *element, const char *item)
Returns path to element and item.
Definition: user_config.c:292
char * getenv()
int G_lstat(const char *, struct stat *)
Get file status.
Definition: paths.c:145