1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:37:35 +00:00

LibC: Implement dirname() and basename()

And write section 3 man pages for them.
This commit is contained in:
Sergey Bugaev 2019-10-02 22:40:52 +03:00 committed by Andreas Kling
parent 8fbcfa934a
commit afdc5688ec
5 changed files with 177 additions and 1 deletions

View file

@ -0,0 +1,53 @@
## Name
basename - extract file name from a path
## Synopsis
```**c++
#include <libgen.h>
char* basename(char* path);
```
## Description
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()`.
## Return value
`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.
## Examples
```c++
#include <AK/LogStream.h>
#include <libgen.h>
int main()
{
char path1[] = "/home/anon/ReadMe.md";
dbg() << basename(path1); // should be "ReadMe.md"
char path2[] = "foo/bar/";
dbg() << basename(path2); // should be "bar"
char path3[] = "foo";
dbg() << basename(path3); // should be "foo"
char path4[] = "/";
dbg() << basename(path4); // should be "/"
}
```
## See also
* [`dirname`(3)](dirname.md)

View file

@ -0,0 +1,54 @@
## Name
dirname - get a file's containing directory path
## Synopsis
```**c++
#include <libgen.h>
char* dirname(char* path);
```
## Description
Given a file path, `dirname()` returns a path to the directory that contains the
file. `dirname()` works purely lexically, meaning it only manipulates the path
as a string, and does not check if such a file or its containing directory
actually exist.
A call to `dirname()` may reuse and modify the passed in `path` buffer. Do not
expect it to have the same value after calling `dirname()`.
## Return value
`dirname()` returns the directory path 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.
## Examples
```c++
#include <AK/LogStream.h>
#include <libgen.h>
int main()
{
char path1[] = "/home/anon/ReadMe.md";
dbg() << dirname(path1); // should be "/home/anon"
char path2[] = "foo/bar/";
dbg() << dirname(path2); // should be "foo"
char path3[] = "foo";
dbg() << dirname(path3); // should be "."
char path4[] = "/";
dbg() << dirname(path4); // should be "/"
}
```
## See also
* [`basename`(3)](basename.md)