OSDN Git Service

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