REALPATH(3P)
π REALPATH(3P) π β like, the POSIX Programmer's Manual one, bestie
okay so before we even start, a lil heads up that the PROLOG has to give you π
π«Ά 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.
ngl bestie, that last part is the "don't come for me if your box says no" disclaimer. read it, internalize it, move on π
β¨ NAME β¨
realpath β resolve a pathname
like, it takes a messy path and hands you the glow-up version π¦
πΈ SYNOPSIS πΈ
#include <stdlib.h>
char *realpath(const char *restrict file_name,
char *restrict resolved_name);
π DESCRIPTION π
So the realpath() function shall derive, from the pathname pointed to by file_name, an absolute pathname that resolves to the same directory entry, whose resolution does not involve '.', '..', or symbolic links. it's giving "no crumbs left, just the real one" β¨
If resolved_name is a null pointer, the generated pathname shall be stored as a null-terminated string in a buffer allocated as if by a call to malloc(). π
Otherwise, if {PATH_MAX} is defined as a constant in the <limits.h> header, then the generated pathname shall be stored as a null-terminated string, up to a maximum of {PATH_MAX} bytes, in the buffer pointed to by resolved_name.
π¨ Ifresolved_nameis not a null pointer and{PATH_MAX}is not defined as a constant in the<limits.h>header, the behavior is undefined. not "kinda weird", not "probably fine" β undefined. do not pass go, do not collect $200 π
π₯ RETURN VALUE π₯
Upon successful completion, realpath() shall return a pointer to the buffer containing the resolved name. icon behavior. otherwise, realpath() shall return a null pointer and set errno to indicate the error. π
If the resolved_name argument is a null pointer, the pointer returned by realpath() can be passed to free(). π«Ά
If the resolved_name argument is not a null pointer and the realpath() function fails, the contents of the buffer pointed to by resolved_name are undefined. π
π ERRORS π
The realpath() function 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.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.
The realpath() function 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. π (translation: the spicy requirements part is over, this is the vibes-and-context part)
π 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 ...
}
bestie notice the NULL in there β that's the malloc'd-buffer route, and then we free() it because we're polite π
π APPLICATION USAGE π
For functions that allocate memory as if by malloc(), the application should release such memory when it is no longer required by a call to free(). For realpath(), this is the return value.
so likeβ¦ if you got a pointer back and you passed NULL, that's your memory now. free it. don't be that girl π«Ά
π§ RATIONALE π§
Since realpath() has no length argument, if {PATH_MAX} is not defined as a constant in <limits.h>, applications have no way of determining how large a buffer they need to allocate for it to be safe to pass to realpath(). A {PATH_MAX} value obtained from a prior pathconf() call is out-of-date by the time realpath() is called. 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.
fr fr that's the whole tea β β no length arg means you literally cannot size the buffer yourself, so let realpath() do it.
β FUTURE DIRECTIONS β
None.
(it's giving "we're done here, nothing to announce, slay") π
π 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 .
okay that's the whole page, bestie, you ate it πβ¨ this was IEEE/The Open Group, 2017, REALPATH(3P) β now go resolve some pathnames and hydrate ππ¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms