# realpath

## 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



           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.
    


## RETURN VALUE



           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.
    


## ERRORS



           The _realpath_() function shall fail if:
    
           **EACCES** Search permission was denied for a component of the path
                  prefix of _file_name_.
    
           **EINVAL** The _file_name_ argument 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 the _file_name_ argument.
    
           **ENAMETOOLONG**
                  The length of a component of a pathname is longer than
                  {NAME_MAX}.
    
           **ENOENT** A component of _file_name_ does not name an existing file or
                  _file_name_ points 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 the _file_name_ argument 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** The _file_name_ argument does not begin with a <slash> and
                  none of the symbolic links (if any) processed during
                  pathname resolution of _file_name_ had 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 the _file_name_ argument.
    
           **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._
    


## 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 such memory when it is no longer
           required by a call to _free_().  For _realpath_(), this is the return
           value.
    


## 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.
    


## FUTURE DIRECTIONS



           None.
    


* * *