getcwd3
Get working directory pathname
Usage
char *
char *
Library
Standard C Library (libc, -lc)
Overview
The getcwd() function copies the absolute pathname of the current working directory into the memory referenced by buf and returns a pointer to buf. The size argument is the size, in bytes, of the array referenced by buf.
If buf is NULL, space is allocated as necessary to store the pathname. This space may later be free(3)'d.
The function getwd() is a compatibility routine which calls getcwd() with its buf argument and a size of MAXPATHLEN (as defined in the include file ⟨sys/param.h⟩). Obviously, buf should be at least MAXPATHLEN bytes in length.
These routines have traditionally been used by programs to save the name of a working directory for the purpose of returning to it. A much faster and less error-prone method of accomplishing this is to open the current directory (‘.’) and use the fchdir(2) function to return.
Return values
Upon successful completion, a pointer to the pathname is returned. Otherwise a NULL pointer is returned and the global variable errno is set to indicate the error. In addition, getwd() copies the error message associated with errno into the memory referenced by buf.
Errors
The getcwd() function will fail if:
- [EINVAL]
-
The size argument is zero.
- [ENOENT]
-
A component of the pathname no longer exists.
- [ENOMEM]
-
Insufficient memory is available.
- [ERANGE]
-
The size argument is greater than zero but smaller than the length of the pathname plus 1.
The getcwd() function may fail if:
- [EACCES]
-
Read or search permission was denied for a component of the pathname. This is only checked in limited cases, depending on implementation details.
History
- FreeBSD 16.0-CURRENT (2026-10-27) to FreeBSD 15.0-RELEASE: the text shown here
- FreeBSD 14.5-RELEASE to FreeBSD 13.3-RELEASE
- FreeBSD 13.2-RELEASE to FreeBSD 13.0-RELEASE
- FreeBSD 12.4-RELEASE
- FreeBSD 12.3-RELEASE
- FreeBSD 12.2-RELEASE
- FreeBSD 12.1-RELEASE
- FreeBSD 12.0-RELEASE
- FreeBSD 11.4-RELEASE
- FreeBSD 11.3-RELEASE
- FreeBSD 11.2-RELEASE
- FreeBSD 11.1-RELEASE
- FreeBSD 11.0-RELEASE
- FreeBSD 10.4-RELEASE
- FreeBSD 10.3-RELEASE
- FreeBSD 10.2-RELEASE
- FreeBSD 10.1-RELEASE
- FreeBSD 10.0-RELEASE
- FreeBSD 9.3-RELEASE
- FreeBSD 9.2-RELEASE
- FreeBSD 9.1-RELEASE
- FreeBSD 9.0-RELEASE
- FreeBSD 8.4-RELEASE
- FreeBSD 8.3-RELEASE
- FreeBSD 8.2-RELEASE
- FreeBSD 8.1-RELEASE
- FreeBSD 8.0-RELEASE
- FreeBSD 7.4-RELEASE
- FreeBSD 7.3-RELEASE
- FreeBSD 7.2-RELEASE
- FreeBSD 7.1-RELEASE
- FreeBSD 7.0-RELEASE
- FreeBSD 6.4-RELEASE
- FreeBSD 6.3-RELEASE to FreeBSD 5.2-RELEASE
- FreeBSD 5.1-RELEASE to FreeBSD 5.0-RELEASE
- FreeBSD 4.11-RELEASE to FreeBSD 4.5-RELEASE
- FreeBSD 4.4-RELEASE
- FreeBSD 4.3-RELEASE
- FreeBSD 4.2-RELEASE to FreeBSD 4.1-RELEASE
- FreeBSD 4.0-RELEASE
- FreeBSD 3.5.1-RELEASE to FreeBSD 3.5-RELEASE
- FreeBSD 3.4-RELEASE to FreeBSD 3.3-RELEASE
- FreeBSD 3.2-RELEASE to FreeBSD 3.0-RELEASE
- FreeBSD 2.2.9-RELEASE
- FreeBSD 2.2.8-RELEASE to FreeBSD 2.2.6-RELEASE
- FreeBSD 2.2.5-RELEASE to FreeBSD 2.1.5-RELEASE
- FreeBSD 2.0.5-RELEASE