summaryrefslogtreecommitdiff
path: root/src/core/weechat.c
diff options
context:
space:
mode:
authorKarthik K <hashken.distro@gmail.com>2015-04-11 12:52:27 +0530
committerKarthik K <hashken.distro@gmail.com>2015-04-17 22:09:43 +0530
commit1eaf0e36e825d5aff394d0e1489b3b813cf144e4 (patch)
tree90a4671dd4db955ed8ea0643a5a7334885a4caa4 /src/core/weechat.c
parentc899254019a64119d52448202e7fcaafb6815144 (diff)
downloadweechat-1eaf0e36e825d5aff394d0e1489b3b813cf144e4.zip
core: Respect environment variable WEECHAT_HOME
If environment variable WEECHAT_HOME is set to a non-empty value, this is taken as the path for Weechat home directory. "--dir" command-line switch has higher precedence over this environment variable.
Diffstat (limited to 'src/core/weechat.c')
-rw-r--r--src/core/weechat.c98
1 files changed, 63 insertions, 35 deletions
diff --git a/src/core/weechat.c b/src/core/weechat.c
index ffdab2511..976944f9f 100644
--- a/src/core/weechat.c
+++ b/src/core/weechat.c
@@ -270,65 +270,93 @@ weechat_parse_args (int argc, char *argv[])
}
/*
- * Creates WeeChat home directory (by default ~/.weechat).
- *
- * Any error in this function is fatal: WeeChat can not run without a home
- * directory.
+ * Helper function for weechat_create_home_dir.
+ * Expands and assigns given string to weechat_home
*/
void
-weechat_create_home_dir ()
+weechat_create_home_dir_set_path (char* local_weechat_home)
{
- char *ptr_home, *config_weechat_home = WEECHAT_HOME;
+ char *ptr_home;
int dir_length;
- struct stat statinfo;
- if (!weechat_home)
+ if (local_weechat_home[0] == '~')
{
- if (strlen (config_weechat_home) == 0)
+ /* replace leading '~' by $HOME */
+ ptr_home = getenv ("HOME");
+ if (!ptr_home)
{
string_iconv_fprintf (stderr,
- _("Error: WEECHAT_HOME is undefined, check "
- "build options\n"));
+ _("Error: unable to get HOME directory\n"));
weechat_shutdown (EXIT_FAILURE, 0);
/* make C static analyzer happy (never executed) */
return;
}
-
- if (config_weechat_home[0] == '~')
+ dir_length = strlen (ptr_home) + strlen (local_weechat_home + 1) + 1;
+ weechat_home = malloc (dir_length);
+ if (weechat_home)
{
- /* replace leading '~' by $HOME */
- ptr_home = getenv ("HOME");
- if (!ptr_home)
- {
- string_iconv_fprintf (stderr,
- _("Error: unable to get HOME directory\n"));
- weechat_shutdown (EXIT_FAILURE, 0);
- /* make C static analyzer happy (never executed) */
- return;
- }
- dir_length = strlen (ptr_home) + strlen (config_weechat_home + 1) + 1;
- weechat_home = malloc (dir_length);
- if (weechat_home)
- {
- snprintf (weechat_home, dir_length,
- "%s%s", ptr_home, config_weechat_home + 1);
- }
+ snprintf (weechat_home, dir_length,
+ "%s%s", ptr_home, local_weechat_home + 1);
}
- else
+ }
+ else
+ {
+ weechat_home = strdup (local_weechat_home);
+ }
+
+ if (!weechat_home)
+ {
+ string_iconv_fprintf (stderr,
+ _("Error: not enough memory for home "
+ "directory\n"));
+ weechat_shutdown (EXIT_FAILURE, 0);
+ /* make C static analyzer happy (never executed) */
+ return;
+ }
+}
+
+/*
+ * Creates WeeChat home directory (by default ~/.weechat).
+ *
+ * Any error in this function is fatal: WeeChat can not run without a home
+ * directory.
+ */
+
+void
+weechat_create_home_dir ()
+{
+ char *ptr_weechat_home, *config_weechat_home;
+ struct stat statinfo;
+
+ /* weechat home is not set yet. Look for environment variable WEECHAT_HOME */
+ if (!weechat_home)
+ {
+ ptr_weechat_home = getenv ("WEECHAT_HOME");
+
+ /* Proceed only if environment variable WEECHAT_HOME is set to some value */
+ if (ptr_weechat_home && strlen (ptr_weechat_home) != 0)
{
- weechat_home = strdup (config_weechat_home);
+ weechat_create_home_dir_set_path (ptr_weechat_home);
}
+ }
+
+ /* If weechat_home is still not set, try to use compile time default */
+ if (!weechat_home)
+ {
+ config_weechat_home = WEECHAT_HOME;
- if (!weechat_home)
+ if (strlen (config_weechat_home) == 0)
{
string_iconv_fprintf (stderr,
- _("Error: not enough memory for home "
- "directory\n"));
+ _("Error: WEECHAT_HOME is undefined, check "
+ "build options\n"));
weechat_shutdown (EXIT_FAILURE, 0);
/* make C static analyzer happy (never executed) */
return;
}
+
+ weechat_create_home_dir_set_path (config_weechat_home);
}
/* if home already exists, it has to be a directory */