getcwd(3) Library Functions Manual getcwd(3) getcwd, getwd, get_current_dir_name - C (libc, -lc) #include char *getcwd(size_t size; char buf[size], size_t size); char *get_current_dir_name(void); [[deprecated]] char *getwd(char buf[PATH_MAX]); glibc (. feature_test_macros(7)): get_current_dir_name(): _GNU_SOURCE getwd(): glibc 2.12: (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L) || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE glibc 2.12: _BSD_SOURCE || _XOPEN_SOURCE >= 500 ( null ), . buf, . getcwd() , buf, size. , null, size , NULL, errno ERANGE; , , , . POSIX.1-2001 glibc : buf NULL, getcwd() malloc(3). size; size , buf . free(3). get_current_dir_name() malloc(3) , . PWD, . free(3). getwd() malloc(3). buf PATH_MAX . , null, PATH_MAX , NULL errno ENAMETOOLONG (, PATH_MAX ; , , pathconf(3)). getwd() . On success, these functions return a pointer to a string containing the pathname of the current working directory. In the case of getcwd() and getwd() this is the same value as buf. NULL errno . buf . EACCES . EFAULT buf . EINVAL size , buf null. EINVAL getwd(): buf NULL. ENAMETOOLONG getwd(): , null, PATH_MAX . ENOENT . ENOMEM . ERANGE size , null. . attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getcwd(), getwd() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ |get_current_dir_name() | | MT-Safe env | +----------------------------+----------------------------------------------------------+--------------------------+ POSIX.1-2001 leaves the behavior of getcwd() unspecified if buf is NULL. POSIX.1-2001 does not define any errors for getwd(). C Linux getcwd(), , , . , PATH_MAX ( Linux 3.12 . PATH_MAX 4096 , ). , ENAMETOOLONG. () , . Following a change in Linux 2.6.36, the pathname returned by the getcwd() system call will be prefixed with the string "(unreachable)" if the current directory is not below the root directory of the current process (e.g., because the process set a new filesystem root using chroot(2) without changing its current directory into the new root). Such behavior can also be caused by an unprivileged user by changing the current directory into another mount namespace. When dealing with pathnames from untrusted sources, callers of the functions described in this page (before glibc 2.27) or the raw getcwd() system call should consider checking whether the returned pathname starts with '/' or '(' to avoid misinterpreting an unreachable path as a relative pathname. getcwd() POSIX.1-2008. get_current_dir_name() GNU. getwd() . getcwd() POSIX.1-2001. getwd() POSIX.1-2001, but marked LEGACY. Removed in POSIX.1-2008. Use getcwd() instead. Linux, getcwd() ( Linux, 2.1.92). /proc/self/cwd. proc, . Linux EACCES. . (<<.>>) fchdir(2) , , , , Linux. Linux 2.6.36 <<(unreachable)>>, getcwd() glibc POSIX , API . glibc 2.27 ; getcwd() ENOENT. pwd(1), chdir(2), fchdir(2), open(2), unlink(2), free(3), malloc(3) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.15 28 2025 . getcwd(3)