]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/realpath.3
sk98lin.4: wfix
[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.\"
45e32119 28.TH REALPATH 3 2013-03-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>
35.sp
c13182ef 36.BI "char *realpath(const char *" path ", char *" resolved_path );
cc4615cc
MK
37.fi
38.sp
39.in -4n
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
42.in
43.sp
44.BR realpath ():
a440dd9b
MK
45.ad l
46.RS 4
47_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
48_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
49.RE
50.ad
fea681da 51.SH DESCRIPTION
7841ad47 52.BR realpath ()
fea681da
MK
53expands all symbolic links and resolves references
54to
f8a07a21 55.IR "/./" ", " "/../"
f81fb444 56and extra \(aq/\(aq
d0cb7cc6 57characters in the null-terminated string named by
fea681da 58.I path
bf0e2b8a
MK
59to produce a canonicalized absolute pathname.
60The resulting pathname is stored as a null-terminated string,
61up to a maximum of
fea681da 62.B PATH_MAX
5a3b4036 63bytes,
bf0e2b8a 64in the buffer pointed to by
fea681da
MK
65.IR resolved_path .
66The resulting path will have no symbolic link,
f8a07a21 67.I "/./"
fea681da 68or
f8a07a21 69.I "/../"
fea681da 70components.
55e80e25
MK
71
72If
73.I resolved_path
74is specified as NULL, then
75.BR realpath ()
76uses
77.BR malloc (3)
78to allocate a buffer of up to
79.B PATH_MAX
80bytes to hold the resolved pathname,
81and returns a pointer to this buffer.
82The 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.
47297adb 91.SH RETURN VALUE
7841ad47
MK
92If there is no error,
93.BR realpath ()
94returns a pointer to the
fea681da
MK
95.IR resolved_path .
96
2221c1b9 97Otherwise, it returns a NULL pointer, the contents
fea681da 98of the array
c13182ef 99.I resolved_path
28d03ce9 100are undefined, and
c13182ef
MK
101.I errno
102is set to indicate the error.
fea681da
MK
103.SH ERRORS
104.TP
105.B EACCES
106Read or search permission was denied for a component of the path prefix.
107.TP
108.B EINVAL
fea681da 109.I path
6387216b 110is NULL.
536bc966
MK
111.\" (In libc5 this would just cause a segfault.)
112(In glibc versions before 2.3,
113this error is also returned if
114.IR resolved_path
115is NULL.)
fea681da
MK
116.TP
117.B EIO
118An I/O error occurred while reading from the file system.
119.TP
120.B ELOOP
121Too many symbolic links were encountered in translating the pathname.
122.TP
123.B ENAMETOOLONG
c13182ef 124A component of a pathname exceeded
fea681da 125.B NAME_MAX
c13182ef 126characters, or an entire pathname exceeded
fea681da
MK
127.B PATH_MAX
128characters.
129.TP
130.B ENOENT
131The named file does not exist.
132.TP
133.B ENOTDIR
134A component of the path prefix is not a directory.
2b2581ee 135.SH VERSIONS
2221c1b9 136On Linux, this function appeared in libc 4.5.21.
47297adb 137.SH CONFORMING TO
2b2581ee
MK
1384.4BSD, POSIX.1-2001.
139
c49e8fc9 140POSIX.1-2001 says that the behavior if
55e80e25
MK
141.I resolved_path
142is NULL is implementation-defined.
143POSIX.1-2008 specifies the behavior described in this page.
c49e8fc9 144.SH NOTES
2221c1b9 145In 4.4BSD and Solaris, the limit on the pathname length is
2f0af33b 146.B MAXPATHLEN
c84371c6 147(found in \fI<sys/param.h>\fP).
2f0af33b 148SUSv2 prescribes
0daa9e92 149.B PATH_MAX
2f0af33b
MK
150and
151.BR NAME_MAX ,
152as found in \fI<limits.h>\fP or provided by the
2b2581ee
MK
153.BR pathconf (3)
154function.
155A typical source fragment would be
156.LP
a6e2f128 157.in +4n
2b2581ee
MK
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
a6e2f128 167.in
d2d293ba 168.LP
2b2581ee
MK
169(But see the BUGS section.)
170.LP
8f826cd8
MK
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.
2b2581ee
MK
180The prototype of
181.BR realpath ()
c84371c6
MK
182is given in \fI<unistd.h>\fP in libc4 and libc5,
183but in \fI<stdlib.h>\fP everywhere else.
0bba64e4
MK
184.SS GNU extensions
185If the call fails with either
186.BR EACCES
187or
188.BR ENOENT
189and
190.I resolved_path
191is not NULL, then the prefix of
192.I path
193that is not readable or does not exist is returned in
194.IR resolved_path .
fea681da 195.SH BUGS
c49e8fc9
MK
196The POSIX.1-2001 standard version of this function is broken by design,
197since it is impossible to determine a suitable size for the output buffer,
7841ad47 198.IR resolved_path .
c49e8fc9 199According to POSIX.1-2001 a buffer of size
2f0af33b
MK
200.B PATH_MAX
201suffices, but
202.B PATH_MAX
203need not be a defined constant, and may have to be obtained using
fb186734 204.BR pathconf (3).
fea681da 205And asking
fb186734 206.BR pathconf (3)
c49e8fc9 207does not really help, since, on the one hand POSIX warns that
fea681da 208the result of
fb186734 209.BR pathconf (3)
c49e8fc9
MK
210may be huge and unsuitable for mallocing memory,
211and on the other hand
fb186734 212.BR pathconf (3)
2f0af33b
MK
213may return \-1 to signify that
214.B PATH_MAX
215is not bounded.
c49e8fc9
MK
216The
217.I "resolved_path\ ==\ NULL"
218feature, not standardized in POSIX.1-2001,
219but standardized in POSIX.1-2008, allows this design problem to be avoided.
fea681da 220.LP
dbe8b62b 221The libc4 and libc5 implementation contained a buffer overflow
fea681da 222(fixed in libc-5.4.13).
d9a10d9d
MK
223Thus, set-user-ID programs like
224.BR mount (8)
dbe8b62b 225needed a private version.
47297adb 226.SH SEE ALSO
fea681da 227.BR readlink (2),
a8431b7b 228.BR canonicalize_file_name (3),
fea681da
MK
229.BR getcwd (3),
230.BR pathconf (3),
231.BR sysconf (3)