Commit 2b73bfb6 authored by Sidharth Kapur's avatar Sidharth Kapur

Use more glib functions

Also, check file_path_len >= home_len
parent 1bc5a64d
......@@ -571,14 +571,18 @@ get_formatted_filename(zathura_t* zathura, const char* file_path, bool statusbar
char *home = girara_get_home_directory(NULL);
size_t home_len = home ? strlen(home) : 0;
if (home_len > 1 && strncmp(home, file_path, home_len) == 0 && (!file_path[home_len] || file_path[home_len] == '/')) {
// Length should be total length of path - length of $HOME + 1 for '~' + 1 for '\0'
int tlen = file_path_len - home_len + 2;
char *tdir = g_try_malloc(sizeof(char) * tlen);
strncpy(tdir + 1, file_path + home_len, tlen);
tdir[0] = '~';
tdir[tlen - 1] = '\0';
return tdir;
if (home_len > 1
&& file_path_len >= home_len
&& g_str_has_prefix(file_path, home)
&& (!file_path[home_len] || file_path[home_len] == '/')) {
size_t remaining_len = file_path_len - home_len;
GString* remaining_path = g_string_new_len(&file_path[home_len], remaining_len);
char* tilde_path = g_strdup_printf("~%s", remaining_path->str);
g_string_free(remaining_path, true);
return tilde_path;
} else {
return g_strdup(file_path);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment