getcwd(3) Library Functions Manual getcwd(3) getcwd, getwd, get_current_dir_name - LIBRARY Standard C library (libc, -lc) #include char *getcwd(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(): Since glibc 2.12: (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L) || /* glibc >= 2.19: */ _DEFAULT_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE Before 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. () , . Linux 2.6.36, , getcwd(), <<(unreachable)>>, (, - , chroot(2) ). . , <> <<(>>, . getcwd() POSIX.1-2008. get_current_dir_name() GNU. getwd() None. 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 ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . getcwd(3)