1 /* Get home directory */
10 #include <sys/types.h>
11 #include <sys/stat.h> /* OS/2 needs this after sys/types.h */
18 #include "config/home.h"
19 #include "config/options.h"
20 #include "intl/gettext/libintl.h"
21 #include "main/main.h"
22 #include "osdep/osdep.h"
23 #include "util/memory.h"
24 #include "util/string.h"
27 unsigned char *elinks_home
= NULL
;
31 strip_trailing_dir_sep(unsigned char *path
)
35 for (i
= strlen(path
) - 1; i
> 0; i
--)
36 if (!dir_sep(path
[i
]))
42 static unsigned char *
43 test_confdir(unsigned char *home
, unsigned char *path
,
44 unsigned char *error_message
)
47 unsigned char *confdir
;
49 if (!path
|| !*path
) return NULL
;
51 if (home
&& *home
&& !dir_sep(*path
))
52 confdir
= straconcat(home
, STRING_DIR_SEP
, path
, NULL
);
54 confdir
= stracpy(path
);
56 if (!confdir
) return NULL
;
58 strip_trailing_dir_sep(confdir
);
60 if (stat(confdir
, &st
)) {
61 if (!mkdir(confdir
, 0700)) {
63 /* I've no idea if following is needed for newly created
64 * directories. It's bad thing to do it everytime. --pasky */
66 chmod(home_elinks
, 0700);
72 } else if (S_ISDIR(st
.st_mode
)) {
78 usrerror(gettext(error_message
), path
, confdir
);
87 /* TODO: Check possibility to use <libgen.h> dirname. */
88 static unsigned char *
89 elinks_dirname(unsigned char *path
)
94 if (!path
) return NULL
;
97 if (!dir
) return NULL
;
99 for (i
= strlen(dir
) - 1; i
>= 0; i
--)
108 static unsigned char *
111 unsigned char *home_elinks
;
112 unsigned char *envhome
= getenv("HOME");
113 unsigned char *home
= envhome
? stracpy(envhome
)
114 : elinks_dirname(program
.path
);
117 strip_trailing_dir_sep(home
);
119 home_elinks
= test_confdir(home
,
120 get_cmd_opt_str("config-dir"),
121 N_("Commandline options -config-dir set to %s, "
122 "but could not create directory %s."));
123 if (home_elinks
) goto end
;
125 home_elinks
= test_confdir(home
, getenv("ELINKS_CONFDIR"),
126 N_("ELINKS_CONFDIR set to %s, "
127 "but could not create directory %s."));
128 if (home_elinks
) goto end
;
130 home_elinks
= test_confdir(home
, ".elinks", NULL
);
131 if (home_elinks
) goto end
;
133 home_elinks
= test_confdir(home
, "elinks", NULL
);
137 add_to_strn(&home_elinks
, STRING_DIR_SEP
);
147 elinks_home
= get_home();
149 ERROR(gettext("Unable to find or create ELinks config "
150 "directory. Please check if you have $HOME "
151 "variable set correctly and if you have "
152 "write permission to your home directory."));
161 mem_free_if(elinks_home
);