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

NAME

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

SYNOPSIS

9       #include <unistd.h>
10
11       char *getcwd(char *buf, size_t size);
12
13       #define _BSD_SOURCE     /* Or: #define _XOPEN_SOURCE 500 */
14       #include <unistd.h>
15
16       char *getwd(char *buf);
17
18       #define _GNU_SOURCE
19       #include <unistd.h>
20
21       char *get_current_dir_name(void);
22

DESCRIPTION

24       The  getcwd() function copies an absolute pathname of the current work‐
25       ing directory to the array pointed to by buf, which is of length size.
26
27       If the current absolute pathname would require  a  buffer  longer  than
28       size  elements, NULL is returned, and errno is set to ERANGE; an appli‐
29       cation should check for this error, and allocate  a  larger  buffer  if
30       necessary.
31
32       If buf is NULL, the behaviour of getcwd() is undefined.
33
34       As  an  extension  to  the  POSIX.1-2001 standard, Linux (libc4, libc5,
35       glibc) getcwd() allocates the buffer dynamically using malloc() if  buf
36       is  NULL  on  call.   In this case, the allocated buffer has the length
37       size unless size is zero, when buf is allocated as  big  as  necessary.
38       It  is  possible (and, indeed, advisable) to free() the buffers if they
39       have been obtained this way.
40
41       get_current_dir_name(), will malloc(3) an array big enough to hold  the
42       current  directory  name.   If the environment variable PWD is set, and
43       its value is correct, then that value will be returned.
44
45       getwd(), does not malloc(3) any memory.  The buf argument should  be  a
46       pointer  to  an  array at least PATH_MAX bytes long.  getwd() does only
47       return the first PATH_MAX bytes of  the  actual  pathname.   Note  that
48       PATH_MAX  need  not  be  a  compile-time constant; it may depend on the
49       filesystem and may even be unlimited.   For  portability  and  security
50       reasons, use of getwd() is deprecated.
51

RETURN VALUE

53       NULL  on  failure  with  errno set accordingly, and buf on success. The
54       contents of the array pointed to by buf is undefined on error.
55

ERRORS

57       EACCES Permission to read or search a component  of  the  filename  was
58              denied.
59
60       EFAULT buf points to a bad address.
61
62       EINVAL The size argument is zero and buf is not a null pointer.
63
64       ENOENT The current working directory has been unlinked.
65
66       ERANGE The  size argument is less than the length of the working direc‐
67              tory name.  You need to allocate a bigger array and try again.
68

NOTES

70       Under Linux, the function getcwd() is a system call (since 2.1.92).  On
71       older  systems  it would query /proc/self/cwd.  If both system call and
72       proc file system are missing, a generic implementation is called.  Only
73       in that case can these calls fail under Linux with EACCES.
74
75       These  functions  are  often  used  to save the location of the current
76       working directory for the purpose of returning to it later. Opening the
77       current  directory  (".")  and calling fchdir(2) to return is usually a
78       faster and  more  reliable  alternative  when  sufficiently  many  file
79       descriptors are available, especially on platforms other than Linux.
80

CONFORMING TO

82       getcwd() conforms to POSIX.1-2001.  getwd() is present in POSIX.1-2001,
83       but marked LEGACY.  get_current_dir_name() is a GNU extension.
84

SEE ALSO

86       chdir(2),  fchdir(2),  open(2),  unlink(2),  free(3),  malloc(3),  fea‐
87       ture_test_macros(7)
88
89
90
91GNU                               2002-04-22                         GETCWD(3)
Impressum