basename - extract file name from a path
#include <libgen.h>
char* basename(char* path);
Given a file path, basename()
returns that file's name. basename()
works purely lexically, meaning it only manipulates the path as a string, and does not check if such a file actually exists.
A call to basename()
may reuse and modify the passed in path
buffer. Do not expect it to have the same value after calling basename()
.
basename()
returns the file name as a string. This string may be allocated in static memory, or it may point to some part of the original path
buffer. Do not free()
the returned string, and do not free()
the original path
buffer while using the returned string.
#include <AK/LogStream.h>
#include <libgen.h>
int main()
{
char path1[] = "/home/anon/README.md";
("{}", basename(path1)); // should be "README.md"
dbgln
char path2[] = "foo/bar/";
("{}", basename(path2)); // should be "bar"
dbgln
char path3[] = "foo";
("{}", basename(path3)); // should be "foo"
dbgln
char path4[] = "/";
("{}", basename(path4)); // should be "/"
dbgln}