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