From patchwork Wed Sep 24 02:47:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Yufen X-Patchwork-Id: 392725 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from whitealder.osuosl.org (whitealder.osuosl.org [140.211.166.138]) by ozlabs.org (Postfix) with ESMTP id EEDDB1400AF for ; Wed, 24 Sep 2014 12:47:21 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 1BBA991F44; Wed, 24 Sep 2014 02:47:21 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7nsQ6QEM2Zz3; Wed, 24 Sep 2014 02:47:20 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by whitealder.osuosl.org (Postfix) with ESMTP id E4A3A91F26; Wed, 24 Sep 2014 02:47:19 +0000 (UTC) X-Original-To: uclibc@lists.busybox.net Delivered-To: uclibc@osuosl.org Received: from whitealder.osuosl.org (whitealder.osuosl.org [140.211.166.138]) by ash.osuosl.org (Postfix) with ESMTP id BBE121C2AC5 for ; Wed, 24 Sep 2014 02:47:17 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id B5A8A91F26 for ; Wed, 24 Sep 2014 02:47:17 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ioO+O6jwn5qb for ; Wed, 24 Sep 2014 02:47:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [119.145.14.64]) by whitealder.osuosl.org (Postfix) with ESMTPS id 9D3D191F22 for ; Wed, 24 Sep 2014 02:47:15 +0000 (UTC) Received: from 172.24.2.119 (EHLO szxeml420-hub.china.huawei.com) ([172.24.2.119]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CCB45656; Wed, 24 Sep 2014 10:47:13 +0800 (CST) Received: from localhost (10.177.25.231) by szxeml420-hub.china.huawei.com (10.82.67.159) with Microsoft SMTP Server id 14.3.158.1; Wed, 24 Sep 2014 10:47:10 +0800 From: Wangyufen To: Subject: [PATCH v2] uclibc:fix basename modify the input path Date: Wed, 24 Sep 2014 10:47:10 +0800 Message-ID: <1411526830-3108-1-git-send-email-wangyufen@huawei.com> X-Mailer: git-send-email 1.8.1.msysgit.1 MIME-Version: 1.0 X-Originating-IP: [10.177.25.231] X-CFilter-Loop: Reflected X-BeenThere: uclibc@uclibc.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "Discussion and development of uClibc \(the embedded C library\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: uclibc-bounces@uclibc.org Sender: "uClibc" From: Wang Yufen I tested basename(path), path is "///", the basename(path) returned "/", but the input path also be modified to "/". It because in basename impl, "last[1] = 0;" modified the input path, I think that isn't correct. This patch fix this problem. Signed-off-by: Wang Yufen --- libc/string/__xpg_basename.c | 54 +++++++++++++++++++++++++++-------------- 1 files changed, 35 insertions(+), 19 deletions(-) diff --git a/libc/string/__xpg_basename.c b/libc/string/__xpg_basename.c index 2e7ade9..4d5a984 100644 --- a/libc/string/__xpg_basename.c +++ b/libc/string/__xpg_basename.c @@ -6,33 +6,49 @@ */ #include - -char *__xpg_basename(register char *path) +#include +char *__xpg_basename (char *path) { - static const char null_or_empty[] = "."; - char *first; - register char *last; - - first = (char *) null_or_empty; + char *p; - if (path && *path) { - first = path; - last = path - 1; + if (path == NULL || path[0] == '\0') + /* We return a pointer to a static string containing ".". */ + p = (char *) "."; + else { + p = strrchr (path, '/'); - do { - if ((*path != '/') && (path > ++last)) { - last = first = path; - } - } while (*++path); + if (p == NULL) + /* There is no slash in the filename. Return the whole string. */ + p = path; + else { + if (p[1] == '\0') { + /* We must remove trailing '/'. */ + while (p > path && p[-1] == '/') + --p; - if (*first == '/') { - last = first; + /* Now we can be in two situations: + a) the string only contains '/' characters, so we return + '/' + b) p points past the last component, but we have to remove + the trailing slash. */ + if (p > path) { + *p-- = '\0'; + while (p > path && p[-1] != '/') + --p; + } else + /* The last slash we already found is the right position + to return. */ + while (p[1] != '\0') + ++p; + } else + /* Go to the first character of the name. */ + ++p; } - last[1] = 0; } - return first; + return p; } + #ifndef __USE_GNU # undef basename weak_alias(__xpg_basename,basename)