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