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