1getcwd(3C) Standard C Library Functions getcwd(3C)
2
3
4
6 getcwd - get pathname of current working directory
7
9 #include <unistd.h>
10
11 char *getcwd(char *buf, size_t size);
12
13
15 The getcwd() function places an absolute pathname of the current work‐
16 ing directory in the array pointed to by buf, and returns buf. The
17 pathname copied to the array contains no components that are symbolic
18 links. The size argument is the size in bytes of the character array
19 pointed to by buf and must be at least one greater than the length of
20 the pathname to be returned.
21
22
23 If buf is not a null pointer, the pathname is stored in the space
24 pointed to by buf.
25
26
27 If buf is a null pointer, getcwd() obtains size bytes of space using
28 malloc(3C). The pointer returned by getcwd() can be used as the argu‐
29 ment in a subsequent call to free().
30
32 Upon successful completion, getcwd() returns the buf argument. If buf
33 is an invalid destination buffer address, NULL is returned and errno is
34 set to EFAULT. Otherwise, a null pointer is returned and errno is set
35 to indicate the error.
36
38 The getcwd() function will fail if:
39
40 EFAULT The buf argument is an invalid destination buffer address.
41
42
43 EINVAL The size argument is equal to 0.
44
45
46 ERANGE The size argument is greater than 0 and less than the length
47 of the pathname plus 1.
48
49
50
51 The getcwd() function may fail if:
52
53 EACCES A parent directory cannot be read to get its name.
54
55
56 ENOMEM Insufficient storage space is available.
57
58
60 Example 1 Determine the absolute pathname of the current working direc‐
61 tory.
62
63
64 The following example returns a pointer to an array that holds the
65 absolute pathname of the current working directory. The pointer is
66 returned in the ptr variable, which points to the buf array where the
67 pathname is stored.
68
69
70 #include <stdlib.h>
71 #include <unistd.h>
72 ...
73 long size;
74 char *buf;
75 char *ptr;
76 size = pathconf(".", _PC_PATH_MAX);
77 if ((buf = (char *)malloc((size_t)size)) != NULL)
78 ptr = getcwd(buf, (size_t)size);
79 ...
80
81
82 Example 2 Print the current working directory.
83
84
85 The following example prints the current working directory.
86
87
88 #include <unistd.h>
89 #include <stdio.h>
90
91 main()
92 {
93 char *cwd;
94 if ((cwd = getcwd(NULL, 64)) == NULL) {
95 perror("pwd");
96 exit(2);
97 }
98 (void)printf("%s\n", cwd);
99 free(cwd); /* free memory allocated by getcwd() */
100 return(0);
101 }
102
103
105 Applications should exercise care when using chdir(2) in conjunction
106 with getcwd(). The current working directory is global to all threads
107 within a process. If more than one thread calls chdir() to change the
108 working directory, a subsequent call to getcwd() could produce unex‐
109 pected results.
110
112 See attributes(5) for descriptions of the following attributes:
113
114
115
116
117 ┌─────────────────────────────┬─────────────────────────────┐
118 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
119 ├─────────────────────────────┼─────────────────────────────┤
120 │Interface Stability │Standard │
121 ├─────────────────────────────┼─────────────────────────────┤
122 │MT-Level │MT-Safe │
123 └─────────────────────────────┴─────────────────────────────┘
124
126 chdir(2), malloc(3C), attributes(5), standards(5)
127
128
129
130SunOS 5.11 18 Oct 2004 getcwd(3C)