dirname(3G)
NAME
dirname − report the parent directory name of a file path name
SYNOPSIS
cc [ flag ... ] file ... −lgen [ library ... ]
#include <libgen.h>
char ∗dirname(char ∗path);
MT-LEVEL
MT-Safe
DESCRIPTION
Given a pointer to a null-terminated character string that contains a file system path name, dirname() returns a string that is the parent directory of that file. In doing this, it may place a null byte in the path name after the next to last element, so the content of path must be disposable. The returned string should not be deallocated by the caller. Trailing “/” characters in the path are not counted as part of the path.
If path or ∗path is zero, a pointer to a static constant “.” is returned.
dirname() and basename() together yield a complete path name. dirname (path) is the directory where basename (path) is found.
EXAMPLES
A simple file name and the strings “.” and “..” all have “.” as their return value.
Input stringOutput pointer
/usr/lib/usr
/usr//
usr.
//
..
...
The following code reads a path name, changes directory to the parent directory of the named file (see chdir(2)), and opens the file.
char path[100], ∗pathcopy;
int fd;
gets (path);
pathcopy = strdup (path);
chdir (dirname (pathcopy) );
free (pathcopy);
fd = open (basename (path), O_RDONLY);
SEE ALSO
basename(1), chdir(2), basename(3G)
NOTES
When compiling multi-thread applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in multi-thread applications.
SunOS 5.5 — Last change: 22 Jan 1993