basename() — Return the last component of a path name

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <libgen.h>

char *basename(char *path);

General description

The basename() function takes the pathname pointed to by path and returns a pointer to the final component of the pathname, deleting any trailing '/' characters.

If the string consists entirely of the '/' character, basename() returns a pointer to the string “/”.

If path is a NULL pointer or points to an empty string, basename() returns a pointer to the string “.”. The basename() function may modify the string pointed to by path.

Examples:
Input String     Output String
"/usr/lib"       "lib"
"/usr/"          "usr"
"/"              "/"

Returned value

If successful, basename() returns a pointer to the final component of path.

There are no errno values defined.

Related information