27#include <grass/config.h>
50static 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) && (S_IWUSR & buf.st_mode) &&
126 (S_IXUSR & buf.st_mode))) {
151static int _elem_count_split(
char *elems)
159 assert((len = strlen(elems)) > 0);
160 assert(len < PTRDIFF_MAX);
164 for (i = 0; begin !=
NULL && (ptrdiff_t)len > begin - elems; i++) {
168 end = strchr(begin,
'/');
170 if (end !=
NULL && end == begin)
190static char *_make_sublevels(
const char *elems)
193 char *cp, *
path, *top, *ptr;
197 if (
NULL == (top = _make_toplevel()))
207 if ((i = _elem_count_split(cp)) < 1) {
214 if ((
path = G_calloc(1, strlen(top) + strlen(elems) + 2)) ==
NULL) {
226 sprintf(
path,
"%s/%s", top, cp);
239 chmod(
path, S_IRWXU);
244 if (!S_ISDIR(buf.st_mode)) {
251 if (!((S_IRUSR & buf.st_mode) && (S_IWUSR & buf.st_mode) &&
252 (S_IXUSR & buf.st_mode))) {
261 ptr = strchr(cp,
'\0');
295 path = _make_toplevel();
297 else if (item ==
NULL) {
298 return _make_sublevels(
element);
306 ptr = strchr(item,
'/');
308 len = strlen(
path) + strlen(item) + 2;
309 if ((ptr = G_realloc(
path, len)) ==
NULL) {
314 ptr = strchr(
path,
'\0');
315 sprintf(ptr,
"/%s", item);
void G_free(void *buf)
Free allocated memory.
#define assert(condition)
int G_mkdir(const char *path)
Creates a new directory.
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
char * G_store(const char *s)
Copy string to allocated memory.
char * G_rc_path(const char *element, const char *item)
Returns path to element and item.