BZ_bzRead2 is based on BZ_bzRead from the bzlib library.
[elinks.git] / src / config / home.c
blob9e23afa97c9dbfeac2a6d88122da72adee40f8d5
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, NULL);
53 else
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)) {
62 #if 0
63 /* I've no idea if following is needed for newly created
64 * directories. It's bad thing to do it everytime. --pasky */
65 #ifdef HAVE_CHMOD
66 chmod(home_elinks, 0700);
67 #endif
68 #endif
69 return confdir;
72 } else if (S_ISDIR(st.st_mode)) {
73 first_use = 0;
74 return confdir;
77 if (error_message) {
78 usrerror(gettext(error_message), path, confdir);
79 sleep(3);
82 mem_free(confdir);
84 return NULL;
87 /* TODO: Check possibility to use <libgen.h> dirname. */
88 static unsigned char *
89 elinks_dirname(unsigned char *path)
91 int i;
92 unsigned char *dir;
94 if (!path) return NULL;
96 dir = stracpy(path);
97 if (!dir) return NULL;
99 for (i = strlen(dir) - 1; i >= 0; i--)
100 if (dir_sep(dir[i]))
101 break;
103 dir[i + 1] = 0;
105 return dir;
108 static unsigned char *
109 get_home(void)
111 unsigned char *home_elinks;
112 unsigned char *envhome = getenv("HOME");
113 unsigned char *home = envhome ? stracpy(envhome)
114 : elinks_dirname(program.path);
116 if (home)
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);
135 end:
136 if (home_elinks)
137 add_to_strn(&home_elinks, STRING_DIR_SEP);
138 mem_free_if(home);
140 return home_elinks;
143 void
144 init_home(void)
146 first_use = 1;
147 elinks_home = get_home();
148 if (!elinks_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."));
153 sleep(3);
154 return;
158 void
159 done_home(void)
161 mem_free_if(elinks_home);