Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

dirname - parent directory of pathname

&pagelevel(4)&pagelevel

Syntax

#include <libgen.h>

char *dirname(char *path);

Description

dirname() determines the parent directory of the pathname to which *path points, and
returns a pointer to a string which contains the name of this parent directory or the string ".".
Trailing slashes (/) at the end of the pathname are not interpreted as part of the path.

If path does not contain a slash, dirname() returns a pointer to the "." string.
If path is a null pointer or points to an empty string, dirname() likewise returns a pointer to
the "." string.
dirname() is not reentrant.

Return val.

pointer to the name of the parent directory

If path does contain a slash.

pointer to "." string

If path does not contain a slash, is a null pointer or points to an empty string.

Example

Input value in path                                 

Return value

"/usr/lib"

"/usr"

"/usr/"

"/"

"usr"

"."

"/"

"/"

"."

"."

".."

"."

The following code fragment reads a pathname, makes the parent directory into the current
working directory, and opens the file:

char path(MAXPATHLEN), *pathcopy;
int fd;
fgets(path, MAXPATHLEN, stdin);
pathcopy = strup(path);
chdir(dirname(pathcopy));
fd = open(basename(path), O_RDONLY);

Notes

dirname() can change the path string. The return value of dirname() can point to a static
area that is overwritten by a subsequent dirname() call.

dirname() and basename() together produce a complete pathname. dirname(path)
determines the pathname of the directory in which basename(path) resides.

See also

basename(), libgen.h.