DIRNAME(3POSIX)
π DIRNAME β the "who's your parent directory bestie" function π
okay so real quick before we get into it: this page is part of the POSIX Programmer's Manual (we're in the DIRNAME(3POSIX) zone, section 3, the C-library-flavoured part of POSIX). heads up though β the Linux implementation of this interface may differ, so pls consult the corresponding Linux manual page for the deets on Linux behavior, or honestly the interface might not even be implemented on Linux at all. no cap. π
β¨ PROLOG β¨
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
πΈ NAME πΈ
dirname β report the parent directory name of a file pathname
π SYNOPSIS π
#include <libgen.h>
char *dirname(char *path);
π¦ DESCRIPTION π¦
okay bestie so here's the tea π΅ β the dirname() function shall take a pointer to a character string that contains a pathname, and return a pointer to a string that is a pathname of the parent directory of that file.
now here's the part that lowkey trips people up: dirname() shall not perform pathname resolution. the result shall not be affected by whether or not path exists, or by its file type. like, it does not care. it is not checking. it is simply vibing and slicing strings. βοΈ
also, trailing '/' characters in the path that are not also leading '/' characters shall not be counted as part of the path. periodt.
- If
pathdoes not contain a'/', thendirname()shall return a pointer to the string".". - If
pathis a null pointer or points to an empty string,dirname()shall return a pointer to the string".".
the dirname() function may modify the string pointed to by path, and may return a pointer to static storage that may then be overwritten by a subsequent call to dirname(). so likeβ¦ don't be shocked when it eats your string, she's a little chaotic like that π
β οΈ also dirname() need not be thread-safe. so if you're out here doing multithreaded nonsense, maybe don't, or at least know what you're doing.
π RETURN VALUE π
The dirname() function shall return a pointer to a string as described above. simple. iconic. π
The dirname() function may modify the string pointed to by path, and may return a pointer to internal storage. The returned pointer might be invalidated or the storage might be overwritten by a subsequent call to dirname(). The returned pointer might also be invalidated if the calling thread is terminated.
like fr fr β that pointer is not yours forever, bestie. it's borrowed and it can get yanked out from under you. π«
π ERRORS π
No errors are defined.
The following sections are informative.
π EXAMPLES π
okay so the following code fragment reads a pathname, changes the current working directory to the parent directory, and opens the file. watch her work β¨:
char *path = NULL, *pathcopy;
size_t buflen = 0;
ssize_t linelen = 0;
int fd;
linelen = getline(&path, &buflen, stdin);
path[linelen-1] = 0;
pathcopy = strdup(path);
if (chdir(dirname(pathcopy)) < 0) {
...
}
if ((fd = open(basename(path), O_RDONLY)) >= 0) {
...
close (fd);
}
...
free (pathcopy);
free (path);
the EXAMPLES section of the basename() function (see basename()) includes a table showing examples of the results of processing several sample pathnames by the basename() and dirname() functions and by the basename and dirname utilities. go check it out, it's giving cheat sheet energy π
β APPLICATION USAGE β
The dirname() and basename() functions together yield a complete pathname. The expression dirname(path) obtains the pathname of the directory where basename(path) is found. they're a duo, a package deal, the main characters fr. π«Ά
Since the meaning of the leading "//" is implementation-defined, dirname("//foo) may return either "//" or '/' (but nothing else).
π so like, don't come for the implementation if it picks one or the other. both are valid. it's giving "choose your own adventure" but make it POSIX.
π§ RATIONALE π§
None.
π FUTURE DIRECTIONS π
None.
π₯ SEE ALSO π₯
basename()- The Base Definitions volume of POSIX.1β2017,
<libgen.h> - The Shell and Utilities volume of POSIX.1β2017,
basename,dirname
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie!! π this little beauty comes to you courtesy of IEEE/The Open Group, 2017 (the DIRNAME(3POSIX) energy, you know the vibe). go forth and find your parent directories, you absolute icon. mwah π
β¨π¦
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms