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