1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
25 .\" Corrected description of getwd().
26 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
27 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
28 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
30 .TH GETCWD 3 2009-03-31 "GNU" "Linux Programmer's Manual"
32 getcwd, getwd, get_current_dir_name \- Get current working directory
35 .B #include <unistd.h>
37 .BI "char *getcwd(char *" buf ", size_t " size );
39 .BI "char *getwd(char *" buf );
41 .B "char *get_current_dir_name(void);"
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
50 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
52 .BR get_current_dir_name ():
55 These functions return a null-terminated string containing an
56 absolute pathname that is the current working directory of
58 The pathname is returned as the function result and via the argument
64 function copies an absolute pathname of the current working directory
65 to the array pointed to by
70 If the length of the absolute pathname of the current working directory,
71 including the terminating null byte, exceeds
73 bytes, NULL is returned, and
77 an application should check for this error, and allocate a larger
80 As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
82 allocates the buffer dynamically using
87 In this case, the allocated buffer has the length
93 is allocated as big as necessary.
98 .BR get_current_dir_name ()
101 an array big enough to hold the absolute pathname of
102 the current working directory.
106 is set, and its value is correct, then that value will be returned.
117 argument should be a pointer to an array at least
120 If the length of the absolute pathname of the current working directory,
121 including the terminating null byte, exceeds
123 bytes, NULL is returned, and
127 (Note that on some systems,
129 may not be a compile-time constant;
130 furthermore, its value may depend on the file system, see
132 For portability and security reasons, use of
136 On success, these functions return a pointer to a string containing
137 the pathname of the current working directory.
142 this is the same value as
145 On failure, these functions return NULL, and
147 is set to indicate the error.
148 The contents of the array pointed to by
150 are undefined on error.
154 Permission to read or search a component of the filename was denied.
158 points to a bad address.
165 is not a null pointer.
174 The size of the null-terminated absolute pathname string exceeds
179 The current working directory has been unlinked.
184 argument is less than the length of the absolute pathname of the
185 working directory, including the terminating null byte.
186 You need to allocate a bigger array and try again.
189 conforms to POSIX.1-2001.
190 Note however that POSIX.1-2001 leaves the behavior of
197 is present in POSIX.1-2001, but marked LEGACY.
198 POSIX.1-2008 removes the specification of
204 does not define any errors for
207 .BR get_current_dir_name ()
210 Under Linux, the function
212 is a system call (since 2.1.92).
213 On older systems it would query
215 If both system call and proc file system are missing, a
216 generic implementation is called.
217 Only in that case can
218 these calls fail under Linux with
221 These functions are often used to save the location of the current working
222 directory for the purpose of returning to it later.
224 directory (".") and calling
226 to return is usually a faster and more reliable alternative when sufficiently
227 many file descriptors are available, especially on platforms other than Linux.