summaryrefslogtreecommitdiff
path: root/Base/usr/share/man/man3/basename.md
blob: f670f4129f30edafdbba7b2d53a9fff2ee9ad02c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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";
    dbgln("{}", basename(path1)); // should be "ReadMe.md"

    char path2[] = "foo/bar/";
    dbgln("{}", basename(path2)); // should be "bar"

    char path3[] = "foo";
    dbgln("{}", basename(path3)); // should be "foo"

    char path4[] = "/";
    dbgln("{}", basename(path4)); // should be "/"
}
```

## See also

* [`dirname`(3)](dirname.md)