]> git.ipfire.org Git - thirdparty/man-pages.git/blame_incremental - man3/realpath.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / realpath.3
... / ...
CommitLineData
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 2017-09-15 "" "Linux Programmer's Manual"
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.PP
36.BI "char *realpath(const char *" path ", char *" resolved_path );
37.fi
38.PP
39.in -4n
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
42.in
43.PP
44.BR realpath ():
45.ad l
46.RS 4
47_XOPEN_SOURCE\ >=\ 500
48.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
49 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
50 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
51.RE
52.ad
53.SH DESCRIPTION
54.BR realpath ()
55expands all symbolic links and resolves references
56to
57.IR "/./" ", " "/../"
58and extra \(aq/\(aq
59characters in the null-terminated string named by
60.I path
61to produce a canonicalized absolute pathname.
62The resulting pathname is stored as a null-terminated string,
63up to a maximum of
64.B PATH_MAX
65bytes,
66in the buffer pointed to by
67.IR resolved_path .
68The resulting path will have no symbolic link,
69.I "/./"
70or
71.I "/../"
72components.
73.PP
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.
93.SH RETURN VALUE
94If there is no error,
95.BR realpath ()
96returns a pointer to the
97.IR resolved_path .
98.PP
99Otherwise, it returns NULL, the contents
100of the array
101.I resolved_path
102are undefined, and
103.I errno
104is set to indicate the error.
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
111.I path
112is NULL.
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.)
118.TP
119.B EIO
120An I/O error occurred while reading from the filesystem.
121.TP
122.B ELOOP
123Too many symbolic links were encountered in translating the pathname.
124.TP
125.B ENAMETOOLONG
126A component of a pathname exceeded
127.B NAME_MAX
128characters, or an entire pathname exceeded
129.B PATH_MAX
130characters.
131.TP
132.B ENOENT
133The named file does not exist.
134.TP
135.B ENOMEM
136Out of memory.
137.TP
138.B ENOTDIR
139A component of the path prefix is not a directory.
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
152.SH CONFORMING TO
1534.4BSD, POSIX.1-2001.
154.PP
155POSIX.1-2001 says that the behavior if
156.I resolved_path
157is NULL is implementation-defined.
158POSIX.1-2008 specifies the behavior described in this page.
159.SH NOTES
160In 4.4BSD and Solaris, the limit on the pathname length is
161.B MAXPATHLEN
162(found in \fI<sys/param.h>\fP).
163SUSv2 prescribes
164.B PATH_MAX
165and
166.BR NAME_MAX ,
167as found in \fI<limits.h>\fP or provided by the
168.BR pathconf (3)
169function.
170A typical source fragment would be
171.PP
172.in +4n
173.EX
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
181.EE
182.in
183.PP
184(But see the BUGS section.)
185.PP
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.
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.
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 .
210.SH BUGS
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,
213.IR resolved_path .
214According to POSIX.1-2001 a buffer of size
215.B PATH_MAX
216suffices, but
217.B PATH_MAX
218need not be a defined constant, and may have to be obtained using
219.BR pathconf (3).
220And asking
221.BR pathconf (3)
222does not really help, since, on the one hand POSIX warns that
223the result of
224.BR pathconf (3)
225may be huge and unsuitable for mallocing memory,
226and on the other hand
227.BR pathconf (3)
228may return \-1 to signify that
229.B PATH_MAX
230is not bounded.
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.
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.
241.SH SEE ALSO
242.BR realpath (1),
243.BR readlink (2),
244.BR canonicalize_file_name (3),
245.BR getcwd (3),
246.BR pathconf (3),
247.BR sysconf (3)