1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
26 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
27 .\" Corrected description of getwd().
28 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
29 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
30 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
32 .TH GETCWD 3 2014-08-19 "GNU" "Linux Programmer's Manual"
34 getcwd, getwd, get_current_dir_name \- get current working directory
37 .B #include <unistd.h>
39 .BI "char *getcwd(char *" buf ", size_t " size );
41 .BI "char *getwd(char *" buf );
43 .B "char *get_current_dir_name(void);"
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
51 .BR get_current_dir_name ():
64 (_XOPEN_SOURCE\ >=\ 500 ||
65 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
66 !(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
70 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
71 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
76 These functions return a null-terminated string containing an
77 absolute pathname that is the current working directory of
79 The pathname is returned as the function result and via the argument
85 function copies an absolute pathname of the current working directory
86 to the array pointed to by
91 If the length of the absolute pathname of the current working directory,
92 including the terminating null byte, exceeds
94 bytes, NULL is returned, and
98 an application should check for this error, and allocate a larger
101 As an extension to the POSIX.1-2001 standard, glibc's
103 allocates the buffer dynamically using
108 In this case, the allocated buffer has the length
114 is allocated as big as necessary.
119 .BR get_current_dir_name ()
122 an array big enough to hold the absolute pathname of
123 the current working directory.
127 is set, and its value is correct, then that value will be returned.
138 argument should be a pointer to an array at least
141 If the length of the absolute pathname of the current working directory,
142 including the terminating null byte, exceeds
144 bytes, NULL is returned, and
148 (Note that on some systems,
150 may not be a compile-time constant;
151 furthermore, its value may depend on the filesystem, see
153 For portability and security reasons, use of
157 On success, these functions return a pointer to a string containing
158 the pathname of the current working directory.
163 this is the same value as
166 On failure, these functions return NULL, and
168 is set to indicate the error.
169 The contents of the array pointed to by
171 are undefined on error.
175 Permission to read or search a component of the filename was denied.
179 points to a bad address.
186 is not a null pointer.
195 The size of the null-terminated absolute pathname string exceeds
200 The current working directory has been unlinked.
205 argument is less than the length of the absolute pathname of the
206 working directory, including the terminating null byte.
207 You need to allocate a bigger array and try again.
210 conforms to POSIX.1-2001.
211 Note however that POSIX.1-2001 leaves the behavior of
218 is present in POSIX.1-2001, but marked LEGACY.
219 POSIX.1-2008 removes the specification of
225 does not define any errors for
228 .BR get_current_dir_name ()
231 Under Linux, the function
233 is a system call (since 2.1.92).
234 On older systems it would query
236 If both system call and proc filesystem are missing, a
237 generic implementation is called.
238 Only in that case can
239 these calls fail under Linux with
242 These functions are often used to save the location of the current working
243 directory for the purpose of returning to it later.
245 directory (".") and calling
247 to return is usually a faster and more reliable alternative when sufficiently
248 many file descriptors are available, especially on platforms other than Linux.