37 #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) &&
126 (S_IWUSR & buf.st_mode) && (S_IXUSR & buf.st_mode)
154 static int _elem_count_split(
char *elems)
162 assert((len = strlen(elems)) > 0);
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');
300 if (element ==
NULL) {
301 path = _make_toplevel();
303 else if (item ==
NULL) {
304 return _make_sublevels(element);
307 path = _make_sublevels(element);
313 ptr = strchr(item,
'/');
315 len = strlen(path) + strlen(item) + 2;
321 ptr = strchr(path,
'\0');
322 sprintf(ptr,
"/%s", item);
void G_free(void *)
Free allocated memory.
#define assert(condition)
int G_mkdir(const char *)
Creates a new directory.
char * G_store(const char *)
Copy string to allocated memory.
char * G_rc_path(const char *element, const char *item)
Returns path to element and item.
int G_lstat(const char *, struct stat *)
Get file status.