LibC: Implement basename and dirname

This commit is contained in:
Bananymous 2024-12-02 02:39:03 +02:00
parent f4c6afbdae
commit cccb4e6d5e
2 changed files with 70 additions and 0 deletions

View File

@ -10,6 +10,7 @@ set(LIBC_SOURCES
ftw.cpp
grp.cpp
inttypes.cpp
libgen.cpp
locale.cpp
malloc.cpp
math.cpp

View File

@ -0,0 +1,69 @@
#include <libgen.h>
#include <limits.h>
#include <string.h>
char* basename(char* path)
{
static char buffer[PATH_MAX];
constexpr auto prep_string =
[](const char* str) -> char*
{
strcpy(buffer, str);
return buffer;
};
if (path == nullptr || path[0] == '\0')
return prep_string(".");
char* endp = path + strlen(path);
while (endp > path && endp[-1] == '/')
endp--;
if (endp == path)
return prep_string("/");
char* startp = endp;
while (startp > path && startp[-1] != '/')
startp--;
memcpy(buffer, startp, endp - startp);
buffer[endp - startp] = '\0';
return buffer;
}
char* dirname(char* path)
{
static char buffer[PATH_MAX];
constexpr auto prep_string =
[](const char* str) -> char*
{
strcpy(buffer, str);
return buffer;
};
if (path == nullptr || path[0] == '\0')
return prep_string(".");
char* endp = path + strlen(path);
while (endp > path && endp[-1] == '/')
endp--;
if (endp == path)
return prep_string("/");
while (endp > path && endp[-1] != '/')
endp--;
if (endp == path)
return prep_string(".");
while (endp > path && endp[-1] == '/')
endp--;
if (endp == path)
return prep_string("/");
memcpy(buffer, path, endp - path);
buffer[endp - path] = '\0';
return buffer;
}