1GETCWD(3P) POSIX Programmer's Manual GETCWD(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 getcwd - get the pathname of the current working directory
13
15 #include <unistd.h>
16
17 char *getcwd(char *buf, size_t size);
18
19
21 The getcwd() function shall place an absolute pathname of the current
22 working directory in the array pointed to by buf, and return buf. The
23 pathname copied to the array shall contain no components that are sym‐
24 bolic links. The size argument is the size in bytes of the character
25 array pointed to by the buf argument. If buf is a null pointer, the
26 behavior of getcwd() is unspecified.
27
29 Upon successful completion, getcwd() shall return the buf argument.
30 Otherwise, getcwd() shall return a null pointer and set errno to indi‐
31 cate the error. The contents of the array pointed to by buf are then
32 undefined.
33
35 The getcwd() function shall fail if:
36
37 EINVAL The size argument is 0.
38
39 ERANGE The size argument is greater than 0, but is smaller than the
40 length of the pathname +1.
41
42
43 The getcwd() function may fail if:
44
45 EACCES Read or search permission was denied for a component of the
46 pathname.
47
48 ENOMEM Insufficient storage space is available.
49
50
51 The following sections are informative.
52
54 Determining the Absolute Pathname of the Current Working Directory
55 The following example returns a pointer to an array that holds the
56 absolute pathname of the current working directory. The pointer is
57 returned in the ptr variable, which points to the buf array where the
58 pathname is stored.
59
60
61 #include <stdlib.h>
62 #include <unistd.h>
63 ...
64 long size;
65 char *buf;
66 char *ptr;
67
68
69 size = pathconf(".", _PC_PATH_MAX);
70
71
72 if ((buf = (char *)malloc((size_t)size)) != NULL)
73 ptr = getcwd(buf, (size_t)size);
74 ...
75
77 None.
78
80 Since the maximum pathname length is arbitrary unless {PATH_MAX} is
81 defined, an application generally cannot supply a buf with size
82 {{PATH_MAX}+1}.
83
84 Having getcwd() take no arguments and instead use the malloc() function
85 to produce space for the returned argument was considered. The advan‐
8