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
,
53 (unsigned char *) NULL
);
55 confdir
= stracpy(path
);
57 if (!confdir
) return NULL
;
59 strip_trailing_dir_sep(confdir
);
61 if (stat(confdir
, &st
)) {
62 if (!mkdir(confdir
, 0700)) {
64 /* I've no idea if following is needed for newly created
65 * directories. It's bad thing to do it everytime. --pasky */
67 chmod(home_elinks
, 0700);
73 } else if (S_ISDIR(st
.st_mode
)) {
79 usrerror(gettext(error_message
), path
, confdir
);
88 /* TODO: Check possibility to use <libgen.h> dirname. */
89 static unsigned char *
90 elinks_dirname(unsigned char *path
)
95 if (!path
) return NULL
;
98 if (!dir
) return NULL
;
100 for (i
= strlen(dir
) - 1; i
>= 0; i
--)
109 static unsigned char *
112 unsigned char *home_elinks
;
113 unsigned char *envhome
= getenv("HOME");
114 unsigned char *home
= NULL
;
116 if (!home
&& envhome
)
117 home
= stracpy(envhome
);
119 home
= user_appdata_directory();
121 home
= elinks_dirname(program
.path
);
124 strip_trailing_dir_sep(home
);
126 home_elinks
= test_confdir(home
,
127 get_cmd_opt_str("config-dir"),
128 N_("Commandline options -config-dir set to %s, "
129 "but could not create directory %s."));
130 if (home_elinks
) goto end
;
132 home_elinks
= test_confdir(home
, getenv("ELINKS_CONFDIR"),
133 N_("ELINKS_CONFDIR set to %s, "
134 "but could not create directory %s."));
135 if (home_elinks
) goto end
;
137 home_elinks
= test_confdir(home
, ".elinks", NULL
);
138 if (home_elinks
) goto end
;
140 home_elinks
= test_confdir(home
, "elinks", NULL
);
144 add_to_strn(&home_elinks
, STRING_DIR_SEP
);
154 elinks_home
= get_home();
156 ERROR(gettext("Unable to find or create ELinks config "
157 "directory. Please check if you have $HOME "
158 "variable set correctly and if you have "
159 "write permission to your home directory."));
168 mem_free_if(elinks_home
);