From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: util-linux-owner@vger.kernel.org Received: from mail-wg0-f51.google.com ([74.125.82.51]:61071 "EHLO mail-wg0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751092AbaEYV62 (ORCPT ); Sun, 25 May 2014 17:58:28 -0400 Received: by mail-wg0-f51.google.com with SMTP id x13so6985089wgg.34 for ; Sun, 25 May 2014 14:58:27 -0700 (PDT) From: Sami Kerola To: util-linux@vger.kernel.org Cc: kerolasa@iki.fi Subject: [PATCH 1/2] rename: allow renaming in subdirectories Date: Sun, 25 May 2014 22:58:22 +0100 Message-Id: <1401055103-28558-1-git-send-email-kerolasa@iki.fi> Sender: util-linux-owner@vger.kernel.org List-ID: Earlier the rename(1) considered path as possible string to be renamed, could lead to an issue with none existing destination. See below for demonstration of this issue. After this change all directory elements are ignored when the match finding happens. $ cd $(mktemp -d) $ mkdir aa ab $ touch a{a,b}/aa $ rename -v a x */aa rename: aa/aa: rename to xa/aa failed: No such file or directory Signed-off-by: Sami Kerola --- misc-utils/rename.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/misc-utils/rename.c b/misc-utils/rename.c index 9db85fb..b945aaa 100644 --- a/misc-utils/rename.c +++ b/misc-utils/rename.c @@ -46,9 +46,14 @@ static int do_rename(char *from, char *to, char *s, int verbose, int symtarget) target[sb.st_size] = '\0'; where = strstr(target, from); - } else - where = strstr(s, from); + } else { + char *file; + file = rindex(s, '/'); + if (file == NULL) + file = s; + where = strstr(file, from); + } if (where == NULL) { free(target); return 0; -- 1.9.3