1REALPATH(3)                Linux Programmer's Manual               REALPATH(3)
2
3
4

NAME

6       realpath - return the canonicalized absolute pathname
7

SYNOPSIS

9       #include <limits.h>
10       #include <stdlib.h>
11
12       char *realpath(const char *path, char *resolved_path);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       realpath(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
17

DESCRIPTION

19       realpath()  expands  all symbolic links and resolves references to /./,
20       /../ and extra '/' characters in the null-terminated  string  named  by
21       path to produce a canonicalized absolute pathname.  The resulting path‐
22       name is stored as a null-terminated string, up to a maximum of PATH_MAX
23       bytes,  in  the buffer pointed to by resolved_path.  The resulting path
24       will have no symbolic link, /./ or /../ components.
25
26       If resolved_path is specified as NULL, then realpath()  uses  malloc(3)
27       to allocate a buffer of up to PATH_MAX bytes to hold the resolved path‐
28       name, and returns a pointer to this buffer.  The caller should  deallo‐
29       cate this buffer using free(3).
30

RETURN VALUE

32       If   there   is   no   error,  realpath()  returns  a  pointer  to  the
33       resolved_path.
34
35       Otherwise it returns a NULL pointer, and  the  contents  of  the  array
36       resolved_path are undefined, and errno is set to indicate the error.
37

ERRORS

39       EACCES Read or search permission was denied for a component of the path
40              prefix.
41
42       EINVAL Either path or resolved_path is NULL.  (In libc5 this would just
43              cause a segfault.)  But, see NOTES below.
44
45       EIO    An I/O error occurred while reading from the file system.
46
47       ELOOP  Too  many  symbolic  links  were  encountered in translating the
48              pathname.
49
50       ENAMETOOLONG
51              A component of a pathname exceeded NAME_MAX  characters,  or  an
52              entire pathname exceeded PATH_MAX characters.
53
54       ENOENT The named file does not exist.
55
56       ENOTDIR
57              A component of the path prefix is not a directory.
58

VERSIONS

60       On Linux this function appeared in libc 4.5.21.
61

CONFORMING TO

63       4.4BSD, POSIX.1-2001.
64
65       POSIX.1-2001  says that the behavior if resolved_path is NULL is imple‐
66       mentation-defined.  POSIX.1-2008 specifies the  behavior  described  in
67       this page.
68

NOTES

70       In  4.4BSD  and  Solaris the limit on the pathname length is MAXPATHLEN
71       (found in <sys/param.h>).  SUSv2 prescribes PATH_MAX and  NAME_MAX,  as
72       found in <limits.h> or provided by the pathconf(3) function.  A typical
73       source fragment would be
74
75           #ifdef PATH_MAX
76             path_max = PATH_MAX;
77           #else
78             path_max = pathconf(path, _PC_PATH_MAX);
79             if (path_max <= 0)
80               path_max = 4096;
81           #endif
82
83       (But see the BUGS section.)
84
85       The 4.4BSD, Linux and SUSv2 versions always return  an  absolute  path‐
86       name.  Solaris may return a relative pathname when the path argument is
87       relative.  The prototype of realpath() is given in <unistd.h> in  libc4
88       and libc5, but in <stdlib.h> everywhere else.
89

BUGS

91       The POSIX.1-2001 standard version of this function is broken by design,
92       since it is impossible to determine a suitable size for the output buf‐
93       fer,  resolved_path.   According  to  POSIX.1-2001  a  buffer  of  size
94       PATH_MAX suffices, but PATH_MAX need not be a defined constant, and may
95       have to be obtained using pathconf(3).  And asking pathconf(3) does not
96       really help, since, on the one hand POSIX  warns  that  the  result  of
97       pathconf(3) may be huge and unsuitable for mallocing memory, and on the
98       other hand pathconf(3) may return -1 to signify that  PATH_MAX  is  not
99       bounded.    The  resolved_path == NULL  feature,  not  standardized  in
100       POSIX.1-2001, but standardized  in  POSIX.1-2008,  allows  this  design
101       problem to be avoided.
102
103       The libc4 and libc5 implementation contains a buffer overflow (fixed in
104       libc-5.4.13).  Thus, set-user-ID programs like mount(8) need a  private
105       version.
106

SEE ALSO

108       readlink(2),    canonicalize_file_name(3),    getcwd(3),   pathconf(3),
109       sysconf(3)
110

COLOPHON

112       This page is part of release 3.25 of the Linux  man-pages  project.   A
113       description  of  the project, and information about reporting bugs, can
114       be found at http://www.kernel.org/doc/man-pages/.
115
116
117
118                                  2009-02-23                       REALPATH(3)
Impressum