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