]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
Fix `rename -a ''`.
authorPhilip Hazelden <philip.hazelden@gmail.com>
Sun, 3 Jul 2022 17:54:43 +0000 (18:54 +0100)
committerPhilip Hazelden <philip.hazelden@gmail.com>
Sun, 3 Jul 2022 22:27:17 +0000 (23:27 +0100)
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_

misc-utils/rename.c
tests/expected/rename/basic
tests/ts/rename/basic

index e47359c0d953c4195bff88b6fe86c2cf03465bda..80a0f31ca4945edcc9636910ff1c9dc89a46a502 100644 (file)
@@ -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++;
index 4a1809828d67ada64a31c9f460c035ac8020ddcc..2712ba14b7fae382e668750d524c85e0c6bcb7cc 100644 (file)
@@ -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_'
index fb20b49dd3090ff1b1bd1bdcaec3eb2441104c9a..83ed5a05d797bc11beec25a411a5ebdc6f25fad1 100755 (executable)
@@ -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