38 #include <sys/types.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;
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;
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))) {
151 static int _elem_count_split(
char *elems)
162 assert(len < PTRDIFF_MAX);
166 for (i = 0; begin !=
NULL && (ptrdiff_t)len > begin - elems; i++) {
170 end = strchr(begin,
'/');
172 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) {
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) && (S_IWUSR & buf.st_mode) &&
254 (S_IXUSR & buf.st_mode))) {
263 ptr = strchr(cp,
'\0');
297 path = _make_toplevel();
299 else if (item ==
NULL) {
300 return _make_sublevels(
element);
308 ptr = strchr(item,
'/');
310 len = strlen(
path) + strlen(item) + 2;
316 ptr = strchr(
path,
'\0');
317 sprintf(ptr,
"/%s", item);
void G_free(void *)
Free allocated memory.
int G_lstat(const char *, struct stat *)
Get file status.
int G_mkdir(const char *)
Creates a new directory.
char * G_store(const char *)
Copy string to allocated memory.
#define assert(condition)
char * G_rc_path(const char *element, const char *item)
Returns path to element and item.