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