]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/realpath.3
share/mk/: distcheck: Run 'check' after 'build'
[thirdparty/man-pages.git] / 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 2017-09-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 .PP
36 .BI "char *realpath(const char *" path ", char *" resolved_path );
37 .fi
38 .PP
39 .in -4n
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .in
43 .PP
44 .BR realpath ():
45 .ad l
46 .RS 4
47 _XOPEN_SOURCE\ >=\ 500
48 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
49 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
50 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
51 .RE
52 .ad
53 .SH DESCRIPTION
54 .BR realpath ()
55 expands all symbolic links and resolves references
56 to
57 .IR "/./" ", " "/../"
58 and extra \(aq/\(aq
59 characters in the null-terminated string named by
60 .I path
61 to produce a canonicalized absolute pathname.
62 The resulting pathname is stored as a null-terminated string,
63 up to a maximum of
64 .B PATH_MAX
65 bytes,
66 in the buffer pointed to by
67 .IR resolved_path .
68 The resulting path will have no symbolic link,
69 .I "/./"
70 or
71 .I "/../"
72 components.
73 .PP
74 If
75 .I resolved_path
76 is specified as NULL, then
77 .BR realpath ()
78 uses
79 .BR malloc (3)
80 to allocate a buffer of up to
81 .B PATH_MAX
82 bytes to hold the resolved pathname,
83 and returns a pointer to this buffer.
84 The caller should deallocate this buffer using
85 .BR free (3).
86 .\" Even if we use resolved_path == NULL, then realpath() will still
87 .\" return ENAMETOOLONG if the resolved pathname would exceed PATH_MAX
88 .\" bytes -- MTK, Dec 04
89 .\" .SH HISTORY
90 .\" The
91 .\" .BR realpath ()
92 .\" function first appeared in 4.4BSD, contributed by Jan-Simon Pendry.
93 .SH RETURN VALUE
94 If there is no error,
95 .BR realpath ()
96 returns a pointer to the
97 .IR resolved_path .
98 .PP
99 Otherwise, it returns NULL, the contents
100 of the array
101 .I resolved_path
102 are undefined, and
103 .I errno
104 is set to indicate the error.
105 .SH ERRORS
106 .TP
107 .B EACCES
108 Read or search permission was denied for a component of the path prefix.
109 .TP
110 .B EINVAL
111 .I path
112 is NULL.
113 .\" (In libc5 this would just cause a segfault.)
114 (In glibc versions before 2.3,
115 this error is also returned if
116 .IR resolved_path
117 is NULL.)
118 .TP
119 .B EIO
120 An I/O error occurred while reading from the filesystem.
121 .TP
122 .B ELOOP
123 Too many symbolic links were encountered in translating the pathname.
124 .TP
125 .B ENAMETOOLONG
126 A component of a pathname exceeded
127 .B NAME_MAX
128 characters, or an entire pathname exceeded
129 .B PATH_MAX
130 characters.
131 .TP
132 .B ENOENT
133 The named file does not exist.
134 .TP
135 .B ENOMEM
136 Out of memory.
137 .TP
138 .B ENOTDIR
139 A component of the path prefix is not a directory.
140 .SH ATTRIBUTES
141 For an explanation of the terms used in this section, see
142 .BR attributes (7).
143 .TS
144 allbox;
145 lb lb lb
146 l l l.
147 Interface Attribute Value
148 T{
149 .BR realpath ()
150 T} Thread safety MT-Safe
151 .TE
152 .SH CONFORMING TO
153 4.4BSD, POSIX.1-2001.
154 .PP
155 POSIX.1-2001 says that the behavior if
156 .I resolved_path
157 is NULL is implementation-defined.
158 POSIX.1-2008 specifies the behavior described in this page.
159 .SH NOTES
160 In 4.4BSD and Solaris, the limit on the pathname length is
161 .B MAXPATHLEN
162 (found in \fI<sys/param.h>\fP).
163 SUSv2 prescribes
164 .B PATH_MAX
165 and
166 .BR NAME_MAX ,
167 as found in \fI<limits.h>\fP or provided by the
168 .BR pathconf (3)
169 function.
170 A typical source fragment would be
171 .PP
172 .in +4n
173 .EX
174 #ifdef PATH_MAX
175 path_max = PATH_MAX;
176 #else
177 path_max = pathconf(path, _PC_PATH_MAX);
178 if (path_max <= 0)
179 path_max = 4096;
180 #endif
181 .EE
182 .in
183 .PP
184 (But see the BUGS section.)
185 .PP
186 .\" 2012-05-05, According to Casper Dik, the statement about
187 .\" Solaris was not true at least as far back as 1997, and
188 .\" may never have been true.
189 .\"
190 .\" The 4.4BSD, Linux and SUSv2 versions always return an absolute
191 .\" pathname.
192 .\" Solaris may return a relative pathname when the
193 .\" .I path
194 .\" argument is relative.
195 .\" The prototype of
196 .\" .BR realpath ()
197 .\" is given in \fI<unistd.h>\fP in libc4 and libc5,
198 .\" but in \fI<stdlib.h>\fP everywhere else.
199 .SS GNU extensions
200 If the call fails with either
201 .BR EACCES
202 or
203 .BR ENOENT
204 and
205 .I resolved_path
206 is not NULL, then the prefix of
207 .I path
208 that is not readable or does not exist is returned in
209 .IR resolved_path .
210 .SH BUGS
211 The POSIX.1-2001 standard version of this function is broken by design,
212 since it is impossible to determine a suitable size for the output buffer,
213 .IR resolved_path .
214 According to POSIX.1-2001 a buffer of size
215 .B PATH_MAX
216 suffices, but
217 .B PATH_MAX
218 need not be a defined constant, and may have to be obtained using
219 .BR pathconf (3).
220 And asking
221 .BR pathconf (3)
222 does not really help, since, on the one hand POSIX warns that
223 the result of
224 .BR pathconf (3)
225 may be huge and unsuitable for mallocing memory,
226 and on the other hand
227 .BR pathconf (3)
228 may return \-1 to signify that
229 .B PATH_MAX
230 is not bounded.
231 The
232 .I "resolved_path\ ==\ NULL"
233 feature, not standardized in POSIX.1-2001,
234 but standardized in POSIX.1-2008, allows this design problem to be avoided.
235 .\" .LP
236 .\" The libc4 and libc5 implementation contained a buffer overflow
237 .\" (fixed in libc-5.4.13).
238 .\" Thus, set-user-ID programs like
239 .\" .BR mount (8)
240 .\" needed a private version.
241 .SH SEE ALSO
242 .BR realpath (1),
243 .BR readlink (2),
244 .BR canonicalize_file_name (3),
245 .BR getcwd (3),
246 .BR pathconf (3),
247 .BR sysconf (3)