1getcwd(3)                  Library Functions Manual                  getcwd(3)
2
3
4

NAME

6       getcwd, getwd, get_current_dir_name - get current working directory
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       char *getcwd(char buf[.size], size_t size);
15       char *get_current_dir_name(void);
16
17       [[deprecated]] char *getwd(char buf[PATH_MAX]);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       get_current_dir_name():
22           _GNU_SOURCE
23
24       getwd():
25           Since glibc 2.12:
26               (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
27                   || /* glibc >= 2.19: */ _DEFAULT_SOURCE
28                   || /* glibc <= 2.19: */ _BSD_SOURCE
29           Before glibc 2.12:
30               _BSD_SOURCE || _XOPEN_SOURCE >= 500
31

DESCRIPTION

33       These  functions return a null-terminated string containing an absolute
34       pathname that is the current working directory of the calling  process.
35       The  pathname  is  returned as the function result and via the argument
36       buf, if present.
37
38       The getcwd() function copies an absolute pathname of the current  work‐
39       ing directory to the array pointed to by buf, which is of length size.
40
41       If  the  length  of the absolute pathname of the current working direc‐
42       tory, including the terminating null byte, exceeds size bytes, NULL  is
43       returned,  and  errno is set to ERANGE; an application should check for
44       this error, and allocate a larger buffer if necessary.
45
46       As an extension to the POSIX.1-2001 standard,  glibc's  getcwd()  allo‐
47       cates  the  buffer dynamically using malloc(3) if buf is NULL.  In this
48       case, the allocated buffer has the length size  unless  size  is  zero,
49       when  buf  is allocated as big as necessary.  The caller should free(3)
50       the returned buffer.
51
52       get_current_dir_name() will malloc(3) an array big enough to  hold  the
53       absolute pathname of the current working directory.  If the environment
54       variable PWD is set, and its value is correct, then that value will  be
55       returned.  The caller should free(3) the returned buffer.
56
57       getwd()  does  not  malloc(3) any memory.  The buf argument should be a
58       pointer to an array at least PATH_MAX bytes long.  If the length of the
59       absolute  pathname of the current working directory, including the ter‐
60       minating null byte, exceeds PATH_MAX bytes, NULL is returned, and errno
61       is  set  to ENAMETOOLONG.  (Note that on some systems, PATH_MAX may not
62       be a compile-time constant; furthermore, its value may  depend  on  the
63       filesystem,  see  pathconf(3).)   For portability and security reasons,
64       use of getwd() is deprecated.
65

RETURN VALUE

67       On success, these functions return a pointer to a string containing the
68       pathname of the current working directory.  In the case of getcwd() and
69       getwd() this is the same value as buf.
70
71       On failure, these functions return NULL, and errno is set  to  indicate
72       the  error.   The contents of the array pointed to by buf are undefined
73       on error.
74

ERRORS

76       EACCES Permission to read or search a component of the filename was de‐
77              nied.
78
79       EFAULT buf points to a bad address.
80
81       EINVAL The size argument is zero and buf is not a null pointer.
82
83       EINVAL getwd(): buf is NULL.
84
85       ENAMETOOLONG
86              getwd():  The  size  of  the  null-terminated  absolute pathname
87              string exceeds PATH_MAX bytes.
88
89       ENOENT The current working directory has been unlinked.
90
91       ENOMEM Out of memory.
92
93       ERANGE The size argument is less than the length of the absolute  path‐
94              name  of  the  working directory, including the terminating null
95              byte.  You need to allocate a bigger array and try again.
96

ATTRIBUTES

98       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
99       tributes(7).
100
101       ┌────────────────────────────────────────┬───────────────┬─────────────┐
102Interface                               Attribute     Value       
103       ├────────────────────────────────────────┼───────────────┼─────────────┤
104getcwd(), getwd()                       │ Thread safety │ MT-Safe     │
105       ├────────────────────────────────────────┼───────────────┼─────────────┤
106get_current_dir_name()                  │ Thread safety │ MT-Safe env │
107       └────────────────────────────────────────┴───────────────┴─────────────┘
108

VERSIONS

110       POSIX.1-2001  leaves  the  behavior  of  getcwd() unspecified if buf is
111       NULL.
112
113       POSIX.1-2001 does not define any errors for getwd().
114

VERSIONS

116   C library/kernel differences
117       On Linux, the  kernel  provides  a  getcwd()  system  call,  which  the
118       functions described in this page will use if possible.  The system call
119       takes the same arguments as the library function of the same name,  but
120       is  limited  to  returning at most PATH_MAX bytes.  (Before Linux 3.12,
121       the limit on the size of the returned  pathname  was  the  system  page
122       size.   On  many  architectures,  PATH_MAX and the system page size are
123       both 4096 bytes, but a few architectures have a larger page size.)   If
124       the  length  of  the  pathname of the current working directory exceeds
125       this limit, then the system call fails with the error ENAMETOOLONG.  In
126       this  case,  the  library functions fall back to a (slower) alternative
127       implementation that returns the full pathname.
128
129       Following a change in  Linux  2.6.36,  the  pathname  returned  by  the
130       getcwd()  system  call will be prefixed with the string "(unreachable)"
131       if the current directory is not below the root directory of the current
132       process  (e.g.,  because  the  process  set a new filesystem root using
133       chroot(2) without changing its current directory into  the  new  root).
134       Such  behavior  can  also be caused by an unprivileged user by changing
135       the current directory into another mount namespace.  When dealing  with
136       pathname  from untrusted sources, callers of the functions described in
137       this page should consider checking whether the returned pathname starts
138       with  '/'  or  '('  to  avoid  misinterpreting an unreachable path as a
139       relative pathname.
140

STANDARDS

142       getcwd()
143              POSIX.1-2008.
144
145       get_current_dir_name()
146              GNU.
147
148       getwd()
149              None.
150

HISTORY

152       getcwd()
153              POSIX.1-2001.
154
155       getwd()
156              POSIX.1-2001, but marked LEGACY.  Removed in POSIX.1-2008.   Use
157              getcwd() instead.
158
159       Under  Linux,  these  functions  make  use  of the getcwd() system call
160       (available since Linux 2.1.92).  On  older  systems  they  would  query
161       /proc/self/cwd.  If both system call and proc filesystem are missing, a
162       generic implementation is called.  Only in that case  can  these  calls
163       fail under Linux with EACCES.
164

NOTES

166       These  functions  are  often  used  to save the location of the current
167       working directory for the purpose of returning to  it  later.   Opening
168       the  current directory (".") and calling fchdir(2) to return is usually
169       a faster and more reliable  alternative  when  sufficiently  many  file
170       descriptors are available, especially on platforms other than Linux.
171

BUGS

173       Since  the  Linux  2.6.36  change  that  added  "(unreachable)"  in the
174       circumstances described above, the glibc implementation of getcwd() has
175       failed  to  conform  to POSIX and returned a relative pathname when the
176       API contract requires an absolute pathname.  With  glibc  2.27  onwards
177       this  is  corrected;  calling  getcwd()  from  such a pathname will now
178       result in failure with ENOENT.
179

SEE ALSO

181       pwd(1), chdir(2), fchdir(2), open(2), unlink(2), free(3), malloc(3)
182
183
184
185Linux man-pages 6.05              2023-07-20                         getcwd(3)
Impressum