Reports the parent directory name of a file path name.
#include <libgen.h> char *dirname (char *path);Function Variants This function also has variants named _dirname32 and _dirname64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.
The dirname function returns a pointer to the string "." (dot), when the path argument:
The dirname function can modify the string pointed to by the path argument.
The dirname and basename functions together yield a complete path name. The expression dirname(path) obtains the path name of the directory where basename(path) is found.
See also basename in this section.
x | A pointer to a string that is the parent directory of the path argument. |
"." | The path argument:
|
Using the dirname function, the following example reads a path name, changes the current working directory to the parent directory, and opens a file.
char path [MAXPATHLEN], *pathcopy; int fd; fgets(path, MAXPATHLEN, stdin); pathcopy = strdup(path); chdir(dirname(pathcopy)); fd = open(basename(path), O_RDONLY);