![]() |
![]() |
![]() |
Return the last component of a pathname
#include <libgen.h> char* basename( char* path );
libc
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:
The basename() function modifies the string pointed to by path, and returns a pointer to static storage.
A pointer to the final component of path.
#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" |
"/" | "/" |
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |
![]() |
![]() |
![]() |