28 #include <grass/config.h>
37 #include <sys/types.h>
40 #include <grass/gis.h>
50 static char *_make_toplevel(
void)
56 char *defaulthomedir =
"c:";
57 char *homedir = getenv(
"HOME");
60 struct passwd *my_passwd;
69 if (
NULL == homedir) {
70 homedir = defaulthomedir;
73 len = strlen(homedir) + 8;
74 if (
NULL == (path = G_calloc(1, len))) {
77 sprintf(path,
"%s%s", homedir,
"/.grass");
80 my_passwd = getpwuid(me);
81 if (my_passwd ==
NULL)
84 len = strlen(my_passwd->pw_dir) + 8;
85 if (
NULL == (path = G_calloc(1, len)))
88 sprintf(path,
"%s%s", my_passwd->pw_dir,
"/.grass");
95 if (errno == ENOENT) {
104 chmod(path, S_IRWXU);
118 if (!S_ISDIR(buf.st_mode)) {
125 if (!((S_IRUSR & buf.st_mode) &&
126 (S_IWUSR & buf.st_mode) && (S_IXUSR & buf.st_mode)
154 static int _elem_count_split(
char *elems)
161 assert(elems !=
NULL);
162 assert((len = strlen(elems)) > 0);
163 assert(*elems !=
'/');
166 for (i = 0; begin !=
NULL && len > begin - elems; i++) {
170 end = strchr(begin,
'/');
172 if (end !=
NULL && end == begin)
193 static char *_make_sublevels(
const char *elems)
196 char *cp, *path, *top, *ptr;
200 if (
NULL == (top = _make_toplevel()))
210 if ((i = _elem_count_split(cp)) < 1) {
217 if ((path = G_calloc(1, strlen(top) + strlen(elems) + 2)) ==
NULL) {
229 sprintf(path,
"%s/%s", top, cp);
242 chmod(path, S_IRWXU);
247 if (!S_ISDIR(buf.st_mode)) {
254 if (!((S_IRUSR & buf.st_mode) &&
255 (S_IWUSR & buf.st_mode) && (S_IXUSR & buf.st_mode)
266 ptr = strchr(cp,
'\0');
297 assert(!(element ==
NULL && item ==
NULL));
300 if (element ==
NULL) {
301 path = _make_toplevel();
303 else if (item ==
NULL) {
304 return _make_sublevels(element);
307 path = _make_sublevels(element);
311 assert(*item !=
'.');
312 assert(path !=
NULL);
313 ptr = strchr(item,
'/');
315 len = strlen(path) + strlen(item) + 2;
316 if ((ptr = G_realloc(path, len)) ==
NULL) {
321 ptr = strchr(path,
'\0');
322 sprintf(ptr,
"/%s", item);
int G_mkdir(const char *path)
Creates a new directory.
char * G_store(const char *s)
Copy string to allocated memory.
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
char * G_rc_path(const char *element, const char *item)
Returns path to element and item.
void G_free(void *buf)
Free allocated memory.