dirname() — Report the parent directory of a path name

Standards

Standards / Extensions C or C++ Dependencies
XPG4.2
Single UNIX Specification, Version 3
both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <libgen.h>

char *dirname(char *path);

General description

The dirname() function takes a pointer to a character string that contains a path name, and returns a pointer to a string that is a path name of the parent directory of that file. Trailing '/' characters in the path are not counted as part of the path.

If path does not contain a '/' then dirname() returns a pointer to the string ".". If path is a NULL pointer or points to an empty string, dirname() returns a pointer to the string ".".

The dirname() function may modify the string pointed to by path.

Examples:
Input String       Output String
"/usr/lib"         "/usr"
"/usr/"            "/"
"usr"              "."
"/"                "/"
"."                "."
".."               "."

Returned value

If successful, dirname() returns a pointer to a string that is the parent directory of path.

If path is a NULL pointer or points to an empty string, dirname() returns a pointer to a string ".".

There are no errno values defined.

Related information