cursor: drop xcursor_add_path_elt

Just use snprintf instead. It doesn't really matter if we have
some duplicate slashes in filenames.

Signed-off-by: Simon Ser <contact@emersion.fr>
This commit is contained in:
Simon Ser 2022-05-27 17:49:10 +02:00
parent ccca002691
commit dfe820efce

View file

@ -527,38 +527,17 @@ xcursor_library_path(void)
return path; return path;
} }
static void
xcursor_add_path_elt(char *path, const char *elt, int len)
{
int pathlen = strlen(path);
/* append / if the path doesn't currently have one */
if (path[0] == '\0' || path[pathlen - 1] != '/') {
strcat(path, "/");
pathlen++;
}
if (len == -1)
len = strlen(elt);
/* strip leading slashes */
while (len && elt[0] == '/') {
elt++;
len--;
}
strncpy(path + pathlen, elt, len);
path[pathlen + len] = '\0';
}
static char * static char *
xcursor_build_theme_dir(const char *dir, const char *theme) xcursor_build_theme_dir(const char *dir, const char *theme)
{ {
const char *colon; const char *colon;
const char *tcolon; const char *tcolon;
char *full; char *full;
char *home; const char *home, *homesep;
int dirlen; int dirlen;
int homelen; int homelen;
int themelen; int themelen;
int len; size_t full_size;
if (!dir || !theme) if (!dir || !theme)
return NULL; return NULL;
@ -575,13 +554,15 @@ xcursor_build_theme_dir(const char *dir, const char *theme)
themelen = tcolon - theme; themelen = tcolon - theme;
home = NULL; home = "";
homelen = 0; homelen = 0;
homesep = "";
if (*dir == '~') { if (*dir == '~') {
home = getenv("HOME"); home = getenv("HOME");
if (!home) if (!home)
return NULL; return NULL;
homelen = strlen(home); homelen = strlen(home);
homesep = "/";
dir++; dir++;
dirlen--; dirlen--;
} }
@ -590,17 +571,12 @@ xcursor_build_theme_dir(const char *dir, const char *theme)
* add space for any needed directory separators, one per component, * add space for any needed directory separators, one per component,
* and one for the trailing null * and one for the trailing null
*/ */
len = 1 + homelen + 1 + dirlen + 1 + themelen + 1; full_size = 1 + homelen + 1 + dirlen + 1 + themelen + 1;
full = malloc(full_size);
full = malloc(len);
if (!full) if (!full)
return NULL; return NULL;
full[0] = '\0'; snprintf(full, full_size, "%s%s%.*s/%.*s", home, homesep,
dirlen, dir, themelen, theme);
if (home)
xcursor_add_path_elt(full, home, -1);
xcursor_add_path_elt(full, dir, dirlen);
xcursor_add_path_elt(full, theme, themelen);
return full; return full;
} }
@ -608,17 +584,16 @@ static char *
xcursor_build_fullname(const char *dir, const char *subdir, const char *file) xcursor_build_fullname(const char *dir, const char *subdir, const char *file)
{ {
char *full; char *full;
size_t full_size;
if (!dir || !subdir || !file) if (!dir || !subdir || !file)
return NULL; return NULL;
full = malloc(strlen(dir) + 1 + strlen(subdir) + 1 + strlen(file) + 1); full_size = strlen(dir) + 1 + strlen(subdir) + 1 + strlen(file) + 1;
full = malloc(full_size);
if (!full) if (!full)
return NULL; return NULL;
full[0] = '\0'; snprintf(full, full_size, "%s/%s/%s", dir, subdir, file);
xcursor_add_path_elt(full, dir, -1);
xcursor_add_path_elt(full, subdir, -1);
xcursor_add_path_elt(full, file, -1);
return full; return full;
} }