From: Philip Hazelden Date: Sun, 3 Jul 2022 17:54:43 +0000 (+0100) Subject: Fix `rename -a ''`. X-Git-Tag: v2.39-rc1~584^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2285629d7d4564773b106fa1fa29a3e34cbed3ea;p=thirdparty%2Futil-linux.git Fix `rename -a ''`. It previously entered an infinite loop. Now it inserts the `to` string between every character of `from`, including at the beginning and end. rename -a '' _ 123.txt # renames to _1_2_3_._t_x_t_ --- diff --git a/misc-utils/rename.c b/misc-utils/rename.c index e47359c0d9..80a0f31ca4 100644 --- a/misc-utils/rename.c +++ b/misc-utils/rename.c @@ -57,8 +57,8 @@ static int string_replace(char *from, char *to, char *s, char *orig, char **newn if (where == NULL) return 1; count++; - while ((all || last) && p) { - p = strstr(p + (last ? 1 : fromlen), from); + while ((all || last) && p && *p) { + p = strstr(p + (last ? 1 : max(fromlen, (size_t) 1)), from); if (p) { if (all) count++; @@ -75,8 +75,13 @@ static int string_replace(char *from, char *to, char *s, char *orig, char **newn p = to; while (*p) *q++ = *p++; - p = where + fromlen; - where = strstr(p, from); + if (fromlen > 0) { + p = where + fromlen; + where = strstr(p, from); + } else { + p = where; + where += 1; + } } while (*p) *q++ = *p++; diff --git a/tests/expected/rename/basic b/tests/expected/rename/basic index 4a1809828d..2712ba14b7 100644 --- a/tests/expected/rename/basic +++ b/tests/expected/rename/basic @@ -16,3 +16,4 @@ what is rename_all* *.? doing here? `rename_zz_last_zzz.y' -> `rename_zz_last_zAAzzBB.y' `rename_zz_last_zzz.z' -> `rename_zz_last_zAAzzBB.z' what is rename*last* doing here? +`rename_all_empty' -> `_r_e_n_a_m_e___a_l_l___e_m_p_t_y_' diff --git a/tests/ts/rename/basic b/tests/ts/rename/basic index fb20b49dd3..83ed5a05d7 100755 --- a/tests/ts/rename/basic +++ b/tests/ts/rename/basic @@ -68,4 +68,8 @@ for i in rename*last* ; do echo "what is $i doing here?" >> $TS_OUTPUT done +touch rename_all_empty +$TS_CMD_RENAME -v -a '' _ rename_all_empty >> $TS_OUTPUT 2>> $TS_ERRLOG +rm -f _r_e_n_a_m_e___a_l_l___e_m_p_t_y_ + ts_finalize