From f7091f78e5e068ca4b73b34882ca402d041cb2c6 Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 24 Nov 2021 21:53:11 +0600 Subject: [PATCH 1/3] fix the malloc size in get_archive_cmd since the tr hack was removed in deead97, the format string is no longer 70 chars. also removes unnecessary malloc casting. --- src/nnn.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index 1c12e1cd..bd342362 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -2721,9 +2721,9 @@ static void get_archive_cmd(char *cmd, const char *archive) static void archive_selection(const char *cmd, const char *archive, const char *curpath) { - /* The 70 comes from the string below */ - char *buf = (char *)malloc((70 + xstrlen(cmd) + xstrlen(archive) - + xstrlen(curpath) + xstrlen(selpath)) * sizeof(char)); + /* The 38 comes from the format string below */ + char *buf = malloc((38 + xstrlen(cmd) + xstrlen(archive) + + xstrlen(curpath) + xstrlen(selpath)) * sizeof(char)); if (!buf) { DPRINTF_S(strerror(errno)); printwarn(NULL); From 578e6d1aaa00b8f051d65fc5617400529e7bd52c Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 24 Nov 2021 22:06:05 +0600 Subject: [PATCH 2/3] use a macro for the format string this makes things slightly more robust as changing the string inside the macro would automatically change the malloc size. --- src/nnn.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index bd342362..c6001abe 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -2719,10 +2719,10 @@ static void get_archive_cmd(char *cmd, const char *archive) xstrsncpy(cmd, archive_cmd[i], ARCHIVE_CMD_LEN); } +#define CMD_FMT " -ze 's|^%s/||' '%s' | xargs -0 %s %s" static void archive_selection(const char *cmd, const char *archive, const char *curpath) { - /* The 38 comes from the format string below */ - char *buf = malloc((38 + xstrlen(cmd) + xstrlen(archive) + char *buf = malloc((sizeof(CMD_FMT) + xstrlen(cmd) + xstrlen(archive) + xstrlen(curpath) + xstrlen(selpath)) * sizeof(char)); if (!buf) { DPRINTF_S(strerror(errno)); @@ -2730,12 +2730,11 @@ static void archive_selection(const char *cmd, const char *archive, const char * return; } - snprintf(buf, CMD_LEN_MAX, - SED" -ze 's|^%s/||' '%s' | xargs -0 %s %s", curpath, selpath, cmd, archive - ); + snprintf(buf, CMD_LEN_MAX, SED CMD_FMT, curpath, selpath, cmd, archive); spawn(utils[UTIL_SH_EXEC], buf, NULL, NULL, F_CLI | F_CONFIRM); free(buf); } +#undef CMD_FMT static void write_lastdir(const char *curpath, const char *outfile) { From bd8faa2dd8d15d32a2a1e4b2cecf92d321749886 Mon Sep 17 00:00:00 2001 From: NRK Date: Wed, 24 Nov 2021 22:34:22 +0600 Subject: [PATCH 3/3] use patterns[] for archive command string --- src/nnn.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/nnn.c b/src/nnn.c index c6001abe..4ad63455 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -765,6 +765,7 @@ static const char * const toks[] = { #define P_CPMVRNM 1 #define P_ARCHIVE 2 #define P_REPLACE 3 +#define P_ARCHIVE_CMD 4 static const char * const patterns[] = { SED" -i 's|^\\(\\(.*/\\)\\(.*\\)$\\)|#\\1\\n\\3|' %s", @@ -772,6 +773,7 @@ static const char * const patterns[] = { "%s | tr '\\n' '\\0' | xargs -0 -n2 sh -c '%s \"$0\" \"$@\" < /dev/tty'", "\\.(bz|bz2|gz|tar|taz|tbz|tbz2|tgz|z|zip)$", SED" -i 's|^%s\\(.*\\)$|%s\\1|' %s", + SED" -ze 's|^%s/||' '%s' | xargs -0 %s %s", }; /* Colors */ @@ -2719,22 +2721,20 @@ static void get_archive_cmd(char *cmd, const char *archive) xstrsncpy(cmd, archive_cmd[i], ARCHIVE_CMD_LEN); } -#define CMD_FMT " -ze 's|^%s/||' '%s' | xargs -0 %s %s" static void archive_selection(const char *cmd, const char *archive, const char *curpath) { - char *buf = malloc((sizeof(CMD_FMT) + xstrlen(cmd) + xstrlen(archive) - + xstrlen(curpath) + xstrlen(selpath)) * sizeof(char)); + char *buf = malloc((xstrlen(patterns[P_ARCHIVE_CMD]) + xstrlen(cmd) + xstrlen(archive) + + xstrlen(curpath) + xstrlen(selpath)) * sizeof(char)); if (!buf) { DPRINTF_S(strerror(errno)); printwarn(NULL); return; } - snprintf(buf, CMD_LEN_MAX, SED CMD_FMT, curpath, selpath, cmd, archive); + snprintf(buf, CMD_LEN_MAX, patterns[P_ARCHIVE_CMD], curpath, selpath, cmd, archive); spawn(utils[UTIL_SH_EXEC], buf, NULL, NULL, F_CLI | F_CONFIRM); free(buf); } -#undef CMD_FMT static void write_lastdir(const char *curpath, const char *outfile) {