1GETCWD(3)                  Linux Programmer's Manual                 GETCWD(3)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

31       These  functions return a null-terminated string containing an absolute
32       pathname that is the current working directory of the calling  process.
33       The  pathname  is  returned as the function result and via the argument
34       buf, if present.
35
36       If the current directory is not below the root directory of the current
37       process  (e.g.,  because  the  process  set a new filesystem root using
38       chroot(2) without changing its current directory into  the  new  root),
39       then,  since  Linux 2.6.36, the returned path will be prefixed with the
40       string "(unreachable)".  Such behavior can also be caused by an unpriv‐
41       ileged user by changing the current directory into another mount names‐
42       pace.  When dealing with paths from untrusted sources, callers of these
43       functions  should  consider  checking  whether the returned path starts
44       with '/' or '(' to avoid misinterpreting an unreachable path as a rela‐
45       tive path.  This is no longer true under some C libraries; see NOTES.
46
47       The  getcwd() function copies an absolute pathname of the current work‐
48       ing directory to the array pointed to by buf, which is of length size.
49
50       If the length of the absolute pathname of the  current  working  direc‐
51       tory,  including the terminating null byte, exceeds size bytes, NULL is
52       returned, and errno is set to ERANGE; an application should  check  for
53       this error, and allocate a larger buffer if necessary.
54
55       As  an  extension  to the POSIX.1-2001 standard, glibc's getcwd() allo‐
56       cates the buffer dynamically using malloc(3) if buf is NULL.   In  this
57       case,  the  allocated  buffer  has the length size unless size is zero,
58       when buf is allocated as big as necessary.  The caller  should  free(3)
59       the returned buffer.
60
61       get_current_dir_name()  will  malloc(3) an array big enough to hold the
62       absolute pathname of the current working directory.  If the environment
63       variable  PWD is set, and its value is correct, then that value will be
64       returned.  The caller should free(3) the returned buffer.
65
66       getwd() does not malloc(3) any memory.  The buf argument  should  be  a
67       pointer to an array at least PATH_MAX