From ddcf4d4105d646d17544261f6570315b68b89e75 Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 16 Nov 2025 11:34:11 +0100 Subject: [PATCH] migration of concat to macro based implementation --- backend.c | 40 ++++++++++++--- backend.h | 2 +- file_previews.c | 5 +- interactions.c | 129 ++++++++++++++++++++++++------------------------ 4 files changed, 102 insertions(+), 74 deletions(-) diff --git a/backend.c b/backend.c index 317bc64..6a1d33d 100644 --- a/backend.c +++ b/backend.c @@ -1,14 +1,40 @@ #include #include -char* concat(const char *s1, const char *s2){ - const size_t len1 = strlen(s1); - const size_t len2 = strlen(s2); - char *result = malloc(len1 + len2 + 1); - memcpy(result, s1, len1); - memcpy(result + len1, s2, len2 + 1); - return result; +#define concat(out, s1, s2, _free) { \ + concat## _free(out, s1, s2); \ } + +#define concat0(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + out = result; + + +#define concat1(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + free(s1); \ + out = result; + +#define concat2(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + free(s2); \ + out = result; + +#define concat3(out, s1, s2) \ + char *result = malloc(strlen(s1) + strlen(s2) + 1); \ + memcpy(result, s1, strlen(s1)); \ + memcpy(result + strlen(s1), s2, strlen(s2) + 1); \ + free(s1); \ + free(s2); \ + out = result; + + char* smartstrcasestr(const char *haystack, const char *needle){ char smart = 0; char *ret; diff --git a/backend.h b/backend.h index 08fa404..29e0fb0 100644 --- a/backend.h +++ b/backend.h @@ -5,5 +5,5 @@ #endif -char* concat(const char *s1, const char *s2); +/*char* concat(const char *s1, const char *s2);*/ char* smartstrcasestr(const char *haystack, const char *needle); diff --git a/file_previews.c b/file_previews.c index 0097fb4..ba4ba49 100644 --- a/file_previews.c +++ b/file_previews.c @@ -85,8 +85,9 @@ char* text(char *path, unsigned long *file_size){ } } char* generic(char *path){ - char *cmd = concat("file ./\"", path); - cmd = concat(cmd, "\""); + char *cmd; + concat(cmd, "file ./\"", path, 0); + concat(cmd, cmd, "\"", 1); FILE *cmd_open = popen(cmd, "r"); char *line = NULL; diff --git a/interactions.c b/interactions.c index 1b6f6e5..a301b5b 100644 --- a/interactions.c +++ b/interactions.c @@ -236,9 +236,10 @@ void move_right(){ if (extension != NULL) { for (i = 0; i < file_extension_default_count; i++) { if (strstr(extension, file_extension_default_cmd[i].file_extension)) { - char *cmd = concat(file_extension_default_cmd[i].command, " ./\""); - cmd = concat(cmd, mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\""); + char *cmd; + concat(cmd, file_extension_default_cmd[i].command, " ./\"", 0); + concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); + concat(cmd, cmd, "\"", 1); if (system(cmd) == -1) { @@ -255,10 +256,10 @@ void move_right(){ for (i = 0; i < mimetype_default_count; i++) { if (strstr(mime, mimetype_default_cmd[i].mimetype)) { - char *cmd = concat(mimetype_default_cmd[i].command, " ./\""); - cmd = concat(cmd, mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\""); - + char *cmd; + concat(cmd, mimetype_default_cmd[i].command, " ./\"", 0); + concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); + concat(cmd, cmd, "\"", 1); if (system(cmd) == -1) { @@ -315,8 +316,8 @@ void open_with(){ mvwin(win_b, terminal_height-6, 0); wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - btm_buffer = concat("open \"", mid_content[selected_file_current].file_name); - btm_buffer = concat(btm_buffer, "\" with:"); + concat(btm_buffer, "open \"", mid_content[selected_file_current].file_name, 0); + concat(btm_buffer, btm_buffer, "\" with:", 1); window_btm(win_b, 1); @@ -328,9 +329,10 @@ void open_with(){ if (err == 0) { - char *cmd = concat(str, " ./\""); - cmd = concat(cmd, mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\""); + char *cmd; + concat(cmd, str, " ./\"", 0); + concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); + concat(cmd, cmd, "\"", 1); #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 images_clear(); @@ -358,8 +360,8 @@ void rename_hovered(){ mvwin(win_b, terminal_height-6, 0); wresize(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION, terminal_width/3); /*the div3 just looks cool*/ - btm_buffer = concat("rename \"", mid_content[selected_file_current].file_name); - btm_buffer = concat(btm_buffer, "\" to:"); + concat(btm_buffer, "rename \"", mid_content[selected_file_current].file_name, 0); + concat(btm_buffer, btm_buffer, "\" to:", 1); window_btm(win_b, 1); @@ -370,10 +372,11 @@ void rename_hovered(){ if (!err) { - char *cmd = concat("mv ./\"", mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\" ./\""); - cmd = concat(cmd, str); - cmd = concat(cmd, "\""); + char *cmd; + concat(cmd, "mv ./\"", mid_content[selected_file_current].file_name, 0); + concat(cmd, cmd, "\" ./\"", 1); + concat(cmd, cmd, str, 1); + concat(cmd, cmd, "\"", 1); if (system(cmd) == -1) { FAIL("rename_hovered", "mv or creating subcommand failed"); @@ -397,12 +400,12 @@ void delete(){ unsigned int i = 0; unsigned int hits = 0; - char *file_str = " "; + char *file_str = ""; for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { - file_str = concat(file_str, "\""); - file_str = concat(file_str, mid_content[i].file_name); - file_str = concat(file_str, "\" "); + concat(file_str, file_str, "\"", 0); + concat(file_str, file_str, mid_content[i].file_name, 1); + concat(file_str, file_str, "\" ", 1); hits++; } } @@ -497,7 +500,7 @@ void makedir(){ int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); if (!err) { - btm_buffer = concat(btm_buffer, str); + concat(btm_buffer, btm_buffer, str, 0); mode_t mask = umask(0); mkdir(str, 0755); /*magic number from default permissions as created by mkdir*/ umask(mask); @@ -527,7 +530,7 @@ void makefile(){ int err = read_string(win_b, BTM_WINDOW_HEIGHT_ON_STR_INTERACTION - 1, 0, str); if (!err) { - btm_buffer = concat(btm_buffer, str); + concat(btm_buffer, btm_buffer, str, 0); FILE *fp; fp = fopen(str, "w"); fclose(fp); @@ -590,7 +593,7 @@ void jump_to_dir(unsigned long passes, int index){ env_str[env_len-1] = '\0'; env_parsed = getenv(env_str); if (env_parsed) { - path = concat(env_parsed, (char*)key_binding[index].black_magic + env_len); + concat(path, env_parsed, (char*)key_binding[index].black_magic + env_len, 0); } else { path = malloc(strlen((char*)key_binding[index].black_magic)); memcpy(path, (char*)key_binding[index].black_magic, strlen((char*)key_binding[index].black_magic)+1); @@ -636,29 +639,30 @@ void cmd_on_selected(unsigned long passes, int index){ pthread_mutex_lock(&mutex_btm); char *btm_buffer_tmp = btm_buffer; + btm_buffer = ""; unsigned int i = 0; unsigned int hits = 0; - char *file_str = " "; + char *file_str = ""; for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { - file_str = concat(file_str, "\""); - file_str = concat(file_str, mid_content[i].file_name); - file_str = concat(file_str, "\" "); + concat(file_str, file_str, "\"", 0); + concat(file_str, file_str, mid_content[i].file_name, 1); + concat(file_str, file_str, "\" ", 1); hits++; } } if (hits) { - btm_buffer = concat(key_binding[index].black_magic, file_str); + concat(btm_buffer, key_binding[index].black_magic, file_str, 2); } else { - btm_buffer = concat(key_binding[index].black_magic, "\""); - btm_buffer = concat(btm_buffer, mid_content[selected_file_current].file_name); - btm_buffer = concat(btm_buffer, "\""); + concat(btm_buffer, key_binding[index].black_magic, "\"", 0); + concat(btm_buffer, btm_buffer, mid_content[selected_file_current].file_name, 1); + concat(btm_buffer, btm_buffer, "\"", 1); } - btm_buffer = concat(btm_buffer, "?"); - btm_buffer = concat(btm_buffer, "\n\n"); - btm_buffer = concat(btm_buffer, "(y/N)"); + concat(btm_buffer, btm_buffer, "?", 1); + concat(btm_buffer, btm_buffer, "\n\n", 1); + concat(btm_buffer, btm_buffer, "(y/N)", 1); werase(win_b); mvwin(win_b, terminal_height-6, 0); @@ -678,9 +682,9 @@ void cmd_on_selected(unsigned long passes, int index){ for (i = 0; i < mid_file_count; i++) { if (mid_content[i].status & FILE_STATUS_SELECTED) { free(cmd); - cmd = concat((char*)key_binding[index].black_magic, " \""); - cmd = concat(cmd, mid_content[i].file_name); - cmd = concat(cmd, "\""); + concat(cmd, (char*)key_binding[index].black_magic, " \"", 0); + concat(cmd, cmd, mid_content[i].file_name, 1); + concat(cmd, cmd, "\"", 1); if (system(cmd) != 0) { /*do nothing*/ } @@ -688,9 +692,9 @@ void cmd_on_selected(unsigned long passes, int index){ } } else { free(cmd); - cmd = concat((char*)key_binding[index].black_magic, " \""); - cmd = concat(cmd, mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\""); + concat(cmd, (char*)key_binding[index].black_magic, " \"", 0); + concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); + concat(cmd, cmd, "\"", 1); if (system(cmd) != 0) { /*do nothing*/ } @@ -706,9 +710,6 @@ void cmd_on_selected(unsigned long passes, int index){ status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); - if (hits) { - free(file_str); - } pthread_mutex_unlock(&mutex_btm); } void yank_text(unsigned long passes, int index){ @@ -716,16 +717,16 @@ void yank_text(unsigned long passes, int index){ char *cmd; if (strncmp((char*)key_binding[index].black_magic, "path", 4) == 0) { char *path=getcwd(NULL, 0); - cmd = concat("echo \"", path); - cmd = concat(cmd, "/"); - cmd = concat(cmd, mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\" | "); - cmd = concat(cmd, clipboard_cmd); + concat(cmd, "echo \"", path, 0); + concat(cmd, cmd, "/", 1); + concat(cmd, cmd, mid_content[selected_file_current].file_name, 1); + concat(cmd, cmd, "\" | ", 1); + concat(cmd, cmd, clipboard_cmd, 1); free(path); } else { - cmd = concat("echo \"", mid_content[selected_file_current].file_name); - cmd = concat(cmd, "\" | "); - cmd = concat(cmd, clipboard_cmd); + concat(cmd, "echo \"", mid_content[selected_file_current].file_name, 0); + concat(cmd, cmd, "\" | ", 1); + concat(cmd, cmd, clipboard_cmd, 1); } if (system(cmd) == -1) { /*do nothing*/ @@ -784,15 +785,15 @@ void paste(){ /*TODO(2025-08-14T22:10:44) escape path*/ char *cmd; if (yank_files.status & YANK_COPY) { - cmd = concat("false | cp -ri ", yank_files.path); + concat(cmd, "false | cp -ri ", yank_files.path, 0); } else { - cmd = concat("mv ", yank_files.path); + concat(cmd, "mv ", yank_files.path, 0); } - cmd = concat(cmd, "/"); - cmd = concat(cmd, *yank_files.list); - cmd = concat(cmd, " ./"); - cmd = concat(cmd, *yank_files.list); - cmd = concat(cmd, " 2>&1"); + concat(cmd, cmd, "/", 1); + concat(cmd, cmd, *yank_files.list, 1); + concat(cmd, cmd, " ./", 1); + concat(cmd, cmd, *yank_files.list, 1); + concat(cmd, cmd, " 2>&1", 1); char *line = malloc(INPUT_BUFFER_SIZE); FILE *cmd_open; while (1) { @@ -802,12 +803,12 @@ void paste(){ } if (strstr(line, "are the same file")) { cmd[strlen(cmd)-strlen(" 2>&1")] = '\0'; - cmd = concat(cmd, "_"); - cmd = concat(cmd, " 2>&1"); + concat(cmd, cmd, "_", 1); + concat(cmd, cmd, " 2>&1", 1); } else if ((strstr(line, "overwrite"))) { cmd[strlen(cmd)-strlen(" 2>&1")] = '\0'; - cmd = concat(cmd, "_"); - cmd = concat(cmd, " 2>&1"); + concat(cmd, cmd, "_", 1); + concat(cmd, cmd, " 2>&1", 1); } else if ((strstr(line, "No such file or directory"))) { pclose(cmd_open); break;