United States-English |
|
|
HP-UX Reference > Bbasename(3C)HP-UX 11i Version 3: February 2007 |
|
NAMEbasename(), dirname() — extract components of a path name DESCRIPTIONbasename() 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 VALUEbasename() returns a pointer to the final component of path. dirname() returns a pointer to a string that is the parent directory of path. EXAMPLESThe 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); WARNINGSbasename() and dirname() may overwrite path. basename() and dirname() in non-threaded applications currently use a static buffer in each function 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 malloc() fails, basename() and dirname() would return "." and errno would be set to ENOMEM. |
Printable version | ||
|