2004-11-03 13:51:07 +00:00
|
|
|
.\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" License.
|
|
|
|
.\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
|
|
|
|
.\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
|
|
|
|
.\" Corrected description of getwd().
|
|
|
|
.\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
|
|
|
|
.\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
|
|
|
|
.\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
|
|
|
|
.\"
|
|
|
|
.TH GETCWD 3 2002-04-22 "GNU" "Linux Programmer's Manual"
|
|
|
|
.SH NAME
|
|
|
|
getcwd, get_current_dir_name, getwd \- Get current working directory
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <unistd.h>
|
|
|
|
.sp
|
|
|
|
.BI "char *getcwd(char *" buf ", size_t " size );
|
2006-08-08 16:25:48 +00:00
|
|
|
.sp
|
|
|
|
.B "#define _BSD_SOURCE /* Or: #define _XOPEN_SOURCE 500 */"
|
|
|
|
.B #include <unistd.h>
|
|
|
|
.sp
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "char *getwd(char *" buf );
|
2006-08-08 16:25:48 +00:00
|
|
|
.sp
|
|
|
|
.B #define _GNU_SOURCE
|
|
|
|
.B #include <unistd.h>
|
|
|
|
.sp
|
|
|
|
.B "char *get_current_dir_name(void);"
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
|
|
|
.BR getcwd ()
|
|
|
|
function copies an absolute pathname of the current working directory
|
|
|
|
to the array pointed to by
|
|
|
|
.IR buf ,
|
|
|
|
which is of length
|
|
|
|
.IR size .
|
|
|
|
.PP
|
2006-02-12 22:19:08 +00:00
|
|
|
If the current absolute pathname would require a buffer longer than
|
2004-11-03 13:51:07 +00:00
|
|
|
.I size
|
2005-11-02 13:55:25 +00:00
|
|
|
elements, NULL is returned, and
|
2004-11-03 13:51:07 +00:00
|
|
|
.I errno
|
|
|
|
is set to
|
|
|
|
.BR ERANGE ;
|
|
|
|
an application should check for this error, and allocate a larger
|
|
|
|
buffer if necessary.
|
|
|
|
.PP
|
|
|
|
If
|
|
|
|
.I buf
|
|
|
|
is NULL, the behaviour of
|
|
|
|
.BR getcwd ()
|
|
|
|
is undefined.
|
|
|
|
.PP
|
2006-08-04 12:39:17 +00:00
|
|
|
As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getcwd ()
|
2004-11-03 13:51:07 +00:00
|
|
|
allocates the buffer dynamically using
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
if
|
|
|
|
.I buf
|
2005-11-02 13:55:25 +00:00
|
|
|
is NULL on call.
|
|
|
|
In this case, the allocated buffer has the length
|
2004-11-03 13:51:07 +00:00
|
|
|
.I size
|
|
|
|
unless
|
|
|
|
.I size
|
|
|
|
is zero, when
|
|
|
|
.I buf
|
|
|
|
is allocated as big as necessary. It is possible (and, indeed,
|
|
|
|
advisable) to
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR free ()
|
2004-11-03 13:51:07 +00:00
|
|
|
the buffers if they have been obtained this way.
|
|
|
|
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR get_current_dir_name (),
|
2006-08-08 16:25:48 +00:00
|
|
|
will
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR malloc (3)
|
|
|
|
an array big enough to hold the current directory name. If the environment
|
|
|
|
variable
|
|
|
|
.B PWD
|
|
|
|
is set, and its value is correct, then that value will be returned.
|
|
|
|
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR getwd (),
|
2006-08-08 16:25:48 +00:00
|
|
|
does not
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR malloc (3)
|
2006-08-08 16:25:48 +00:00
|
|
|
any memory.
|
|
|
|
The
|
2004-11-03 13:51:07 +00:00
|
|
|
.I buf
|
|
|
|
argument should be a pointer to an array at least
|
|
|
|
.B PATH_MAX
|
|
|
|
bytes long.
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR getwd ()
|
2004-11-03 13:51:07 +00:00
|
|
|
does only return the first
|
|
|
|
.B PATH_MAX
|
|
|
|
bytes of the actual pathname.
|
|
|
|
Note that
|
|
|
|
.B PATH_MAX
|
|
|
|
need not be a compile-time constant; it may depend on the filesystem
|
2006-08-08 16:25:48 +00:00
|
|
|
and may even be unlimited.
|
|
|
|
For portability and security reasons, use of
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR getwd ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is deprecated.
|
|
|
|
.SH "RETURN VALUE"
|
2005-11-02 13:55:25 +00:00
|
|
|
NULL
|
2004-11-03 13:51:07 +00:00
|
|
|
on failure with
|
|
|
|
.I errno
|
|
|
|
set accordingly, and
|
|
|
|
.I buf
|
|
|
|
on success. The contents of the array pointed to by
|
|
|
|
.IR buf
|
|
|
|
is undefined on error.
|
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
|
|
|
.B EACCES
|
2006-02-12 22:15:41 +00:00
|
|
|
Permission to read or search a component of the filename was denied.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EFAULT
|
|
|
|
.IR buf
|
|
|
|
points to a bad address.
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
The
|
|
|
|
.IR size
|
|
|
|
argument is zero and
|
|
|
|
.IR buf
|
|
|
|
is not a null pointer.
|
|
|
|
.TP
|
|
|
|
.B ENOENT
|
|
|
|
The current working directory has been unlinked.
|
|
|
|
.TP
|
|
|
|
.B ERANGE
|
|
|
|
The
|
|
|
|
.IR size
|
|
|
|
argument is less than the length of the working directory name.
|
|
|
|
You need to allocate a bigger array and try again.
|
|
|
|
.SH NOTES
|
|
|
|
Under Linux, the function
|
|
|
|
.BR getcwd ()
|
|
|
|
is a system call (since 2.1.92).
|
|
|
|
On older systems it would query
|
|
|
|
.IR /proc/self/cwd .
|
|
|
|
If both system call and proc file system are missing, a
|
|
|
|
generic implementation is called. Only in that case can
|
|
|
|
these calls fail under Linux with
|
|
|
|
.BR EACCES .
|
|
|
|
.LP
|
|
|
|
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 (".") and calling
|
|
|
|
.BR fchdir (2)
|
|
|
|
to return is usually a faster and more reliable alternative when sufficiently
|
|
|
|
many file descriptors are available, especially on platforms other than Linux.
|
|
|
|
.SH "CONFORMING TO"
|
2006-08-08 16:25:48 +00:00
|
|
|
.BR getcwd()
|
|
|
|
conforms to POSIX.1-2001.
|
|
|
|
.BR getwd()
|
|
|
|
is present in POSIX.1-2001, but marked LEGACY.
|
|
|
|
.BR get_current_dir_name ()
|
|
|
|
is a GNU extension.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR chdir (2),
|
|
|
|
.BR fchdir (2),
|
|
|
|
.BR open (2),
|
|
|
|
.BR unlink (2),
|
|
|
|
.BR free (3),
|
2006-04-21 06:49:34 +00:00
|
|
|
.BR malloc (3),
|
2006-05-15 09:13:10 +00:00
|
|
|
.BR feature_test_macros (7)
|