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