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       getcwd(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
20       get_current_dir_name(): _GNU_SOURCE
21

DESCRIPTION

23       These  functions return a null-terminated string containing an absolute
24       pathname that is the current working directory of the calling  process.
25       The  pathname  is  returned as the function result and via the argument
26       buf, if present.
27
28       The getcwd() function copies an absolute pathname of the current  work‐
29       ing directory to the array pointed to by buf, which is of length size.
30
31       If  the  length  of the absolute pathname of the current working direc‐
32       tory, including the terminating null byte, exceeds size bytes, NULL  is
33       returned,  and  errno is set to ERANGE; an application should check for
34       this error, and allocate a larger buffer if necessary.
35
36       As an extension to the  POSIX.1-2001  standard,  Linux  (libc4,  libc5,
37       glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf
38       is NULL.  In this case, the allocated buffer has the length size unless
39       size  is  zero,  when buf is allocated as big as necessary.  The caller
40       should free(3) the returned buffer.
41
42       get_current_dir_name() will malloc(3) an array big enough to  hold  the
43       absolute pathname of the current working directory.  If the environment
44       variable PWD is set, and its value is correct, then that value will  be
45       returned.  The caller should free(3) the returned buffer.
46
47       getwd()  does  not  malloc(3) any memory.  The buf argument should be a
48       pointer to an array at least PATH_MAX bytes long.  If the length of the
49       absolute  pathname of the current working directory, including the ter‐
50       minating null byte, exceeds PATH_MAX bytes, NULL is returned, and errno
51       is  set  to ENAMETOOLONG.  (Note that on some systems, PATH_MAX may not
52       be a compile-time constant; furthermore, its value may  depend  on  the
53       file  system,  see pathconf(3).)  For portability and security reasons,
54       use of getwd() is deprecated.
55

RETURN VALUE

57       On success, these functions return a pointer to a string containing the
58       pathname  of  the  current working directory.  In the case getcwd() and
59       getwd() this is the same value as buf.
60
61       On failure, these functions return NULL, and errno is set  to  indicate
62       the  error.   The contents of the array pointed to by buf are undefined
63       on error.
64

ERRORS

66       EACCES Permission to read or search a component  of  the  filename  was
67              denied.
68
69       EFAULT buf points to a bad address.
70
71       EINVAL The size argument is zero and buf is not a null pointer.
72
73       EINVAL getwd(): buf is NULL.
74
75       ENAMETOOLONG
76              getwd():  The  size  of  the  null-terminated  absolute pathname
77              string exceeds PATH_MAX bytes.
78
79       ENOENT The current working directory has been unlinked.
80
81       ERANGE The size argument is less than the length of the absolute  path‐
82              name  of  the  working directory, including the terminating null
83              byte.  You need to allocate a bigger array and try again.
84

CONFORMING TO

86       getcwd() conforms to  POSIX.1-2001.   Note  however  that  POSIX.1-2001
87       leaves the behavior of getcwd() unspecified if buf is NULL.
88
89       getwd()  is  present  in POSIX.1-2001, but marked LEGACY.  POSIX.1-2008
90       removes  the  specification  of   getwd().    Use   getcwd()   instead.
91       POSIX.1-2001 does not define any errors for getwd().
92
93       get_current_dir_name() is a GNU extension.
94

NOTES

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

SEE ALSO

108       chdir(2), fchdir(2), open(2), unlink(2), free(3), malloc(3)
109

COLOPHON

111       This page is part of release 3.22 of the Linux  man-pages  project.   A
112       description  of  the project, and information about reporting bugs, can
113       be found at http://www.kernel.org/doc/man-pages/.
114
115
116
117GNU                               2009-03-31                         GETCWD(3)
Impressum