---
name: "realpath"
section: "3"
platform: "macos"
package: "Libc"
version: "1752.120.2"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/realpath/3"
---

# realpath(3)

returns the canonicalized absolute pathname

## NAME

**realpath** – returns the canonicalized absolute pathname

## SYNOPSIS

**#include \<stdlib.h\>**

*char \**\

**realpath**(*const char \*restrict file\_name*, *char \*restrict resolved\_name*);

## DESCRIPTION

The **realpath**() function resolves all symbolic links, extra “/” characters, and references to */./* and */../* in *file\_name*. If the *resolved\_name* argument is non-NULL, the resulting absolute pathname is copied there (it *must* refer to a buffer capable of storing at least PATH\_MAX characters).

As a permitted extension to the standard, if *resolved\_name* is NULL,  memory is allocated for the resulting absolute pathname, and is returned by **realpath**(). This memory should be freed by a call to [free(3)](/free/3.md) when no longer needed.

The **realpath**() function will resolve both absolute and relative paths and return the absolute pathname corresponding to *file\_name*. All components of *file\_name* must exist when **realpath**() is called.

## RETURN VALUES

On success, the **realpath**() function returns the address of the resulting absolute pathname, which is *resolved\_name* if it was non-NULL, or the address of newly allocated memory. If an error occurs, **realpath**() returns NULL. If *resolved\_name* was non-NULL, it will contain the pathname which caused the problem.

## VARIANTS

Defining  \_DARWIN\_C\_SOURCE or \_DARWIN\_BETTER\_REALPATH before including stdlib.h will cause the provided implementation of **realpath**() to use F\_GETPATH from [fcntl(2)](/fcntl/2.md) to discover the path.

## ERRORS

The function **realpath**() may fail and set the external variable *errno* for any of the errors specified for the library functions [alloca(3)](/alloca/3.md), [getattrlist(2)](/getattrlist/2.md), [getcwd(3)](/getcwd/3.md), [lstat(2)](/lstat/2.md), [readlink(2)](/readlink/2.md), [stat(2)](/stat/2.md), and [strdup(3)](/strdup/3.md).

## LEGACY SYNOPSIS

**#include \<sys/param.h\>** **#include \<stdlib.h\>**

The include file ⟨sys/param.h⟩ is necessary.

## LEGACY DESCRIPTION

In legacy mode, the last component of *file\_name* does not need to exist when **realpath**() is called.

## SEE ALSO

[free(3)](/free/3.md), [getcwd(3)](/getcwd/3.md), [compat(5)](/compat/5.md)

## HISTORY

The **realpath**() function first appeared in 4.4BSD.
