basename()

Find the part of a string after the last slash (/)

Synopsis:

#include <libgen.h>

char* basename( char* path );

Arguments:

path
The string to parse.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

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 NULL pointer, or points to an empty string

Together the dirname() and basename() functions yield a complete pathname. The expression dirname(path) obtains the pathname of the directory where basename(path) is found.

The basename() function might modify the string pointed to by path, and can return 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:

POSIX 1003.1 XSI

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

Last modified: 2013-12-23

comments powered by Disqus