[Previous] [Contents] [Next]

basename()

Return the last component of a pathname

Synopsis:

#include <libgen.h>

char* basename( char* path );

Library:

libc

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.

The basename() function returns:

A pointer to the string "/"
If the string consists entirely of the "/" character
A pointer to the string "."
If path is a NUL pointer or points to any empty string

The basename() function modifies the string pointed to by path, and returns a pointer to static storage.

Returns:

A pointer to the final component of path.

Examples:

#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>

int main( int argc, char** argv )
{
        int x;

        for( x = 1; x < argc; x++ ) {
                printf( "%s\n", basename( argv[x] ) );
        }

        return EXIT_SUCCESS;
}

The table below shows the output of the program, given the input:

Input Output
"/usr/lib" "lib"
"/usr/" "usr"
"/" "/"

Classification:

Standard Unix

Safety:
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes

See also:

dirname()


[Previous] [Contents] [Next]