Elinks currently only support GB2312 as Chinese encoding, but GBK and
[elinks.git] / src / config / home.c
blob2af61a639a4f4a70f36afe6d0603a6c5161f656c
1 /* Get home directory */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h> /* OS/2 needs this after sys/types.h */
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
16 #include "elinks.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;
28 int first_use = 0;
30 static inline void
31 strip_trailing_dir_sep(unsigned char *path)
33 int i;
35 for (i = strlen(path) - 1; i > 0; i--)
36 if (!dir_sep(path[i]))
37 break;
39 path[i + 1] = 0;
42 static unsigned char *
43 test_confdir(unsigned char *home, unsigned char *path,
44 unsigned char *error_message)
46 struct stat st;
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);
54 else
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)) {
63 #if 0
64 /* I've no idea if following is needed for newly created
65 * directories. It's bad thing to do it everytime. --pasky */
66 #ifdef HAVE_CHMOD
67 chmod(home_elinks, 0700);
68 #endif
69 #endif
70 return confdir;
73 } else if (S_ISDIR(st.st_mode)) {
74 first_use = 0;
75 return confdir;
78 if (error_message) {
79 usrerror(gettext(error_message), path, confdir);
80 sleep(3);
83 mem_free(confdir);
85 return NULL;
88 /* TODO: Check possibility to use <libgen.h> dirname. */
89 static unsigned char *
90 elinks_dirname(unsigned char *path)
92 int i;
93 unsigned char *dir;
95 if (!path) return NULL;
97 dir = stracpy(path);
98 if (!dir) return NULL;
100 for (i = strlen(dir) - 1; i >= 0; i--)
101 if (dir_sep(dir[i]))
102 break;
104 dir[i + 1] = 0;
106 return dir;
109 static unsigned char *
110 get_home(void)
112 unsigned char *home_elinks;
113 unsigned char *envhome = getenv("HOME");
114 unsigned char *home = NULL;
116 if (!home && envhome)
117 home = stracpy(envhome);
118 if (!home)
119 home = user_appdata_directory();
120 if (!home)
121 home = elinks_dirname(program.path);
123 if (home)
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);
142 end:
143 if (home_elinks)
144 add_to_strn(&home_elinks, STRING_DIR_SEP);
145 mem_free_if(home);
147 return home_elinks;
150 void
151 init_home(void)
153 first_use = 1;
154 elinks_home = get_home();
155 if (!elinks_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."));
160 sleep(3);
161 return;
165 void
166 done_home(void)
168 mem_free_if(elinks_home);