]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/realpath.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / realpath.3
CommitLineData
a1eaacb1 1'\" t
fea681da
MK
2.\" Copyright (C) 1999 Andries Brouwer (aeb@cwi.nl)
3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
5.\"
6.\" Rewritten old page, 990824, aeb@cwi.nl
7841ad47 7.\" 2004-12-14, mtk, added discussion of resolved_path == NULL
fea681da 8.\"
4c1c5274 9.TH realpath 3 (date) "Linux man-pages (unreleased)"
fea681da
MK
10.SH NAME
11realpath \- return the canonicalized absolute pathname
f1d2fd3f
AC
12.SH LIBRARY
13Standard C library
8fc3b2cf 14.RI ( libc ", " \-lc )
fea681da
MK
15.SH SYNOPSIS
16.nf
17.B #include <limits.h>
18.B #include <stdlib.h>
c6d039a3 19.P
0f8d366c
AC
20.BI "char *realpath(const char *restrict " path ,
21.BI " char *restrict " resolved_path );
cc4615cc 22.fi
c6d039a3 23.P
d39ad78f 24.RS -4
cc4615cc
MK
25Feature Test Macro Requirements for glibc (see
26.BR feature_test_macros (7)):
d39ad78f 27.RE
c6d039a3 28.P
cc4615cc 29.BR realpath ():
9d2adbae 30.nf
5c10d2c5
MK
31 _XOPEN_SOURCE >= 500
32.\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
75c018a1
AC
33 || /* glibc >= 2.19: */ _DEFAULT_SOURCE
34 || /* glibc <= 2.19: */ _BSD_SOURCE
9d2adbae 35.fi
fea681da 36.SH DESCRIPTION
7841ad47 37.BR realpath ()
fea681da
MK
38expands all symbolic links and resolves references
39to
f8a07a21 40.IR "/./" ", " "/../"
b957f81f 41and extra \[aq]/\[aq]
d0cb7cc6 42characters in the null-terminated string named by
fea681da 43.I path
bf0e2b8a
MK
44to produce a canonicalized absolute pathname.
45The resulting pathname is stored as a null-terminated string,
46up to a maximum of
fea681da 47.B PATH_MAX
5a3b4036 48bytes,
bf0e2b8a 49in the buffer pointed to by
fea681da
MK
50.IR resolved_path .
51The resulting path will have no symbolic link,
f8a07a21 52.I "/./"
fea681da 53or
f8a07a21 54.I "/../"
fea681da 55components.
c6d039a3 56.P
55e80e25
MK
57If
58.I resolved_path
59is specified as NULL, then
60.BR realpath ()
61uses
62.BR malloc (3)
63to allocate a buffer of up to
64.B PATH_MAX
65bytes to hold the resolved pathname,
66and returns a pointer to this buffer.
67The caller should deallocate this buffer using
68.BR free (3).
69.\" Even if we use resolved_path == NULL, then realpath() will still
70.\" return ENAMETOOLONG if the resolved pathname would exceed PATH_MAX
71.\" bytes -- MTK, Dec 04
47297adb 72.SH RETURN VALUE
7841ad47
MK
73If there is no error,
74.BR realpath ()
75returns a pointer to the
fea681da 76.IR resolved_path .
c6d039a3 77.P
b437fdd9 78Otherwise, it returns NULL, the contents
fea681da 79of the array
c13182ef 80.I resolved_path
28d03ce9 81are undefined, and
c13182ef
MK
82.I errno
83is set to indicate the error.
fea681da
MK
84.SH ERRORS
85.TP
86.B EACCES
87Read or search permission was denied for a component of the path prefix.
88.TP
89.B EINVAL
fea681da 90.I path
6387216b 91is NULL.
536bc966 92.\" (In libc5 this would just cause a segfault.)
b324e17d 93(Before glibc 2.3,
536bc966 94this error is also returned if
1ae6b2c7 95.I resolved_path
536bc966 96is NULL.)
fea681da
MK
97.TP
98.B EIO
9ee4a2b6 99An I/O error occurred while reading from the filesystem.
fea681da
MK
100.TP
101.B ELOOP
102Too many symbolic links were encountered in translating the pathname.
103.TP
104.B ENAMETOOLONG
c13182ef 105A component of a pathname exceeded
fea681da 106.B NAME_MAX
c13182ef 107characters, or an entire pathname exceeded
fea681da
MK
108.B PATH_MAX
109characters.
110.TP
111.B ENOENT
112The named file does not exist.
113.TP
66ced968
MK
114.B ENOMEM
115Out of memory.
116.TP
fea681da
MK
117.B ENOTDIR
118A component of the path prefix is not a directory.
3b8c8d78
MS
119.SH ATTRIBUTES
120For an explanation of the terms used in this section, see
121.BR attributes (7).
122.TS
123allbox;
c466875e 124lbx lb lb
3b8c8d78
MS
125l l l.
126Interface Attribute Value
127T{
9e54434e
BR
128.na
129.nh
3b8c8d78
MS
130.BR realpath ()
131T} Thread safety MT-Safe
132.TE
4131356c
AC
133.SH VERSIONS
134.SS GNU extensions
135If the call fails with either
136.B EACCES
137or
138.B ENOENT
139and
140.I resolved_path
141is not NULL, then the prefix of
142.I path
143that is not readable or does not exist is returned in
144.IR resolved_path .
3113c7f3 145.SH STANDARDS
4131356c
AC
146POSIX.1-2008.
147.SH HISTORY
1484.4BSD, POSIX.1-2001, Solaris.
c6d039a3 149.P
c49e8fc9 150POSIX.1-2001 says that the behavior if
55e80e25
MK
151.I resolved_path
152is NULL is implementation-defined.
153POSIX.1-2008 specifies the behavior described in this page.
c6d039a3 154.P
2221c1b9 155In 4.4BSD and Solaris, the limit on the pathname length is
2f0af33b 156.B MAXPATHLEN
c84371c6 157(found in \fI<sys/param.h>\fP).
2f0af33b 158SUSv2 prescribes
0daa9e92 159.B PATH_MAX
2f0af33b
MK
160and
161.BR NAME_MAX ,
162as found in \fI<limits.h>\fP or provided by the
2b2581ee
MK
163.BR pathconf (3)
164function.
165A typical source fragment would be
c6d039a3 166.P
a6e2f128 167.in +4n
b8302363 168.EX
2b2581ee
MK
169#ifdef PATH_MAX
170 path_max = PATH_MAX;
171#else
172 path_max = pathconf(path, _PC_PATH_MAX);
173 if (path_max <= 0)
174 path_max = 4096;
175#endif
b8302363 176.EE
a6e2f128 177.in
c6d039a3 178.P
2b2581ee 179(But see the BUGS section.)
c6d039a3 180.\".P
8f826cd8
MK
181.\" 2012-05-05, According to Casper Dik, the statement about
182.\" Solaris was not true at least as far back as 1997, and
183.\" may never have been true.
184.\"
185.\" The 4.4BSD, Linux and SUSv2 versions always return an absolute
186.\" pathname.
187.\" Solaris may return a relative pathname when the
188.\" .I path
189.\" argument is relative.
788b19a2
MK
190.\" The prototype of
191.\" .BR realpath ()
192.\" is given in \fI<unistd.h>\fP in libc4 and libc5,
193.\" but in \fI<stdlib.h>\fP everywhere else.
fea681da 194.SH BUGS
c49e8fc9
MK
195The POSIX.1-2001 standard version of this function is broken by design,
196since it is impossible to determine a suitable size for the output buffer,
7841ad47 197.IR resolved_path .
c49e8fc9 198According to POSIX.1-2001 a buffer of size
2f0af33b
MK
199.B PATH_MAX
200suffices, but
201.B PATH_MAX
202need not be a defined constant, and may have to be obtained using
fb186734 203.BR pathconf (3).
fea681da 204And asking
fb186734 205.BR pathconf (3)
c49e8fc9 206does not really help, since, on the one hand POSIX warns that
fea681da 207the result of
fb186734 208.BR pathconf (3)
c49e8fc9
MK
209may be huge and unsuitable for mallocing memory,
210and on the other hand
fb186734 211.BR pathconf (3)
2f0af33b
MK
212may return \-1 to signify that
213.B PATH_MAX
214is not bounded.
c49e8fc9
MK
215The
216.I "resolved_path\ ==\ NULL"
217feature, not standardized in POSIX.1-2001,
218but standardized in POSIX.1-2008, allows this design problem to be avoided.
c6d039a3 219.\" .P
788b19a2
MK
220.\" The libc4 and libc5 implementation contained a buffer overflow
221.\" (fixed in libc-5.4.13).
222.\" Thus, set-user-ID programs like
223.\" .BR mount (8)
224.\" needed a private version.
47297adb 225.SH SEE ALSO
9e4f79d4 226.BR realpath (1),
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)