]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rename: properly handle directories with trailing slash
authorThomas Weißschuh <thomas@t-8ch.de>
Mon, 4 Dec 2023 23:06:59 +0000 (00:06 +0100)
committerThomas Weißschuh <thomas@t-8ch.de>
Mon, 4 Dec 2023 23:11:24 +0000 (00:11 +0100)
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 <thomas@t-8ch.de>
misc-utils/rename.c
tests/expected/rename/basic
tests/ts/rename/basic

index 66a15833c6d58b3529bf34357334917e6f2a0ee7..9ab38695f2ce03ecbf69c5d36fc02d4755dbf976 100644 (file)
@@ -28,6 +28,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
 #include <errno.h>
 #include <getopt.h>
 #include <fcntl.h>
+#include <libgen.h>
 #include <unistd.h>
 #include <termios.h>
 #include <sys/types.h>
@@ -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);
index 2712ba14b7fae382e668750d524c85e0c6bcb7cc..7f1af7cb3f9c425fe834b1d6174f5277d49be481 100644 (file)
@@ -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'
index 786b0926b8cd42f389028d1eecda4692232871ed..28d1a57b6823e596ebf4f0fff1514de59a1bc417 100755 (executable)
@@ -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