Find the part of a string after the last slash (/)
#include <libgen.h> char* basename( char* path );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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:
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.
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 |