36 #include <sys/types.h>
39 #include <grass/gis.h>
49 static char *_make_toplevel(
void)
55 char *defaulthomedir =
"c:";
56 char *homedir =
getenv(
"HOME");
59 struct passwd *my_passwd;
68 if (
NULL == homedir) {
69 homedir = defaulthomedir;
72 len = strlen(homedir) + 8;
73 if (
NULL == (path = G_calloc(1, len))) {
76 sprintf(path,
"%s%s", homedir,
"/.grass");
79 my_passwd = getpwuid(me);
80 if (my_passwd ==
NULL)
83 len = strlen(my_passwd->pw_dir) + 8;
84 if (
NULL == (path = G_calloc(1, len)))
87 sprintf(path,
"%s%s", my_passwd->pw_dir,
"/.grass");
94 if (
errno == ENOENT) {
103 chmod(path, S_IRWXU);
117 if (!S_ISDIR(
buf.st_mode)) {
124 if (!((S_IRUSR &
buf.st_mode) &&
125 (S_IWUSR &
buf.st_mode) && (S_IXUSR &
buf.st_mode)
153 static int _elem_count_split(
char *elems)
160 assert(elems !=
NULL);
161 assert((len = strlen(elems)) > 0);
162 assert(*elems !=
'/');
165 for (i = 0; begin !=
NULL && len > begin - elems; i++) {
169 end = strchr(begin,
'/');
171 if (end !=
NULL && end == begin)
192 static char *_make_sublevels(
const char *elems)
195 char *cp, *path, *top, *ptr;
199 if (
NULL == (top = _make_toplevel()))
209 if ((i = _elem_count_split(cp)) < 1) {
216 if ((path = G_calloc(1, strlen(top) + strlen(elems) + 2)) ==
NULL) {
228 sprintf(path,
"%s/%s", top, cp);
241 chmod(path, S_IRWXU);
246 if (!S_ISDIR(
buf.st_mode)) {
253 if (!((S_IRUSR &
buf.st_mode) &&
254 (S_IWUSR &
buf.st_mode) && (S_IXUSR &
buf.st_mode)
265 ptr = strchr(cp,
'\0');
296 assert(!(element ==
NULL && item ==
NULL));
299 if (element ==
NULL) {
300 path = _make_toplevel();
302 else if (item ==
NULL) {
303 return _make_sublevels(element);
306 path = _make_sublevels(element);
310 assert(*item !=
'.');
311 assert(path !=
NULL);
312 ptr = strchr(item,
'/');
314 len = strlen(path) + strlen(item) + 2;
315 if ((ptr = G_realloc(path, len)) ==
NULL) {
320 ptr = strchr(path,
'\0');
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
char * G_store(const char *s)
Copy string to allocated memory.
int G_mkdir(const char *path)
Creates a new directory.
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
char * G_rc_path(const char *element, const char *item)
Returns path to element and item.