ensure no duplicate XDG paths are prepended during startup

the previous patch(es) had a number of issues which made them unsuitable for general use:

* only checking "/usr" and "/usr/local" paths, despite this only being accurate if e was installed into /usr or /usr/local

* only checking if the paths were at the beginning of the string, when it's possible that they could be anywhere

* failure to also check XDG_CONFIG_DIRS

* improper formatting: this is a bit of a nitpick, but there are no correct instances of 'strcmp(a, b) == 0' in the e codebase.
This commit is contained in:
Mike Blumenkrantz 2015-01-29 16:01:25 -05:00
parent fbde0824d1
commit 4aa4c64508
1 changed files with 28 additions and 5 deletions

View File

@ -116,6 +116,20 @@ EAPI Eina_Bool e_nopause = EINA_FALSE;
EINTERN const char *e_first_frame = NULL;
EINTERN double e_first_frame_start_time = -1;
static Eina_Bool
_xdg_check_str(const char *env, const char *str)
{
const char *p;
size_t len;
len = strlen(str);
for (p = strstr(env, str); p; p++, p = strstr(p, str))
{
if ((!p[len]) || (p[len] == ':')) return EINA_TRUE;
}
return EINA_FALSE;
}
static void
_xdg_data_dirs_augment(void)
{
@ -126,18 +140,27 @@ _xdg_data_dirs_augment(void)
if (!p) return;
s = getenv("XDG_DATA_DIRS");
snprintf(newpath, sizeof(newpath), "%s:%s/share", e_prefix_data_get(), p);
if (s)
{
if (strncmp(s, newpath, strlen(newpath)))
Eina_Bool pfxdata, pfx;
pfxdata = !_xdg_check_str(s, e_prefix_data_get());
snprintf(newpath, sizeof(newpath), "%s/share", p);
pfx = !_xdg_check_str(s, newpath);
if (pfxdata || pfx)
{
snprintf(buf, sizeof(buf), "%s:%s", newpath, s);
snprintf(buf, sizeof(buf), "%s%s%s%s%s",
pfxdata ? e_prefix_data_get() : "",
pfxdata ? ":" : "",
pfx ? newpath : "",
pfx ? ":" : "",
s);
e_util_env_set("XDG_DATA_DIRS", buf);
}
}
else
{
snprintf(buf, sizeof(buf), "%s:/usr/local/share:/usr/share", newpath);
snprintf(buf, sizeof(buf), "%s:%s/share:/usr/local/share:/usr/share", e_prefix_data_get(), p);
e_util_env_set("XDG_DATA_DIRS", buf);
}
@ -145,7 +168,7 @@ _xdg_data_dirs_augment(void)
snprintf(newpath, sizeof(newpath), "%s/etc/xdg", p);
if (s)
{
if (strncmp(s, newpath, strlen(newpath)))
if (!_xdg_check_str(s, newpath))
{
snprintf(buf, sizeof(buf), "%s:%s", newpath, s);
e_util_env_set("XDG_CONFIG_DIRS", buf);