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               _BSD_SOURCE ||
25                   (_XOPEN_SOURCE >= 500 ||
26                       _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
27                   !(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
28           Before glibc 2.12:
29               _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
30               _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
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, Linux (libc4, libc5,
47       glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf
48       is NULL.  In this case, the allocated buffer has the length size unless
49       size is zero, when buf is allocated as big as  necessary.   The  caller
50       should free(3) 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       file system, 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  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.  The contents of the array pointed to by buf  are  undefined
73       on error.
74

ERRORS

76       EACCES Permission  to  read  or  search a component of the filename was
77              denied.
78
79       EFAULT buf points to a bad address.
80
81       EINVAL The size argument is zero and buf is not a NULL pointer.
82
83       EINVAL getwd(): buf is NULL.
84
85       ENAMETOOLONG
86              getwd(): The  size  of  the  null-terminated  absolute  pathname
87              string exceeds PATH_MAX bytes.
88
89       ENOENT The current working directory has been unlinked.
90
91       ERANGE The  size argument is less than the length of the absolute path‐
92              name of the working directory, including  the  terminating  null
93              byte.  You need to allocate a bigger array and try again.
94

CONFORMING TO

96       getcwd()  conforms  to  POSIX.1-2001.   Note  however that POSIX.1-2001
97       leaves the behavior of getcwd() unspecified if buf is NULL.
98
99       getwd() is present in POSIX.1-2001, but  marked  LEGACY.   POSIX.1-2008
100       removes   the   specification   of   getwd().   Use  getcwd()  instead.
101       POSIX.1-2001 does not define any errors for getwd().
102
103       get_current_dir_name() is a GNU extension.
104

NOTES

106       Under Linux, the function getcwd() is a system call (since 2.1.92).  On
107       older  systems  it would query /proc/self/cwd.  If both system call and
108       proc file system are missing, a generic implementation is called.  Only
109       in that case can these calls fail under Linux with EACCES.
110
111       These  functions  are  often  used  to save the location of the current
112       working directory for the purpose of returning to  it  later.   Opening
113       the  current directory (".") and calling fchdir(2) to return is usually
114       a faster and more reliable  alternative  when  sufficiently  many  file
115       descriptors are available, especially on platforms other than Linux.
116

SEE ALSO

118       chdir(2), fchdir(2), open(2), unlink(2), free(3), malloc(3)
119

COLOPHON

121       This  page  is  part of release 3.53 of the Linux man-pages project.  A
122       description of the project, and information about reporting  bugs,  can
123       be found at http://www.kernel.org/doc/man-pages/.
124
125
126
127GNU                               2010-09-20                         GETCWD(3)
Impressum