Blame man3/getcwd.3

Packit 7cfc04
.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
Packit 7cfc04
.\"   Corrected description of getwd().
Packit 7cfc04
.\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
Packit 7cfc04
.\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
Packit 7cfc04
.\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETCWD 3 2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getcwd, getwd, get_current_dir_name \- get current working directory
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *getcwd(char *" buf ", size_t " size );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *getwd(char *" buf );
Packit 7cfc04
.PP
Packit 7cfc04
.B "char *get_current_dir_name(void);"
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR get_current_dir_name ():
Packit 7cfc04
.RS
Packit 7cfc04
_GNU_SOURCE
Packit 7cfc04
.RE
Packit 7cfc04
.PP
Packit 7cfc04
.BR getwd ():
Packit 7cfc04
.ad l
Packit 7cfc04
.RS 4
Packit 7cfc04
.PD 0
Packit 7cfc04
.TP 4
Packit 7cfc04
Since glibc 2.12:
Packit 7cfc04
.nf
Packit 7cfc04
(_XOPEN_SOURCE\ >=\ 500) && ! (_POSIX_C_SOURCE\ >=\ 200809L)
Packit 7cfc04
    || /* Glibc since 2.19: */ _DEFAULT_SOURCE
Packit 7cfc04
    || /* Glibc versions <= 2.19: */ _BSD_SOURCE
Packit 7cfc04
.TP 4
Packit 7cfc04
.fi
Packit 7cfc04
Before glibc 2.12:
Packit 7cfc04
_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
Packit 7cfc04
.\"    || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
Packit 7cfc04
.PD
Packit 7cfc04
.RE
Packit 7cfc04
.ad b
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
These functions return a null-terminated string containing an
Packit 7cfc04
absolute pathname that is the current working directory of
Packit 7cfc04
the calling process.
Packit 7cfc04
The pathname is returned as the function result and via the argument
Packit 7cfc04
.IR buf ,
Packit 7cfc04
if present.
Packit 7cfc04
.PP
Packit 7cfc04
If the current directory is not below the root directory of the current
Packit 7cfc04
process (e.g., because the process set a new filesystem root using
Packit 7cfc04
.BR chroot (2)
Packit 7cfc04
without changing its current directory into the new root),
Packit 7cfc04
then, since Linux 2.6.36,
Packit 7cfc04
.\" commit 8df9d1a4142311c084ffeeacb67cd34d190eff74
Packit 7cfc04
the returned path will be prefixed with the string "(unreachable)".
Packit 7cfc04
Such behavior can also be caused by an unprivileged user by changing
Packit 7cfc04
the current directory into another mount namespace.
Packit 7cfc04
When dealing with paths from untrusted sources, callers of these
Packit 7cfc04
functions should consider checking whether the returned path starts
Packit 7cfc04
with '/' or '(' to avoid misinterpreting an unreachable path
Packit 7cfc04
as a relative path.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
function copies an absolute pathname of the current working directory
Packit 7cfc04
to the array pointed to by
Packit 7cfc04
.IR buf ,
Packit 7cfc04
which is of length
Packit 7cfc04
.IR size .
Packit 7cfc04
.PP
Packit 7cfc04
If the length of the absolute pathname of the current working directory,
Packit 7cfc04
including the terminating null byte, exceeds
Packit 7cfc04
.I size
Packit 7cfc04
bytes, NULL is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR ERANGE ;
Packit 7cfc04
an application should check for this error, and allocate a larger
Packit 7cfc04
buffer if necessary.
Packit 7cfc04
.PP
Packit 7cfc04
As an extension to the POSIX.1-2001 standard, glibc's
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
allocates the buffer dynamically using
Packit 7cfc04
.BR malloc (3)
Packit 7cfc04
if
Packit 7cfc04
.I buf
Packit 7cfc04
is NULL.
Packit 7cfc04
In this case, the allocated buffer has the length
Packit 7cfc04
.I size
Packit 7cfc04
unless
Packit 7cfc04
.I size
Packit 7cfc04
is zero, when
Packit 7cfc04
.I buf
Packit 7cfc04
is allocated as big as necessary.
Packit 7cfc04
The caller should
Packit 7cfc04
.BR free (3)
Packit 7cfc04
the returned buffer.
Packit 7cfc04
.PP
Packit 7cfc04
.BR get_current_dir_name ()
Packit 7cfc04
will
Packit 7cfc04
.BR malloc (3)
Packit 7cfc04
an array big enough to hold the absolute pathname of
Packit 7cfc04
the current working directory.
Packit 7cfc04
If the environment
Packit 7cfc04
variable
Packit 7cfc04
.B PWD
Packit 7cfc04
is set, and its value is correct, then that value will be returned.
Packit 7cfc04
The caller should
Packit 7cfc04
.BR free (3)
Packit 7cfc04
the returned buffer.
Packit 7cfc04
.PP
Packit 7cfc04
.BR getwd ()
Packit 7cfc04
does not
Packit 7cfc04
.BR malloc (3)
Packit 7cfc04
any memory.
Packit 7cfc04
The
Packit 7cfc04
.I buf
Packit 7cfc04
argument should be a pointer to an array at least
Packit 7cfc04
.B PATH_MAX
Packit 7cfc04
bytes long.
Packit 7cfc04
If the length of the absolute pathname of the current working directory,
Packit 7cfc04
including the terminating null byte, exceeds
Packit 7cfc04
.B PATH_MAX
Packit 7cfc04
bytes, NULL is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to
Packit 7cfc04
.BR ENAMETOOLONG .
Packit 7cfc04
(Note that on some systems,
Packit 7cfc04
.B PATH_MAX
Packit 7cfc04
may not be a compile-time constant;
Packit 7cfc04
furthermore, its value may depend on the filesystem, see
Packit 7cfc04
.BR pathconf (3).)
Packit 7cfc04
For portability and security reasons, use of
Packit 7cfc04
.BR getwd ()
Packit 7cfc04
is deprecated.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, these functions return a pointer to a string containing
Packit 7cfc04
the pathname of the current working directory.
Packit 7cfc04
In the case
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
and
Packit 7cfc04
.BR getwd ()
Packit 7cfc04
this is the same value as
Packit 7cfc04
.IR buf .
Packit 7cfc04
.PP
Packit 7cfc04
On failure, these functions return NULL, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to indicate the error.
Packit 7cfc04
The contents of the array pointed to by
Packit 7cfc04
.I buf
Packit 7cfc04
are undefined on error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
Permission to read or search a component of the filename was denied.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
.I buf
Packit 7cfc04
points to a bad address.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The
Packit 7cfc04
.I size
Packit 7cfc04
argument is zero and
Packit 7cfc04
.I buf
Packit 7cfc04
is not a null pointer.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.BR getwd ():
Packit 7cfc04
.I buf
Packit 7cfc04
is NULL.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENAMETOOLONG
Packit 7cfc04
.BR getwd ():
Packit 7cfc04
The size of the null-terminated absolute pathname string exceeds
Packit 7cfc04
.B PATH_MAX
Packit 7cfc04
bytes.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
The current working directory has been unlinked.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Out of memory.
Packit 7cfc04
.TP
Packit 7cfc04
.B ERANGE
Packit 7cfc04
The
Packit 7cfc04
.I size
Packit 7cfc04
argument is less than the length of the absolute pathname of the
Packit 7cfc04
working directory, including the terminating null byte.
Packit 7cfc04
You need to allocate a bigger array and try again.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw22 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR getcwd (),
Packit 7cfc04
.BR getwd ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
T{
Packit 7cfc04
.BR get_current_dir_name ()
Packit 7cfc04
T}	Thread safety	MT-Safe env
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
conforms to POSIX.1-2001.
Packit 7cfc04
Note however that POSIX.1-2001 leaves the behavior of
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
unspecified if
Packit 7cfc04
.I buf
Packit 7cfc04
is NULL.
Packit 7cfc04
.PP
Packit 7cfc04
.BR getwd ()
Packit 7cfc04
is present in POSIX.1-2001, but marked LEGACY.
Packit 7cfc04
POSIX.1-2008 removes the specification of
Packit 7cfc04
.BR getwd ().
Packit 7cfc04
Use
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
instead.
Packit 7cfc04
POSIX.1-2001
Packit 7cfc04
does not define any errors for
Packit 7cfc04
.BR getwd ().
Packit 7cfc04
.PP
Packit 7cfc04
.BR get_current_dir_name ()
Packit 7cfc04
is a GNU extension.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Under Linux, the function
Packit 7cfc04
.BR getcwd ()
Packit 7cfc04
is a system call (since 2.1.92).
Packit 7cfc04
On older systems it would query
Packit 7cfc04
.IR /proc/self/cwd .
Packit 7cfc04
If both system call and proc filesystem are missing, a
Packit 7cfc04
generic implementation is called.
Packit 7cfc04
Only in that case can
Packit 7cfc04
these calls fail under Linux with
Packit 7cfc04
.BR EACCES .
Packit 7cfc04
.PP
Packit 7cfc04
These functions are often used to save the location of the current working
Packit 7cfc04
directory for the purpose of returning to it later.
Packit 7cfc04
Opening the current
Packit 7cfc04
directory (".") and calling
Packit 7cfc04
.BR fchdir (2)
Packit 7cfc04
to return is usually a faster and more reliable alternative when sufficiently
Packit 7cfc04
many file descriptors are available, especially on platforms other than Linux.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR pwd (1),
Packit 7cfc04
.BR chdir (2),
Packit 7cfc04
.BR fchdir (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR unlink (2),
Packit 7cfc04
.BR free (3),
Packit 7cfc04
.BR malloc (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.