libc/dirname: Handle the consecutive '/' correctly

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I8387aca067e91724fb4656cd7af59bbd626ceffa
This commit is contained in:
Xiang Xiao 2021-06-23 02:03:46 +08:00 committed by Abdelatif Guettouche
parent 5a6e08e281
commit 6d8d5af345
1 changed files with 14 additions and 9 deletions

View File

@ -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;
}