From: Thomas Weißschuh Date: Mon, 4 Dec 2023 23:06:59 +0000 (+0100) Subject: rename: properly handle directories with trailing slash X-Git-Tag: v2.40-rc1~133^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7b67193a53209907f84a6253dcf3aceaf2eb8da1;p=thirdparty%2Futil-linux.git rename: properly handle directories with trailing slash Instead of hand-rolling a basename() function use the standard one which provides the functionality that we want to use. Also add a testcase. Fixes #2569 Signed-off-by: Thomas Weißschuh --- diff --git a/misc-utils/rename.c b/misc-utils/rename.c index 66a15833c6..9ab38695f2 100644 --- a/misc-utils/rename.c +++ b/misc-utils/rename.c @@ -28,6 +28,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done #include #include #include +#include #include #include #include @@ -58,12 +59,7 @@ static char *find_initial_replace(char *from, char *to, char *orig) /* We only want to search in the final path component. Don't include the final '/' in that component; if `from` is empty, we want it to first match after the '/', not before. */ - search_start = strrchr(orig, '/'); - - if (search_start == NULL) - search_start = orig; - else - search_start++; + search_start = basename(orig); } return strstr(search_start, from); diff --git a/tests/expected/rename/basic b/tests/expected/rename/basic index 2712ba14b7..7f1af7cb3f 100644 --- a/tests/expected/rename/basic +++ b/tests/expected/rename/basic @@ -17,3 +17,4 @@ what is rename_all* *.? doing here? `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_' +`rename_dir' -> `rename_new_dir' diff --git a/tests/ts/rename/basic b/tests/ts/rename/basic index 786b0926b8..28d1a57b68 100755 --- a/tests/ts/rename/basic +++ b/tests/ts/rename/basic @@ -72,4 +72,8 @@ 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_ +mkdir rename_dir +$TS_CMD_RENAME -v dir new_dir rename_dir/ >> $TS_OUTPUT 2>> $TS_ERRLOG +rmdir rename_new_dir + ts_finalize