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 *restrict path,
13 char *restrict resolved_path);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 realpath():
18 _XOPEN_SOURCE >= 500
19 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
20 || /* Glibc <= 2.19: */ _BSD_SOURCE
21
23 realpath() expands all symbolic links and resolves references to /./,
24 /../ and extra '/' characters in the null-terminated string named by
25 path to produce a canonicalized absolute pathname. The resulting path‐
26 name is stored as a null-terminated string, up to a maximum of PATH_MAX
27 bytes, in the buffer pointed to by resolved_path. The resulting path
28 will have no symbolic link, /./ or /../ components.
29
30 If resolved_path is specified as NULL, then realpath() uses malloc(3)
31 to allocate a buffer of up to PATH_MAX bytes to hold the resolved path‐
32 name, and returns a pointer to this buffer. The caller should deallo‐
33 cate this buffer using free(3).
34
36 If there is no error, realpath() returns a pointer to the re‐
37 solved_path.
38
39 Otherwise, it returns NULL, the contents of the array resolved_path are
40 undefined, and errno is set to indicate the error.
41
43 EACCES Read or search permission was denied for a component of the path
44 prefix.
45
46 EINVAL path is NULL. (In glibc versions before 2.3, this error is also
47 returned if resolved_path is NULL.)
48
49 EIO An I/O error occurred while reading from the filesystem.
50
51 ELOOP Too many symbolic links were encountered in translating the
52 pathname.
53
54 ENAMETOOLONG
55 A component of a pathname exceeded NAME_MAX characters, or an
56 entire pathname exceeded PATH_MAX characters.
57
58 ENOENT The named file does not exist.
59
60 ENOMEM Out of memory.
61
62 ENOTDIR
63 A component of the path prefix is not a directory.
64
66 For an explanation of the terms used in this section, see at‐
67 tributes(7).
68
69 ┌────────────────────────────────────────────┬───────────────┬─────────┐
70 │Interface │ Attribute │ Value │
71 ├────────────────────────────────────────────┼───────────────┼─────────┤
72 │realpath() │ Thread safety │ MT-Safe │
73 └────────────────────────────────────────────┴───────────────┴─────────┘
74
76 4.4BSD, POSIX.1-2001.
77
78 POSIX.1-2001 says that the behavior if resolved_path is NULL is imple‐
79 mentation-defined. POSIX.1-2008 specifies the behavior described in
80 this page.
81
83 In 4.4BSD and Solaris, the limit on the pathname length is MAXPATHLEN
84 (found in <sys/param.h>). SUSv2 prescribes PATH_MAX and NAME_MAX, as
85 found in <limits.h> or provided by the pathconf(3) function. A typical
86 source fragment would be
87
88 #ifdef PATH_MAX
89 path_max = PATH_MAX;
90 #else
91 path_max = pathconf(path, _PC_PATH_MAX);
92 if (path_max <= 0)
93 path_max = 4096;
94 #endif
95
96 (But see the BUGS section.)
97
98 GNU extensions
99 If the call fails with either EACCES or ENOENT and resolved_path is not
100 NULL, then the prefix of path that is not readable or does not exist is
101 returned in resolved_path.
102
104 The POSIX.1-2001 standard version of this function is broken by design,
105 since it is impossible to determine a suitable size for the output buf‐
106 fer, resolved_path. According to POSIX.1-2001 a buffer of size
107 PATH_MAX suffices, but PATH_MAX need not be a defined constant, and may
108 have to be obtained using pathconf(3). And asking pathconf(3) does not
109 really help, since, on the one hand POSIX warns that the result of
110 pathconf(3) may be huge and unsuitable for mallocing memory, and on the
111 other hand pathconf(3) may return -1 to signify that PATH_MAX is not
112 bounded. The resolved_path == NULL feature, not standardized in
113 POSIX.1-2001, but standardized in POSIX.1-2008, allows this design
114 problem to be avoided.
115
117 realpath(1), readlink(2), canonicalize_file_name(3), getcwd(3), path‐
118 conf(3), sysconf(3)
119
121 This page is part of release 5.12 of the Linux man-pages project. A
122 description of the project, information about reporting bugs, and the
123 latest version of this page, can be found at
124 https://www.kernel.org/doc/man-pages/.
125
126
127
128 2021-03-22 REALPATH(3)