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