1REALPATH(3) Linux Programmer's Manual REALPATH(3)
2
3
4
6 realpath - return the canonicalized absolute pathname
7
9 #include <limits.h>
10 #include <stdlib.h>
11
12 char *realpath(const char *path, char *resolved_path);
13
15 realpath() expands all symbolic links and resolves references to '/./',
16 '/../' and extra '/' characters in the null terminated string named by
17 path and stores the canonicalized absolute pathname in the buffer of
18 size PATH_MAX named by resolved_path. The resulting path will have no
19 symbolic link, '/./' or '/../' components.
20
22 If there is no error, realpath() returns a pointer to the
23 resolved_path.
24
25 Otherwise it returns a NULL pointer, and the contents of the array
26 resolved_path are undefined. The global variable errno is set to indi‐
27 cate the error.
28
30 EACCES Read or search permission was denied for a component of the path
31 prefix.
32
33 EINVAL Either path or resolved_path is NULL. (In libc5 this would just
34 cause a segfault.) But, see NOTES below.
35
36 EIO An I/O error occurred while reading from the file system.
37
38 ELOOP Too many symbolic links were encountered in translating the
39 pathname.
40
41 ENAMETOOLONG
42 A component of a pathname exceeded NAME_MAX characters, or an
43 entire pathname exceeded PATH_MAX characters.
44
45 ENOENT The named file does not exist.
46
47 ENOTDIR
48 A component of the path prefix is not a directory.
49
51 The glibc implementation of realpath() provides a non-standard exten‐
52 sion. If resolved_path is specified as NULL, then realpath() uses mal‐
53 loc(3) to allocate a buffer of up to PATH_MAX bytes to hold the
54 resolved pathname, and returns a pointer to this buffer. The caller
55 should deallocate this buffer using free(3).
56
58 Avoid using this function. It is broken by design since (unless using
59 the non-standard resolved_path == NULL feature) it is impossible to
60 determine a suitable size for the output buffer, resolved_path.
61 According to POSIX a buffer of size PATH_MAX suffices, but PATH_MAX
62 need not be a defined constant, and may have to be obtained using path‐
63 conf(). And asking pathconf() does not really help, since on the one
64 hand POSIX warns that the result of pathconf() may be huge and unsuit‐
65 able for mallocing memory. And on the other hand pathconf() may return
66 -1 to signify that PATH_MAX is not bounded.
67
68 The libc4 and libc5 implementation contains a buffer overflow (fixed in
69 libc-5.4.13). Thus, set-user-ID programs like mount need a private
70 version.
71
73 The realpath() function first appeared in 4.4BSD, contributed by Jan-
74 Simon Pendry. In Linux this function appears in libc 4.5.21.
75
77 4.4BSD, POSIX.1-2001.
78
79 In 4.4BSD and Solaris the limit on the pathname length is MAXPATHLEN
80 (found in <sys/param.h>). SUSv2 prescribes PATH_MAX and NAME_MAX, as
81 found in <limits.h> or provided by the pathconf() function. A typical
82 source fragment would be
83
84 #ifdef PATH_MAX
85 path_max = PATH_MAX;
86 #else
87 path_max = pathconf (path, _PC_PATH_MAX);
88 if (path_max <= 0)
89 path_max = 4096;
90 #endif
91 (But see the BUGS section.)
92
93 The 4.4BSD, Linux and SUSv2 versions always return an absolute path‐
94 name. Solaris may return a relative pathname when the path argument is
95 relative. The prototype of realpath() is given in <unistd.h> in libc4
96 and libc5, but in <stdlib.h> everywhere else.
97
99 readlink(2), canonicalize_file_name(3), getcwd(3), pathconf(3),
100 sysconf(3)
101
102
103
104 2004-12-14 REALPATH(3)