]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/realpath.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / realpath.3
CommitLineData
fea681da
MK
1.\" Copyright (C) 1999 Andries Brouwer (aeb@cwi.nl)
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 12.\"
fea681da
MK
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.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da
MK
24.\"
25.\" Rewritten old page, 990824, aeb@cwi.nl
7841ad47 26.\" 2004-12-14, mtk, added discussion of resolved_path == NULL
fea681da 27.\"
4b8c67d9 28.TH REALPATH 3 2017-09-15 "" "Linux Programmer's Manual"
fea681da
MK
29.SH NAME
30realpath \- return the canonicalized absolute pathname
31.SH SYNOPSIS
32.nf
33.B #include <limits.h>
34.B #include <stdlib.h>
68e4db0a 35.PP
c13182ef 36.BI "char *realpath(const char *" path ", char *" resolved_path );
cc4615cc 37.fi
68e4db0a 38.PP
cc4615cc
MK
39.in -4n
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
42.in
68e4db0a 43.PP
cc4615cc 44.BR realpath ():
a440dd9b
MK
45.ad l
46.RS 4
2b1b0424 47_XOPEN_SOURCE\ >=\ 500
cf7fa0a1 48.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
2b1b0424
MK
49 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
50 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
a440dd9b
MK
51.RE
52.ad
fea681da 53.SH DESCRIPTION
7841ad47 54.BR realpath ()
fea681da
MK
55expands all symbolic links and resolves references
56to
f8a07a21 57.IR "/./" ", " "/../"
f81fb444 58and extra \(aq/\(aq
d0cb7cc6 59characters in the null-terminated string named by
fea681da 60.I path
bf0e2b8a
MK
61to produce a canonicalized absolute pathname.
62The resulting pathname is stored as a null-terminated string,
63up to a maximum of
fea681da 64.B PATH_MAX
5a3b4036 65bytes,
bf0e2b8a 66in the buffer pointed to by
fea681da
MK
67.IR resolved_path .
68The resulting path will have no symbolic link,
f8a07a21 69.I "/./"
fea681da 70or
f8a07a21 71.I "/../"
fea681da 72components.
847e0d88 73.PP
55e80e25
MK
74If
75.I resolved_path
76is specified as NULL, then
77.BR realpath ()
78uses
79.BR malloc (3)
80to allocate a buffer of up to
81.B PATH_MAX
82bytes to hold the resolved pathname,
83and returns a pointer to this buffer.
84The 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.
47297adb 93.SH RETURN VALUE
7841ad47
MK
94If there is no error,
95.BR realpath ()
96returns a pointer to the
fea681da 97.IR resolved_path .
847e0d88 98.PP
b437fdd9 99Otherwise, it returns NULL, the contents
fea681da 100of the array
c13182ef 101.I resolved_path
28d03ce9 102are undefined, and
c13182ef
MK
103.I errno
104is set to indicate the error.
fea681da
MK
105.SH ERRORS
106.TP
107.B EACCES
108Read or search permission was denied for a component of the path prefix.
109.TP
110.B EINVAL
fea681da 111.I path
6387216b 112is NULL.
536bc966
MK
113.\" (In libc5 this would just cause a segfault.)
114(In glibc versions before 2.3,
115this error is also returned if
116.IR resolved_path
117is NULL.)
fea681da
MK
118.TP
119.B EIO
9ee4a2b6 120An I/O error occurred while reading from the filesystem.
fea681da
MK
121.TP
122.B ELOOP
123Too many symbolic links were encountered in translating the pathname.
124.TP
125.B ENAMETOOLONG
c13182ef 126A component of a pathname exceeded
fea681da 127.B NAME_MAX
c13182ef 128characters, or an entire pathname exceeded
fea681da
MK
129.B PATH_MAX
130characters.
131.TP
132.B ENOENT
133The named file does not exist.
134.TP
66ced968
MK
135.B ENOMEM
136Out of memory.
137.TP
fea681da
MK
138.B ENOTDIR
139A component of the path prefix is not a directory.
3b8c8d78
MS
140.SH ATTRIBUTES
141For an explanation of the terms used in this section, see
142.BR attributes (7).
143.TS
144allbox;
145lb lb lb
146l l l.
147Interface Attribute Value
148T{
149.BR realpath ()
150T} Thread safety MT-Safe
151.TE
47297adb 152.SH CONFORMING TO
2b2581ee 1534.4BSD, POSIX.1-2001.
847e0d88 154.PP
c49e8fc9 155POSIX.1-2001 says that the behavior if
55e80e25
MK
156.I resolved_path
157is NULL is implementation-defined.
158POSIX.1-2008 specifies the behavior described in this page.
c49e8fc9 159.SH NOTES
2221c1b9 160In 4.4BSD and Solaris, the limit on the pathname length is
2f0af33b 161.B MAXPATHLEN
c84371c6 162(found in \fI<sys/param.h>\fP).
2f0af33b 163SUSv2 prescribes
0daa9e92 164.B PATH_MAX
2f0af33b
MK
165and
166.BR NAME_MAX ,
167as found in \fI<limits.h>\fP or provided by the
2b2581ee
MK
168.BR pathconf (3)
169function.
170A typical source fragment would be
dd3568a1 171.PP
a6e2f128 172.in +4n
b8302363 173.EX
2b2581ee
MK
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
b8302363 181.EE
a6e2f128 182.in
dd3568a1 183.PP
2b2581ee 184(But see the BUGS section.)
dd3568a1 185.PP
8f826cd8
MK
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.
788b19a2
MK
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.
0bba64e4
MK
199.SS GNU extensions
200If the call fails with either
201.BR EACCES
202or
203.BR ENOENT
204and
205.I resolved_path
206is not NULL, then the prefix of
207.I path
208that is not readable or does not exist is returned in
209.IR resolved_path .
fea681da 210.SH BUGS
c49e8fc9
MK
211The POSIX.1-2001 standard version of this function is broken by design,
212since it is impossible to determine a suitable size for the output buffer,
7841ad47 213.IR resolved_path .
c49e8fc9 214According to POSIX.1-2001 a buffer of size
2f0af33b
MK
215.B PATH_MAX
216suffices, but
217.B PATH_MAX
218need not be a defined constant, and may have to be obtained using
fb186734 219.BR pathconf (3).
fea681da 220And asking
fb186734 221.BR pathconf (3)
c49e8fc9 222does not really help, since, on the one hand POSIX warns that
fea681da 223the result of
fb186734 224.BR pathconf (3)
c49e8fc9
MK
225may be huge and unsuitable for mallocing memory,
226and on the other hand
fb186734 227.BR pathconf (3)
2f0af33b
MK
228may return \-1 to signify that
229.B PATH_MAX
230is not bounded.
c49e8fc9
MK
231The
232.I "resolved_path\ ==\ NULL"
233feature, not standardized in POSIX.1-2001,
234but standardized in POSIX.1-2008, allows this design problem to be avoided.
788b19a2
MK
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.
47297adb 241.SH SEE ALSO
9e4f79d4 242.BR realpath (1),
fea681da 243.BR readlink (2),
a8431b7b 244.BR canonicalize_file_name (3),
fea681da
MK
245.BR getcwd (3),
246.BR pathconf (3),
247.BR sysconf (3)