Tidy up some code

* less allocations
* less duplicate code

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>

(cherry picked from commit 9d335729a0)
This commit is contained in:
Steffen Jaeckel
2025-09-10 14:07:41 +02:00
committed by Jabber Developer
parent 1a385b8cd2
commit d3fa1c2f78

View File

@@ -1675,7 +1675,7 @@ char*
cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean previous) cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean previous)
{ {
unsigned int output_off = 0; unsigned int output_off = 0;
char* tmp; char* tmp = NULL;
// strip command // strip command
char* inpcp = (char*)input + strlen(startstr); char* inpcp = (char*)input + strlen(startstr);
@@ -1687,38 +1687,36 @@ cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean
// strip quotes // strip quotes
if (*inpcp == '"') { if (*inpcp == '"') {
tmp = strchr(inpcp + 1, '"'); tmp = strrchr(inpcp + 1, '"');
if (tmp) { if (tmp) {
*tmp = '\0'; *tmp = '\0';
} }
tmp = strdup(inpcp + 1); tmp = strdup(inpcp + 1);
free(inpcp); free(inpcp);
inpcp = tmp; inpcp = tmp;
tmp = NULL;
} }
// expand ~ to $HOME // expand ~ to $HOME
if (inpcp[0] == '~' && inpcp[1] == '/') { if (inpcp[0] == '~' && inpcp[1] == '/') {
tmp = g_strdup_printf("%s/%sfoo", getenv("HOME"), inpcp + 2); char* home = getenv("HOME");
if (!tmp) { if (!home) {
free(inpcp); free(inpcp);
return NULL; return NULL;
} }
output_off = strlen(getenv("HOME")) + 1; tmp = g_strdup_printf("%s/%sfoo", home, inpcp + 2);
output_off = strlen(home) + 1;
} else { } else {
tmp = g_strdup_printf("%sfoo", inpcp); tmp = g_strdup_printf("%sfoo", inpcp);
if (!tmp) {
free(inpcp);
return NULL;
}
} }
free(inpcp); free(inpcp);
inpcp = tmp; if (!tmp) {
return NULL;
}
char* inpcp2 = strdup(inpcp); char* foofile = strdup(basename(tmp));
char* foofile = strdup(basename(inpcp2)); char* directory = strdup(dirname(tmp));
char* directory = strdup(dirname(inpcp)); g_free(tmp);
free(inpcp);
free(inpcp2);
GArray* files = g_array_new(TRUE, FALSE, sizeof(char*)); GArray* files = g_array_new(TRUE, FALSE, sizeof(char*));
g_array_set_clear_func(files, (GDestroyNotify)_filepath_item_free); g_array_set_clear_func(files, (GDestroyNotify)_filepath_item_free);
@@ -1737,40 +1735,26 @@ cmd_ac_complete_filepath(const char* const input, char* const startstr, gboolean
continue; continue;
} }
char* acstring; char* acstring = NULL;
if (output_off) { if (output_off) {
tmp = g_strdup_printf("%s/%s", directory, dir->d_name); tmp = g_strdup_printf("%s/%s", directory, dir->d_name);
if (!tmp) { if (tmp) {
free(directory); acstring = g_strdup_printf("~/%s", tmp + output_off);
free(foofile); g_free(tmp);
return NULL;
} }
acstring = g_strdup_printf("~/%s", tmp + output_off);
if (!acstring) {
free(directory);
free(foofile);
return NULL;
}
free(tmp);
} else if (strcmp(directory, "/") == 0) { } else if (strcmp(directory, "/") == 0) {
acstring = g_strdup_printf("/%s", dir->d_name); acstring = g_strdup_printf("/%s", dir->d_name);
if (!acstring) {
free(directory);
free(foofile);
return NULL;
}
} else { } else {
acstring = g_strdup_printf("%s/%s", directory, dir->d_name); acstring = g_strdup_printf("%s/%s", directory, dir->d_name);
if (!acstring) { }
free(directory); if (!acstring) {
free(foofile); g_array_free(files, TRUE);
return NULL; free(foofile);
} free(directory);
return NULL;
} }
char* acstring_cpy = strdup(acstring); g_array_append_val(files, acstring);
g_array_append_val(files, acstring_cpy);
free(acstring);
} }
closedir(d); closedir(d);
} }