Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect environment variable WEECHAT_HOME #391

Merged
merged 1 commit into from Apr 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 63 additions & 35 deletions src/core/weechat.c
Expand Up @@ -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 */
Expand Down