man-pages/man3/getcwd.3.html
2021-03-31 01:06:50 +01:00

463 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETCWD</TITLE>
</HEAD><BODY>
<H1>GETCWD</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2018-04-30<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getcwd, getwd, get_current_dir_name - get current working directory
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>char *getcwd(char *</B><I>buf</I><B>, size_t </B><I>size</I><B>);</B>
<B>char *getwd(char *</B><I>buf</I><B>);</B>
<B>char *get_current_dir_name(void);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>get_current_dir_name</B>():
<DL COMPACT><DT id="1"><DD>
_GNU_SOURCE
</DL>
<P>
<B>getwd</B>():
<DL COMPACT><DT id="2"><DD>
<DL COMPACT>
<DT id="3">Since glibc 2.12:<DD>
<PRE>
(_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500) &amp;&amp; ! (_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L)
|| /* Glibc since 2.19: */ _DEFAULT_SOURCE
|| /* Glibc versions &lt;= 2.19: */ _BSD_SOURCE
<DT id="4"></PRE>
<DD>
Before glibc 2.12:
_BSD_SOURCE || _XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
These functions return a null-terminated string containing an
absolute pathname that is the current working directory of
the calling process.
The pathname is returned as the function result and via the argument
<I>buf</I>,
if present.
<P>
The
<B>getcwd</B>()
function copies an absolute pathname of the current working directory
to the array pointed to by
<I>buf</I>,
which is of length
<I>size</I>.
<P>
If the length of the absolute pathname of the current working directory,
including the terminating null byte, exceeds
<I>size</I>
bytes, NULL is returned, and
<I>errno</I>
is set to
<B>ERANGE</B>;
an application should check for this error, and allocate a larger
buffer if necessary.
<P>
As an extension to the POSIX.1-2001 standard, glibc's
<B>getcwd</B>()
allocates the buffer dynamically using
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
if
<I>buf</I>
is NULL.
In this case, the allocated buffer has the length
<I>size</I>
unless
<I>size</I>
is zero, when
<I>buf</I>
is allocated as big as necessary.
The caller should
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3)
the returned buffer.
<P>
<B>get_current_dir_name</B>()
will
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
an array big enough to hold the absolute pathname of
the current working directory.
If the environment
variable
<B>PWD</B>
is set, and its value is correct, then that value will be returned.
The caller should
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3)
the returned buffer.
<P>
<B>getwd</B>()
does not
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
any memory.
The
<I>buf</I>
argument should be a pointer to an array at least
<B>PATH_MAX</B>
bytes long.
If the length of the absolute pathname of the current working directory,
including the terminating null byte, exceeds
<B>PATH_MAX</B>
bytes, NULL is returned, and
<I>errno</I>
is set to
<B>ENAMETOOLONG</B>.
(Note that on some systems,
<B>PATH_MAX</B>
may not be a compile-time constant;
furthermore, its value may depend on the filesystem, see
<B><A HREF="/cgi-bin/man/man2html?3+pathconf">pathconf</A></B>(3).)
For portability and security reasons, use of
<B>getwd</B>()
is deprecated.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return a pointer to a string containing
the pathname of the current working directory.
In the case of
<B>getcwd</B>()
and
<B>getwd</B>()
this is the same value as
<I>buf</I>.
<P>
On failure, these functions return NULL, and
<I>errno</I>
is set to indicate the error.
The contents of the array pointed to by
<I>buf</I>
are undefined on error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="5"><B>EACCES</B>
<DD>
Permission to read or search a component of the filename was denied.
<DT id="6"><B>EFAULT</B>
<DD>
<I>buf</I>
points to a bad address.
<DT id="7"><B>EINVAL</B>
<DD>
The
<I>size</I>
argument is zero and
<I>buf</I>
is not a null pointer.
<DT id="8"><B>EINVAL</B>
<DD>
<B>getwd</B>():
<I>buf</I>
is NULL.
<DT id="9"><B>ENAMETOOLONG</B>
<DD>
<B>getwd</B>():
The size of the null-terminated absolute pathname string exceeds
<B>PATH_MAX</B>
bytes.
<DT id="10"><B>ENOENT</B>
<DD>
The current working directory has been unlinked.
<DT id="11"><B>ENOMEM</B>
<DD>
Out of memory.
<DT id="12"><B>ERANGE</B>
<DD>
The
<I>size</I>
argument is less than the length of the absolute pathname of the
working directory, including the terminating null byte.
You need to allocate a bigger array and try again.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>getcwd</B>(),
<B>getwd</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
<TR VALIGN=top><TD>
<B>get_current_dir_name</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe env<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>getcwd</B>()
conforms to POSIX.1-2001.
Note however that POSIX.1-2001 leaves the behavior of
<B>getcwd</B>()
unspecified if
<I>buf</I>
is NULL.
<P>
<B>getwd</B>()
is present in POSIX.1-2001, but marked LEGACY.
POSIX.1-2008 removes the specification of
<B>getwd</B>().
Use
<B>getcwd</B>()
instead.
POSIX.1-2001
does not define any errors for
<B>getwd</B>().
<P>
<B>get_current_dir_name</B>()
is a GNU extension.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Under Linux, these functions make use of the
<B>getcwd</B>()
system call (available since Linux 2.1.92).
On older systems they would query
<I>/proc/self/cwd</I>.
If both system call and proc filesystem are missing, a
generic implementation is called.
Only in that case can
these calls fail under Linux with
<B>EACCES</B>.
<P>
These functions are often used to save the location of the current working
directory for the purpose of returning to it later.
Opening the current
directory (&quot;.&quot;) and calling
<B><A HREF="/cgi-bin/man/man2html?2+fchdir">fchdir</A></B>(2)
to return is usually a faster and more reliable alternative when sufficiently
many file descriptors are available, especially on platforms other than Linux.
<A NAME="lbAJ">&nbsp;</A>
<H3>C library/kernel differences</H3>
On Linux, the kernel provides a
<B>getcwd</B>()
system call, which the functions described in this page will use if possible.
The system call takes the same arguments as the library function
of the same name, but is limited to returning at most
<B>PATH_MAX</B>
bytes.
(Before Linux 3.12,
the limit on the size of the returned pathname was the system page size.
On many architectures,
<B>PATH_MAX</B>
and the system page size are both 4096 bytes,
but a few architectures have a larger page size.)
If the length of the pathname of the current working directory
exceeds this limit, then the system call fails with the error
<B>ENAMETOOLONG</B>.
In this case, the library functions fall back to
a (slower) alternative implementation that returns the full pathname.
<P>
Following a change in Linux 2.6.36,
the pathname returned by the
<B>getcwd</B>()
system call will be prefixed with the string &quot;(unreachable)&quot;
if the current directory is not below the root directory of the current
process (e.g., because the process set a new filesystem root using
<B><A HREF="/cgi-bin/man/man2html?2+chroot">chroot</A></B>(2)
without changing its current directory into the new root).
Such behavior can also be caused by an unprivileged user by changing
the current directory into another mount namespace.
When dealing with pathname from untrusted sources, callers of the
functions described in this page
should consider checking whether the returned pathname starts
with '/' or '(' to avoid misinterpreting an unreachable path
as a relative pathname.
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
Since the Linux 2.6.36 change that added &quot;(unreachable)&quot; in the
circumstances described above, the glibc implementation of
<B>getcwd</B>()
has failed to conform to POSIX and returned a relative pathname when the API
contract requires an absolute pathname.
With glibc 2.27 onwards this is corrected;
calling
<B>getcwd</B>()
from such a pathname will now result in failure with
<B>ENOENT</B>.
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+pwd">pwd</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+chdir">chdir</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fchdir">fchdir</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+unlink">unlink</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="13"><A HREF="#lbAB">NAME</A><DD>
<DT id="14"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="15"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="16"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="17"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="18"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="19"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="20"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="21"><A HREF="#lbAJ">C library/kernel differences</A><DD>
</DL>
<DT id="22"><A HREF="#lbAK">BUGS</A><DD>
<DT id="23"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="24"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:44 GMT, March 31, 2021
</BODY>
</HTML>