Browse Source

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_
pull/1741/head
Philip Hazelden 1 month ago
parent
commit
2285629d7d
  1. 13
      misc-utils/rename.c
  2. 1
      tests/expected/rename/basic
  3. 4
      tests/ts/rename/basic

13
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++;

1
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_'

4
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

Loading…
Cancel
Save