From 6d8d5af345a0053031aef301e4e97c381f8dee29 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 23 Jun 2021 02:03:46 +0800 Subject: [PATCH] libc/dirname: Handle the consecutive '/' correctly Signed-off-by: Xiang Xiao Change-Id: I8387aca067e91724fb4656cd7af59bbd626ceffa --- libs/libc/libgen/lib_dirname.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/libs/libc/libgen/lib_dirname.c b/libs/libc/libgen/lib_dirname.c index a48ebb61d6..2fdfc5b5a7 100644 --- a/libs/libc/libgen/lib_dirname.c +++ b/libs/libc/libgen/lib_dirname.c @@ -94,18 +94,23 @@ FAR char *dirname(FAR char *path) p = strrchr(path, '/'); if (p) { - /* Handle the case where the only '/' in the string is the at the - * beginning of the path. - */ - - if (p == path) + do { - return "/"; + /* Handle the case where the only '/' in the string is the at the + * beginning of the path. + */ + + if (p == path) + { + return "/"; + } + + /* No, the directory component is the substring before the '/'. */ + + *p-- = '\0'; } + while (*p == '/'); - /* No, the directory component is the substring before the '/'. */ - - *p = '\0'; return path; }