REALPATH(3POSIX)
π realpath(3POSIX) β the POSIX Programmer's Manual, bestie edition π
okay so before we even get into it β this page lives in the POSIX Programmer's Manual. That means the Linux version of this interface might be different (so go read the matching Linux man page for the deets on Linux behavior), or honestly it might not even be implemented on Linux at all. Just so you know going in. π
πΈ 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 β¨
realpath β resolve a pathname π
π SYNOPSIS π
#include <stdlib.h>
char *realpath(const char *restrict file_name,
char *restrict resolved_name);
π¦ DESCRIPTION π¦
so the realpath() function is gonna derive, from the pathname that file_name points to, an absolute pathname that resolves to the same directory entry. and the way that resolution works? no ., no .., no symbolic links. clean. iconic. periodt. β¨
now here's the fork in the road:
- if
resolved_nameis a null pointer, the generated pathname gets stored as a null-terminated string in a buffer that was allocated as if by a call tomalloc(). she's self-sufficient, she allocates her own space. π - otherwise, if
{PATH_MAX}is defined as a constant in the<limits.h>header, then the generated pathname gets stored as a null-terminated string, up to a max of{PATH_MAX}bytes, in the buffer thatresolved_namepoints to.
π¨ okay bestie listen β ifresolved_nameis not a null pointer AND{PATH_MAX}is not defined as a constant in the<limits.h>header, the behavior is undefined. undefined. like, we don't know what happens. do not do this. i'm serious. π
π₯ RETURN VALUE π₯
on a successful completion, realpath() returns a pointer to the buffer containing the resolved name. slay. π
otherwise? realpath() returns a null pointer and sets errno to indicate the error. we love a diagnostic queen.
if the resolved_name argument is a null pointer, the pointer that realpath() returns can be passed to free(). (hold that thought, it comes back in APPLICATION USAGE.)
if the resolved_name argument is not a null pointer and the realpath() function fails, the contents of the buffer that resolved_name points to are undefined. like genuinely unknown. don't look at it. π
π ERRORS π
realpath() shall fail if:
EACCESβ search permission was denied for a component of the path prefix offile_name.EINVALβ thefile_nameargument is a null pointer.EIOβ an error occurred while reading from the file system.ELOOPβ a loop exists in symbolic links encountered during resolution of thefile_nameargument. (a symlink ouroboros. she's eating herself. π)ENAMETOOLONGβ the length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ a component offile_namedoes not name an existing file, orfile_namepoints to an empty string.ENOTDIRβ a component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or thefile_nameargument contains at least one non-<slash>character and ends with one or more trailing<slash>characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
and realpath() may fail if:
EACCESβ thefile_nameargument does not begin with a<slash>and none of the symbolic links (if any) processed during pathname resolution offile_namehad contents that began with a<slash>, and either search permission was denied for the current directory or read or search permission was denied for a directory above the current directory in the file hierarchy.ELOOPβ more than{SYMLOOP_MAX}symbolic links were encountered during resolution of thefile_nameargument.ENAMETOOLONGβ the length of a pathname exceeds{PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds{PATH_MAX}.ENOMEMβ insufficient storage space is available.
π the following sections are informative. not normative. still good to know tho.
π EXAMPLES π
π Generating an Absolute Pathname
the following example generates an absolute pathname for the file identified by the symlinkpath argument. the generated pathname is stored in the buffer pointed to by actualpath. π«Ά
#include <stdlib.h>
...
char *symlinkpath = "/tmp/symlink/file";
char *actualpath;
actualpath = realpath(symlinkpath, NULL);
if (actualpath != NULL)
{
... use actualpath ...
free(actualpath);
}
else
{
... handle error ...
}
π APPLICATION USAGE π
for functions that allocate memory as if by malloc(), the application should release that memory when it's no longer required β by a call to free(). and for realpath(), this is the return value. so yes, you gotta free her. don't ghost your allocations. π§
β RATIONALE β
since realpath() has no length argument, if {PATH_MAX} is not defined as a constant in <limits.h>, applications have literally no way of determining how large a buffer they need to allocate for it to be safe to pass to realpath().
and a {PATH_MAX} value you got from a prior pathconf() call? out-of-date by the time realpath() is called. it's giving expired. π
hence the only reliable way to use realpath() when {PATH_MAX} is not defined in <limits.h> is to pass a null pointer for resolved_name so that realpath() will allocate a buffer of the necessary size. let her cook. π
πΈ FUTURE DIRECTIONS πΈ
None. that's it. nothing. she's complete. π
π¦ SEE ALSO π¦
fpathconf(), free(), getcwd(), sysconf()
The Base Definitions volume of POSIX.1β2017, <limits.h>, <stdlib.h>
π 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 one came from IEEE/The Open Group, 2017 β mwah, thank you for your service, absolute icons. πβ¨
π¦ 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