2005-09-15 09:58:31 -04:00
|
|
|
/* Get home directory */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "config/home.h"
|
|
|
|
#include "config/options.h"
|
2021-08-08 15:25:08 -04:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/main.h"
|
|
|
|
#include "osdep/osdep.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
char *elinks_home = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
int first_use = 0;
|
|
|
|
|
|
|
|
static inline void
|
2021-01-02 10:20:27 -05:00
|
|
|
strip_trailing_dir_sep(char *path)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = strlen(path) - 1; i > 0; i--)
|
|
|
|
if (!dir_sep(path[i]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
path[i + 1] = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
|
|
|
test_confdir(char *home, char *path,
|
|
|
|
char *error_message)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct stat st;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *confdir;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!path || !*path) return NULL;
|
|
|
|
|
|
|
|
if (home && *home && !dir_sep(*path))
|
2007-03-11 06:59:11 -04:00
|
|
|
confdir = straconcat(home, STRING_DIR_SEP, path,
|
2021-01-02 10:20:27 -05:00
|
|
|
(char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
else
|
|
|
|
confdir = stracpy(path);
|
|
|
|
|
|
|
|
if (!confdir) return NULL;
|
|
|
|
|
|
|
|
strip_trailing_dir_sep(confdir);
|
|
|
|
|
|
|
|
if (stat(confdir, &st)) {
|
|
|
|
if (!mkdir(confdir, 0700)) {
|
|
|
|
#if 0
|
|
|
|
/* I've no idea if following is needed for newly created
|
|
|
|
* directories. It's bad thing to do it everytime. --pasky */
|
|
|
|
#ifdef HAVE_CHMOD
|
|
|
|
chmod(home_elinks, 0700);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return confdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
|
|
|
first_use = 0;
|
|
|
|
return confdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_message) {
|
|
|
|
usrerror(gettext(error_message), path, confdir);
|
|
|
|
sleep(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_free(confdir);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Check possibility to use <libgen.h> dirname. */
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
|
|
|
elinks_dirname(char *path)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int i;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *dir;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!path) return NULL;
|
|
|
|
|
|
|
|
dir = stracpy(path);
|
|
|
|
if (!dir) return NULL;
|
|
|
|
|
|
|
|
for (i = strlen(dir) - 1; i >= 0; i--)
|
|
|
|
if (dir_sep(dir[i]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
dir[i + 1] = 0;
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
2005-09-15 09:58:31 -04:00
|
|
|
get_home(void)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *home_elinks;
|
2021-12-10 10:05:52 -05:00
|
|
|
char *envhome = getenv("HOME_ETC") ?: getenv("HOME");
|
2021-01-02 10:20:27 -05:00
|
|
|
char *home = NULL;
|
2007-07-17 17:27:08 -04:00
|
|
|
|
|
|
|
if (!home && envhome)
|
|
|
|
home = stracpy(envhome);
|
|
|
|
if (!home)
|
|
|
|
home = user_appdata_directory();
|
|
|
|
if (!home)
|
|
|
|
home = elinks_dirname(program.path);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (home)
|
|
|
|
strip_trailing_dir_sep(home);
|
|
|
|
|
|
|
|
home_elinks = test_confdir(home,
|
|
|
|
get_cmd_opt_str("config-dir"),
|
|
|
|
N_("Commandline options -config-dir set to %s, "
|
|
|
|
"but could not create directory %s."));
|
|
|
|
if (home_elinks) goto end;
|
|
|
|
|
|
|
|
home_elinks = test_confdir(home, getenv("ELINKS_CONFDIR"),
|
|
|
|
N_("ELINKS_CONFDIR set to %s, "
|
|
|
|
"but could not create directory %s."));
|
|
|
|
if (home_elinks) goto end;
|
|
|
|
|
|
|
|
home_elinks = test_confdir(home, ".elinks", NULL);
|
|
|
|
if (home_elinks) goto end;
|
|
|
|
|
|
|
|
home_elinks = test_confdir(home, "elinks", NULL);
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (home_elinks)
|
2006-07-02 02:53:33 -04:00
|
|
|
add_to_strn(&home_elinks, STRING_DIR_SEP);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free_if(home);
|
|
|
|
|
|
|
|
return home_elinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_home(void)
|
|
|
|
{
|
|
|
|
first_use = 1;
|
|
|
|
elinks_home = get_home();
|
|
|
|
if (!elinks_home) {
|
|
|
|
ERROR(gettext("Unable to find or create ELinks config "
|
|
|
|
"directory. Please check if you have $HOME "
|
|
|
|
"variable set correctly and if you have "
|
|
|
|
"write permission to your home directory."));
|
|
|
|
sleep(3);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
done_home(void)
|
|
|
|
{
|
|
|
|
mem_free_if(elinks_home);
|
|
|
|
}
|