]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/realpath.3
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[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.
7841ad47
MK
97.SH NOTES
98The glibc implementation of
99.BR realpath ()
100provides a non-standard extension.
101If
102.I resolved_path
103is specified as NULL, then
104.BR realpath ()
105uses
106.BR malloc (3)
3678235c
MK
107to allocate a buffer of up to PATH_MAX bytes
108to hold the resolved pathname,
7841ad47
MK
109and returns a pointer to this buffer.
110The caller should deallocate this buffer using
111.BR free (3).
c13182ef
MK
112.\" Even if we use resolved_path == NULL, then realpath() will still
113.\" return ENAMETOOLONG if the resolved pathname would exceed PATH_MAX
3678235c 114.\" bytes -- MTK, Dec 04
fea681da 115.SH BUGS
c13182ef
MK
116Avoid using this function.
117It is broken by design since (unless
3678235c
MK
118using the non-standard
119.I "resolved_path\ ==\ NULL"
120feature) it is
7841ad47
MK
121impossible to determine a suitable size for the output buffer,
122.IR resolved_path .
fea681da
MK
123According to POSIX a buffer of size PATH_MAX suffices, but
124PATH_MAX need not be a defined constant, and may have to be
125obtained using
31e9a9ec 126.BR pathconf ().
fea681da 127And asking
31e9a9ec 128.BR pathconf ()
fea681da
MK
129does not really help, since on the one hand POSIX warns that
130the result of
31e9a9ec 131.BR pathconf ()
c13182ef
MK
132may be huge and unsuitable for mallocing memory.
133And on the other
fea681da 134hand
31e9a9ec 135.BR pathconf ()
fea681da
MK
136may return \-1 to signify that PATH_MAX is not bounded.
137.LP
138The libc4 and libc5 implementation contains a buffer overflow
139(fixed in libc-5.4.13).
a8431b7b 140Thus, set-user-ID programs like mount need a private version.
fea681da
MK
141.SH HISTORY
142The
7841ad47 143.BR realpath ()
b14d4aa5 144function first appeared in 4.4BSD, contributed by Jan-Simon Pendry.
fea681da
MK
145In Linux this function appears in libc 4.5.21.
146.SH "CONFORMING TO"
68e1685c
MK
1474.4BSD, POSIX.1-2001.
148
b14d4aa5 149In 4.4BSD and Solaris the limit on the pathname length is MAXPATHLEN
c13182ef 150(found in <sys/param.h>).
68e1685c 151SUSv2 prescribes PATH_MAX and
fea681da 152NAME_MAX, as found in <limits.h> or provided by the
31e9a9ec 153.BR pathconf ()
c13182ef 154function.
68e1685c 155A typical source fragment would be
fea681da
MK
156.LP
157.RS
158.nf
159#ifdef PATH_MAX
160 path_max = PATH_MAX;
161#else
cf0a9ace 162 path_max = pathconf(path, _PC_PATH_MAX);
fea681da
MK
163 if (path_max <= 0)
164 path_max = 4096;
165#endif
166.fi
167.RE
168(But see the BUGS section.)
169.LP
b14d4aa5 170The 4.4BSD, Linux and SUSv2 versions always return an absolute
c13182ef
MK
171pathname.
172Solaris may return a relative pathname when the
fea681da
MK
173.I path
174argument is relative.
175The prototype of
7841ad47 176.BR realpath ()
fea681da
MK
177is given in <unistd.h> in libc4 and libc5,
178but in <stdlib.h> everywhere else.
179.SH "SEE ALSO"
180.BR readlink (2),
a8431b7b 181.BR canonicalize_file_name (3),
fea681da
MK
182.BR getcwd (3),
183.BR pathconf (3),
184.BR sysconf (3)