realpath()

Updated: April 19, 2023

Resolve a pathname

Synopsis:

#include <stdlib.h>

char * realpath( const char * pathname,
                 char * resolved_name );

Arguments:

pathname
The pathname that you want to resolve.
resolved_name
NULL, or a pointer to a buffer where the function can store the resolved name.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The realpath() function resolves all symbolic links, extra slash (/) characters and references to /./ and /../ in pathname, and copies the resulting null-terminated absolute pathname into the memory referenced by resolved_name.

Use one of the following methods to specify the buffer for the resolved name:
  • Pass NULL for resolved_name to make realpath() allocate an appropriately sized buffer. You're responsible for freeing this memory by calling free().
  • Specify a pointer to a buffer that is at least PATH_MAX in size.

This function resolves both absolute and relative paths and returns the absolute pathname corresponding to pathname. All components of the pathname must exist when you call realpath().

Returns:

A pointer to resolved_name, or NULL if an error occurred, in which case resolved_name contains the pathname that caused the problem.

Errors:

The realpath() function may fail and set errno to any of the errors specified for chdir(), close(), fchdir(), lstat(), open(), readlink() and getcwd().

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes