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
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 realpath():
17 _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
18 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
19
21 realpath() expands all symbolic links and resolves references to /./,
22 /../ and extra '/' characters in the null-terminated string named by
23 path to produce a canonicalized absolute pathname. The resulting path‐
24 name is stored as a null-terminated string, up to a maximum of PATH_MAX
25 bytes, in the buffer pointed to by resolved_path. The resulting path
26 will have no symbolic link, /./ or /../ components.
27
28 If resolved_path is specified as NULL, then realpath() uses malloc(3)
29 to allocate a buffer of up to PATH_MAX bytes to hold the resolved path‐
30 name, and returns a pointer to this buffer. The caller should deallo‐
31 cate this buffer using free(3).
32
34 If there is no error, realpath() returns a pointer to the
35 resolved_path.
36
37 Otherwise, it returns a NULL pointer, the contents of the array
38 resolved_path are undefined, and errno is set to indicate the error.
39
41 EACCES Read or search permission was denied for a component of the path
42 prefix.
43
44 EINVAL path is NULL. (In glibc versions before 2.3, this error is also
45 returned if resolved_path is NULL.)
46
47 EIO An I/O error occurred while reading from the file system.
48
49 ELOOP Too many symbolic links were encountered in translating the
50 pathname.
51
52 ENAMETOOLONG
53 A component of a pathname exceeded NAME_MAX characters, or an
54 entire pathname exceeded PATH_MAX characters.
55
56 ENOENT The named file does not exist.
57
58 ENOTDIR
59 A component of the path prefix is not a directory.
60
62 On Linux, this function appeared in libc 4.5.21.
63
65 4.4BSD, POSIX.1-2001.
66
67 POSIX.1-2001 says that the behavior if resolved_path is NULL is imple‐
68 mentation-defined. POSIX.1-2008 specifies the behavior described in
69 this page.
70
72 In 4.4BSD and Solaris, the limit on the pathname length is MAXPATHLEN
73 (found in <sys/param.h>). SUSv2 prescribes PATH_MAX and NAME_MAX, as
74 found in <limits.h> or provided by the pathconf(3) function. A typical
75 source fragment would be
76
77 #ifdef PATH_MAX
78 path_max = PATH_MAX;
79 #else
80 path_max = pathconf(path, _PC_PATH_MAX);
81 if (path_max <= 0)
82 path_max = 4096;
83 #endif
84
85 (But see the BUGS section.)
86
87 The prototype of realpath() is given in <unistd.h> in libc4 and libc5,
88 but in <stdlib.h> everywhere else.
89
90 GNU extensions
91 If the call fails with either EACCES or ENOENT and resolved_path is not
92 NULL, then the prefix of path that is not readable or does not exist is
93 returned in resolved_path.
94
96 The POSIX.1-2001 standard version of this function is broken by design,
97 since it is impossible to determine a suitable size for the output buf‐
98 fer, resolved_path. According to POSIX.1-2001 a buffer of size
99 PATH_MAX suffices, but PATH_MAX need not be a defined constant, and may
100 have to be obtained using pathconf(3). And asking pathconf(3) does not
101 really help, since, on the one hand POSIX warns that the result of
102 pathconf(3) may be huge and unsuitable for mallocing memory, and on the
103 other hand pathconf(3) may return -1 to signify that PATH_MAX is not
104 bounded. The resolved_path == NULL feature, not standardized in
105 POSIX.1-2001, but standardized in POSIX.1-2008, allows this design
106 problem to be avoided.
107
108 The libc4 and libc5 implementation contained a buffer overflow (fixed
109 in libc-5.4.13). Thus, set-user-ID programs like mount(8) needed a
110 private version.
111
113 readlink(2), canonicalize_file_name(3), getcwd(3), pathconf(3),
114 sysconf(3)
115
117 This page is part of release 3.53 of the Linux man-pages project. A
118 description of the project, and information about reporting bugs, can
119 be found at http://www.kernel.org/doc/man-pages/.
120
121
122
123 2013-03-15 REALPATH(3)