HP-UX Reference (11i v2 03/08) - 3 Library Functions A-M (vol 6)

b
basename(3C) basename(3C)
NAME
basename( ), dirname( ) - extract components of a path name
SYNOPSIS
#include <libgen.h>
char *basename(char *path);
char *dirname(char *path);
DESCRIPTION
basename() takes the path name pointed to by path and returns a pointer to the final component of the
path name, deleting any trailing ’/’ characters. If the string consists entirely of ’/’ characters,
basename() returns a pointer to the string "/". If path is a null pointer or points to the empty string,
basename() returns a pointer to the string ".".
dirname() takes the path name pointed to by path and returns a pointer to a string that is a path name
of the parent directory of that file. If path is a null pointer, points to the empty string, or does not contain
a ’/’ character, then
dirname() returns a pointer to the string ".".
RETURN VALUE
basename() returns a pointer to the final component of path.
dirname() returns a pointer to a string that is the parent directory of path.
EXAMPLES
The following code fragment calls
basename() and dirname().
#include <libgen.h>
const char *path="/usr/local/bin/foo";
char *base, *dir;
char *dup0, *dup1;
char *dup2, *dup3;
/*Use strdup in case literals
* are in text, or basename()
* and dirname() modify the
* input string.
*/
dup0=strdup(path);
dup1=strdup(path);
base=basename(dup0);
dir=dirname(dup1);
/* Use strdup before modifying
* return string in case a
* pointer to a literal is
* returned.
*/
dup2=strdup(base);
dup3=strdup(dir);
WARNINGS
basename() and dirname() may overwrite path.
basename() and dirname() in non-threaded applications currently use a static buffer in each func-
tion to hold the result string. Any subsequent calls to basename() and dirname() may overwrite the
static buffer. At some release after HP-UX 11i Version 1, HP may use malloc() to allocate the buffers.
Once allocated, those buffers will be reused and their addresses will not change; however, if the mal-
loc() fails, basename() and dirname() would return "." and errno would be set to [ENOMEM].
AUTHOR
basename() and dirname() were developed by HP.
HP-UX 11i Version 2: August 2003 1 Hewlett-Packard Company Section 339