REALPATH(3POSIX) | POSIX Programmer's Manual | REALPATH(3POSIX) |
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.
realpath — resolve a pathname
#include <stdlib.h>
char *realpath(const char *restrict file_name, char *restrict resolved_name);
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. 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.
If resolved_name is not a null pointer and {PATH_MAX} is not defined as a constant in the <limits.h> header, the behavior is undefined.
Upon successful completion, realpath() shall return a pointer to the buffer containing the resolved name. 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.
The realpath() function shall fail if:
The realpath() function may fail if:
The following sections are informative.
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 ... }
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.
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.
None.
fpathconf(), free(), getcwd(), sysconf()
The Base Definitions volume of POSIX.1‐2017, <limits.h>, <stdlib.h>
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 .
2017 | IEEE/The Open Group |