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.