]>
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 | .\" |
55e80e25 | 26 | .TH REALPATH 3 2008-08-21 "" "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 (): | |
43 | _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 | |
fea681da | 44 | .SH DESCRIPTION |
7841ad47 | 45 | .BR realpath () |
fea681da MK |
46 | expands all symbolic links and resolves references |
47 | to | |
f8a07a21 | 48 | .IR "/./" ", " "/../" |
f81fb444 | 49 | and extra \(aq/\(aq |
c13182ef | 50 | characters in the null terminated string named by |
fea681da MK |
51 | .I path |
52 | and stores the canonicalized absolute pathname in the buffer of size | |
53 | .B PATH_MAX | |
54 | named by | |
55 | .IR resolved_path . | |
56 | The resulting path will have no symbolic link, | |
f8a07a21 | 57 | .I "/./" |
fea681da | 58 | or |
f8a07a21 | 59 | .I "/../" |
fea681da | 60 | components. |
55e80e25 MK |
61 | |
62 | If | |
63 | .I resolved_path | |
64 | is specified as NULL, then | |
65 | .BR realpath () | |
66 | uses | |
67 | .BR malloc (3) | |
68 | to allocate a buffer of up to | |
69 | .B PATH_MAX | |
70 | bytes to hold the resolved pathname, | |
71 | and returns a pointer to this buffer. | |
72 | The caller should deallocate this buffer using | |
73 | .BR free (3). | |
74 | .\" Even if we use resolved_path == NULL, then realpath() will still | |
75 | .\" return ENAMETOOLONG if the resolved pathname would exceed PATH_MAX | |
76 | .\" bytes -- MTK, Dec 04 | |
77 | .\" .SH HISTORY | |
78 | .\" The | |
79 | .\" .BR realpath () | |
80 | .\" function first appeared in 4.4BSD, contributed by Jan-Simon Pendry. | |
fea681da | 81 | .SH "RETURN VALUE" |
7841ad47 MK |
82 | If there is no error, |
83 | .BR realpath () | |
84 | returns a pointer to the | |
fea681da MK |
85 | .IR resolved_path . |
86 | ||
87 | Otherwise it returns a NULL pointer, and the contents | |
88 | of the array | |
c13182ef MK |
89 | .I resolved_path |
90 | are undefined. | |
91 | The global variable | |
92 | .I errno | |
93 | is set to indicate the error. | |
fea681da MK |
94 | .SH ERRORS |
95 | .TP | |
96 | .B EACCES | |
97 | Read or search permission was denied for a component of the path prefix. | |
98 | .TP | |
99 | .B EINVAL | |
100 | Either | |
101 | .I path | |
102 | or | |
103 | .I resolved_path | |
6387216b MK |
104 | is NULL. |
105 | (In libc5 this would just cause a segfault.) | |
7841ad47 | 106 | But, see NOTES below. |
fea681da MK |
107 | .TP |
108 | .B EIO | |
109 | An I/O error occurred while reading from the file system. | |
110 | .TP | |
111 | .B ELOOP | |
112 | Too many symbolic links were encountered in translating the pathname. | |
113 | .TP | |
114 | .B ENAMETOOLONG | |
c13182ef | 115 | A component of a pathname exceeded |
fea681da | 116 | .B NAME_MAX |
c13182ef | 117 | characters, or an entire pathname exceeded |
fea681da MK |
118 | .B PATH_MAX |
119 | characters. | |
120 | .TP | |
121 | .B ENOENT | |
122 | The named file does not exist. | |
123 | .TP | |
124 | .B ENOTDIR | |
125 | A component of the path prefix is not a directory. | |
2b2581ee MK |
126 | .SH VERSIONS |
127 | On Linux this function appeared in libc 4.5.21. | |
128 | .SH "CONFORMING TO" | |
129 | 4.4BSD, POSIX.1-2001. | |
130 | ||
55e80e25 MK |
131 | POSIX.1 says that the behavior if |
132 | .I resolved_path | |
133 | is NULL is implementation-defined. | |
134 | POSIX.1-2008 specifies the behavior described in this page. | |
2f0af33b MK |
135 | In 4.4BSD and Solaris the limit on the pathname length is |
136 | .B MAXPATHLEN | |
c84371c6 | 137 | (found in \fI<sys/param.h>\fP). |
2f0af33b | 138 | SUSv2 prescribes |
0daa9e92 | 139 | .B PATH_MAX |
2f0af33b MK |
140 | and |
141 | .BR NAME_MAX , | |
142 | as found in \fI<limits.h>\fP or provided by the | |
2b2581ee MK |
143 | .BR pathconf (3) |
144 | function. | |
145 | A typical source fragment would be | |
146 | .LP | |
a6e2f128 | 147 | .in +4n |
2b2581ee MK |
148 | .nf |
149 | #ifdef PATH_MAX | |
150 | path_max = PATH_MAX; | |
151 | #else | |
152 | path_max = pathconf(path, _PC_PATH_MAX); | |
153 | if (path_max <= 0) | |
154 | path_max = 4096; | |
155 | #endif | |
156 | .fi | |
a6e2f128 | 157 | .in |
d2d293ba | 158 | .LP |
2b2581ee MK |
159 | (But see the BUGS section.) |
160 | .LP | |
161 | The 4.4BSD, Linux and SUSv2 versions always return an absolute | |
162 | pathname. | |
163 | Solaris may return a relative pathname when the | |
164 | .I path | |
165 | argument is relative. | |
166 | The prototype of | |
167 | .BR realpath () | |
c84371c6 MK |
168 | is given in \fI<unistd.h>\fP in libc4 and libc5, |
169 | but in \fI<stdlib.h>\fP everywhere else. | |
fea681da | 170 | .SH BUGS |
c13182ef MK |
171 | Avoid using this function. |
172 | It is broken by design since (unless | |
3678235c MK |
173 | using the non-standard |
174 | .I "resolved_path\ ==\ NULL" | |
175 | feature) it is | |
7841ad47 MK |
176 | impossible to determine a suitable size for the output buffer, |
177 | .IR resolved_path . | |
2f0af33b MK |
178 | According to POSIX a buffer of size |
179 | .B PATH_MAX | |
180 | suffices, but | |
181 | .B PATH_MAX | |
182 | need not be a defined constant, and may have to be obtained using | |
fb186734 | 183 | .BR pathconf (3). |
fea681da | 184 | And asking |
fb186734 | 185 | .BR pathconf (3) |
fea681da MK |
186 | does not really help, since on the one hand POSIX warns that |
187 | the result of | |
fb186734 | 188 | .BR pathconf (3) |
c13182ef MK |
189 | may be huge and unsuitable for mallocing memory. |
190 | And on the other | |
fea681da | 191 | hand |
fb186734 | 192 | .BR pathconf (3) |
2f0af33b MK |
193 | may return \-1 to signify that |
194 | .B PATH_MAX | |
195 | is not bounded. | |
fea681da MK |
196 | .LP |
197 | The libc4 and libc5 implementation contains a buffer overflow | |
198 | (fixed in libc-5.4.13). | |
d9a10d9d MK |
199 | Thus, set-user-ID programs like |
200 | .BR mount (8) | |
201 | need a private version. | |
fea681da MK |
202 | .SH "SEE ALSO" |
203 | .BR readlink (2), | |
a8431b7b | 204 | .BR canonicalize_file_name (3), |
fea681da MK |
205 | .BR getcwd (3), |
206 | .BR pathconf (3), | |
207 | .BR sysconf (3) |