]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (C) 1999 Andries Brouwer (aeb@cwi.nl) |
2 | .\" | |
3 | .\" Permission is granted to make and distribute verbatim copies of this | |
4 | .\" manual provided the copyright notice and this permission notice are | |
5 | .\" preserved on all copies. | |
6 | .\" | |
7 | .\" Permission is granted to copy and distribute modified versions of this | |
8 | .\" manual under the conditions for verbatim copying, provided that the | |
9 | .\" entire resulting derived work is distributed under the terms of a | |
10 | .\" permission notice identical to this one. | |
c13182ef | 11 | .\" |
fea681da MK |
12 | .\" Since the Linux kernel and libraries are constantly changing, this |
13 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
14 | .\" responsibility for errors or omissions, or for damages resulting from | |
15 | .\" the use of the information contained herein. The author(s) may not | |
16 | .\" have taken the same level of care in the production of this manual, | |
17 | .\" which is licensed free of charge, as they might when working | |
18 | .\" professionally. | |
c13182ef | 19 | .\" |
fea681da MK |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" | |
23 | .\" Rewritten old page, 990824, aeb@cwi.nl | |
7841ad47 | 24 | .\" 2004-12-14, mtk, added discussion of resolved_path == NULL |
fea681da | 25 | .\" |
bf42aad4 | 26 | .TH REALPATH 3 2012-08-14 "" "Linux Programmer's Manual" |
fea681da MK |
27 | .SH NAME |
28 | realpath \- return the canonicalized absolute pathname | |
29 | .SH SYNOPSIS | |
30 | .nf | |
31 | .B #include <limits.h> | |
32 | .B #include <stdlib.h> | |
33 | .sp | |
c13182ef | 34 | .BI "char *realpath(const char *" path ", char *" resolved_path ); |
cc4615cc MK |
35 | .fi |
36 | .sp | |
37 | .in -4n | |
38 | Feature Test Macro Requirements for glibc (see | |
39 | .BR feature_test_macros (7)): | |
40 | .in | |
41 | .sp | |
42 | .BR realpath (): | |
a440dd9b MK |
43 | .ad l |
44 | .RS 4 | |
45 | _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || | |
46 | _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED | |
47 | .RE | |
48 | .ad | |
fea681da | 49 | .SH DESCRIPTION |
7841ad47 | 50 | .BR realpath () |
fea681da MK |
51 | expands all symbolic links and resolves references |
52 | to | |
f8a07a21 | 53 | .IR "/./" ", " "/../" |
f81fb444 | 54 | and extra \(aq/\(aq |
d0cb7cc6 | 55 | characters in the null-terminated string named by |
fea681da | 56 | .I path |
bf0e2b8a MK |
57 | to produce a canonicalized absolute pathname. |
58 | The resulting pathname is stored as a null-terminated string, | |
59 | up to a maximum of | |
fea681da | 60 | .B PATH_MAX |
5a3b4036 | 61 | bytes, |
bf0e2b8a | 62 | in the buffer pointed to by |
fea681da MK |
63 | .IR resolved_path . |
64 | The resulting path will have no symbolic link, | |
f8a07a21 | 65 | .I "/./" |
fea681da | 66 | or |
f8a07a21 | 67 | .I "/../" |
fea681da | 68 | components. |
55e80e25 MK |
69 | |
70 | If | |
71 | .I resolved_path | |
72 | is specified as NULL, then | |
73 | .BR realpath () | |
74 | uses | |
75 | .BR malloc (3) | |
76 | to allocate a buffer of up to | |
77 | .B PATH_MAX | |
78 | bytes to hold the resolved pathname, | |
79 | and returns a pointer to this buffer. | |
80 | The caller should deallocate this buffer using | |
81 | .BR free (3). | |
82 | .\" Even if we use resolved_path == NULL, then realpath() will still | |
83 | .\" return ENAMETOOLONG if the resolved pathname would exceed PATH_MAX | |
84 | .\" bytes -- MTK, Dec 04 | |
85 | .\" .SH HISTORY | |
86 | .\" The | |
87 | .\" .BR realpath () | |
88 | .\" function first appeared in 4.4BSD, contributed by Jan-Simon Pendry. | |
fea681da | 89 | .SH "RETURN VALUE" |
7841ad47 MK |
90 | If there is no error, |
91 | .BR realpath () | |
92 | returns a pointer to the | |
fea681da MK |
93 | .IR resolved_path . |
94 | ||
95 | Otherwise it returns a NULL pointer, and the contents | |
96 | of the array | |
c13182ef | 97 | .I resolved_path |
28d03ce9 | 98 | are undefined, and |
c13182ef MK |
99 | .I errno |
100 | is set to indicate the error. | |
fea681da MK |
101 | .SH ERRORS |
102 | .TP | |
103 | .B EACCES | |
104 | Read or search permission was denied for a component of the path prefix. | |
105 | .TP | |
106 | .B EINVAL | |
fea681da | 107 | .I path |
6387216b | 108 | is NULL. |
536bc966 MK |
109 | .\" (In libc5 this would just cause a segfault.) |
110 | (In glibc versions before 2.3, | |
111 | this error is also returned if | |
112 | .IR resolved_path | |
113 | is NULL.) | |
fea681da MK |
114 | .TP |
115 | .B EIO | |
116 | An I/O error occurred while reading from the file system. | |
117 | .TP | |
118 | .B ELOOP | |
119 | Too many symbolic links were encountered in translating the pathname. | |
120 | .TP | |
121 | .B ENAMETOOLONG | |
c13182ef | 122 | A component of a pathname exceeded |
fea681da | 123 | .B NAME_MAX |
c13182ef | 124 | characters, or an entire pathname exceeded |
fea681da MK |
125 | .B PATH_MAX |
126 | characters. | |
127 | .TP | |
128 | .B ENOENT | |
129 | The named file does not exist. | |
130 | .TP | |
131 | .B ENOTDIR | |
132 | A component of the path prefix is not a directory. | |
2b2581ee MK |
133 | .SH VERSIONS |
134 | On Linux this function appeared in libc 4.5.21. | |
135 | .SH "CONFORMING TO" | |
136 | 4.4BSD, POSIX.1-2001. | |
137 | ||
c49e8fc9 | 138 | POSIX.1-2001 says that the behavior if |
55e80e25 MK |
139 | .I resolved_path |
140 | is NULL is implementation-defined. | |
141 | POSIX.1-2008 specifies the behavior described in this page. | |
c49e8fc9 | 142 | .SH NOTES |
2f0af33b MK |
143 | In 4.4BSD and Solaris the limit on the pathname length is |
144 | .B MAXPATHLEN | |
c84371c6 | 145 | (found in \fI<sys/param.h>\fP). |
2f0af33b | 146 | SUSv2 prescribes |
0daa9e92 | 147 | .B PATH_MAX |
2f0af33b MK |
148 | and |
149 | .BR NAME_MAX , | |
150 | as found in \fI<limits.h>\fP or provided by the | |
2b2581ee MK |
151 | .BR pathconf (3) |
152 | function. | |
153 | A typical source fragment would be | |
154 | .LP | |
a6e2f128 | 155 | .in +4n |
2b2581ee MK |
156 | .nf |
157 | #ifdef PATH_MAX | |
158 | path_max = PATH_MAX; | |
159 | #else | |
160 | path_max = pathconf(path, _PC_PATH_MAX); | |
161 | if (path_max <= 0) | |
162 | path_max = 4096; | |
163 | #endif | |
164 | .fi | |
a6e2f128 | 165 | .in |
d2d293ba | 166 | .LP |
2b2581ee MK |
167 | (But see the BUGS section.) |
168 | .LP | |
8f826cd8 MK |
169 | .\" 2012-05-05, According to Casper Dik, the statement about |
170 | .\" Solaris was not true at least as far back as 1997, and | |
171 | .\" may never have been true. | |
172 | .\" | |
173 | .\" The 4.4BSD, Linux and SUSv2 versions always return an absolute | |
174 | .\" pathname. | |
175 | .\" Solaris may return a relative pathname when the | |
176 | .\" .I path | |
177 | .\" argument is relative. | |
2b2581ee MK |
178 | The prototype of |
179 | .BR realpath () | |
c84371c6 MK |
180 | is given in \fI<unistd.h>\fP in libc4 and libc5, |
181 | but in \fI<stdlib.h>\fP everywhere else. | |
fea681da | 182 | .SH BUGS |
c49e8fc9 MK |
183 | The POSIX.1-2001 standard version of this function is broken by design, |
184 | since it is impossible to determine a suitable size for the output buffer, | |
7841ad47 | 185 | .IR resolved_path . |
c49e8fc9 | 186 | According to POSIX.1-2001 a buffer of size |
2f0af33b MK |
187 | .B PATH_MAX |
188 | suffices, but | |
189 | .B PATH_MAX | |
190 | need not be a defined constant, and may have to be obtained using | |
fb186734 | 191 | .BR pathconf (3). |
fea681da | 192 | And asking |
fb186734 | 193 | .BR pathconf (3) |
c49e8fc9 | 194 | does not really help, since, on the one hand POSIX warns that |
fea681da | 195 | the result of |
fb186734 | 196 | .BR pathconf (3) |
c49e8fc9 MK |
197 | may be huge and unsuitable for mallocing memory, |
198 | and on the other hand | |
fb186734 | 199 | .BR pathconf (3) |
2f0af33b MK |
200 | may return \-1 to signify that |
201 | .B PATH_MAX | |
202 | is not bounded. | |
c49e8fc9 MK |
203 | The |
204 | .I "resolved_path\ ==\ NULL" | |
205 | feature, not standardized in POSIX.1-2001, | |
206 | but standardized in POSIX.1-2008, allows this design problem to be avoided. | |
fea681da | 207 | .LP |
dbe8b62b | 208 | The libc4 and libc5 implementation contained a buffer overflow |
fea681da | 209 | (fixed in libc-5.4.13). |
d9a10d9d MK |
210 | Thus, set-user-ID programs like |
211 | .BR mount (8) | |
dbe8b62b | 212 | needed a private version. |
fea681da MK |
213 | .SH "SEE ALSO" |
214 | .BR readlink (2), | |
a8431b7b | 215 | .BR canonicalize_file_name (3), |
fea681da MK |
216 | .BR getcwd (3), |
217 | .BR pathconf (3), | |
218 | .BR sysconf (3) |