]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/realpath.3
These interfaces are specified in POSIX.1-2008.
[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.\"
cc4615cc 26.TH REALPATH 3 2007-07-26 "" "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 ():
43_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
fea681da 44.SH DESCRIPTION
7841ad47 45.BR realpath ()
fea681da
MK
46expands all symbolic links and resolves references
47to
f8a07a21 48.IR "/./" ", " "/../"
f81fb444 49and extra \(aq/\(aq
c13182ef 50characters in the null terminated string named by
fea681da
MK
51.I path
52and stores the canonicalized absolute pathname in the buffer of size
53.B PATH_MAX
54named by
55.IR resolved_path .
56The resulting path will have no symbolic link,
f8a07a21 57.I "/./"
fea681da 58or
f8a07a21 59.I "/../"
fea681da
MK
60components.
61.SH "RETURN VALUE"
7841ad47
MK
62If there is no error,
63.BR realpath ()
64returns a pointer to the
fea681da
MK
65.IR resolved_path .
66
67Otherwise it returns a NULL pointer, and the contents
68of the array
c13182ef
MK
69.I resolved_path
70are undefined.
71The global variable
72.I errno
73is set to indicate the error.
fea681da
MK
74.SH ERRORS
75.TP
76.B EACCES
77Read or search permission was denied for a component of the path prefix.
78.TP
79.B EINVAL
80Either
81.I path
82or
83.I resolved_path
6387216b
MK
84is NULL.
85(In libc5 this would just cause a segfault.)
7841ad47 86But, see NOTES below.
fea681da
MK
87.TP
88.B EIO
89An I/O error occurred while reading from the file system.
90.TP
91.B ELOOP
92Too many symbolic links were encountered in translating the pathname.
93.TP
94.B ENAMETOOLONG
c13182ef 95A component of a pathname exceeded
fea681da 96.B NAME_MAX
c13182ef 97characters, or an entire pathname exceeded
fea681da
MK
98.B PATH_MAX
99characters.
100.TP
101.B ENOENT
102The named file does not exist.
103.TP
104.B ENOTDIR
105A component of the path prefix is not a directory.
2b2581ee
MK
106.SH VERSIONS
107On Linux this function appeared in libc 4.5.21.
108.SH "CONFORMING TO"
1094.4BSD, POSIX.1-2001.
110
2f0af33b
MK
111In 4.4BSD and Solaris the limit on the pathname length is
112.B MAXPATHLEN
c84371c6 113(found in \fI<sys/param.h>\fP).
2f0af33b 114SUSv2 prescribes
0daa9e92 115.B PATH_MAX
2f0af33b
MK
116and
117.BR NAME_MAX ,
118as found in \fI<limits.h>\fP or provided by the
2b2581ee
MK
119.BR pathconf (3)
120function.
121A typical source fragment would be
122.LP
a6e2f128 123.in +4n
2b2581ee
MK
124.nf
125#ifdef PATH_MAX
126 path_max = PATH_MAX;
127#else
128 path_max = pathconf(path, _PC_PATH_MAX);
129 if (path_max <= 0)
130 path_max = 4096;
131#endif
132.fi
a6e2f128 133.in
d2d293ba 134.LP
2b2581ee
MK
135(But see the BUGS section.)
136.LP
137The 4.4BSD, Linux and SUSv2 versions always return an absolute
138pathname.
139Solaris may return a relative pathname when the
140.I path
141argument is relative.
142The prototype of
143.BR realpath ()
c84371c6
MK
144is given in \fI<unistd.h>\fP in libc4 and libc5,
145but in \fI<stdlib.h>\fP everywhere else.
7841ad47
MK
146.SH NOTES
147The glibc implementation of
148.BR realpath ()
149provides a non-standard extension.
150If
151.I resolved_path
152is specified as NULL, then
153.BR realpath ()
154uses
155.BR malloc (3)
2f0af33b
MK
156to allocate a buffer of up to
157.B PATH_MAX
158bytes to hold the resolved pathname,
7841ad47
MK
159and returns a pointer to this buffer.
160The caller should deallocate this buffer using
161.BR free (3).
c13182ef
MK
162.\" Even if we use resolved_path == NULL, then realpath() will still
163.\" return ENAMETOOLONG if the resolved pathname would exceed PATH_MAX
3678235c 164.\" bytes -- MTK, Dec 04
2b2581ee
MK
165.\" .SH HISTORY
166.\" The
167.\" .BR realpath ()
168.\" function first appeared in 4.4BSD, contributed by Jan-Simon Pendry.
fea681da 169.SH BUGS
c13182ef
MK
170Avoid using this function.
171It is broken by design since (unless
3678235c
MK
172using the non-standard
173.I "resolved_path\ ==\ NULL"
174feature) it is
7841ad47
MK
175impossible to determine a suitable size for the output buffer,
176.IR resolved_path .
2f0af33b
MK
177According to POSIX a buffer of size
178.B PATH_MAX
179suffices, but
180.B PATH_MAX
181need not be a defined constant, and may have to be obtained using
fb186734 182.BR pathconf (3).
fea681da 183And asking
fb186734 184.BR pathconf (3)
fea681da
MK
185does not really help, since on the one hand POSIX warns that
186the result of
fb186734 187.BR pathconf (3)
c13182ef
MK
188may be huge and unsuitable for mallocing memory.
189And on the other
fea681da 190hand
fb186734 191.BR pathconf (3)
2f0af33b
MK
192may return \-1 to signify that
193.B PATH_MAX
194is not bounded.
fea681da
MK
195.LP
196The libc4 and libc5 implementation contains a buffer overflow
197(fixed in libc-5.4.13).
d9a10d9d
MK
198Thus, set-user-ID programs like
199.BR mount (8)
200need a private version.
fea681da
MK
201.SH "SEE ALSO"
202.BR readlink (2),
a8431b7b 203.BR canonicalize_file_name (3),
fea681da
MK
204.BR getcwd (3),
205.BR pathconf (3),
206.BR sysconf (3)