]>
Commit | Line | Data |
---|---|---|
bf5a7247 | 1 | .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) |
fea681da | 2 | .\" |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
c08df37a | 24 | .\" |
fea681da MK |
25 | .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu) |
26 | .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de): | |
27 | .\" Corrected description of getwd(). | |
28 | .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj | |
29 | .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb | |
30 | .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch> | |
31 | .\" | |
09b8afdc | 32 | .TH GETCWD 3 2018-04-30 "GNU" "Linux Programmer's Manual" |
fea681da | 33 | .SH NAME |
f68512e9 | 34 | getcwd, getwd, get_current_dir_name \- get current working directory |
fea681da MK |
35 | .SH SYNOPSIS |
36 | .nf | |
37 | .B #include <unistd.h> | |
68e4db0a | 38 | .PP |
fea681da | 39 | .BI "char *getcwd(char *" buf ", size_t " size ); |
68e4db0a | 40 | .PP |
fea681da | 41 | .BI "char *getwd(char *" buf ); |
68e4db0a | 42 | .PP |
ecad088b | 43 | .B "char *get_current_dir_name(void);" |
fea681da | 44 | .fi |
68e4db0a | 45 | .PP |
cc4615cc MK |
46 | .in -4n |
47 | Feature Test Macro Requirements for glibc (see | |
48 | .BR feature_test_macros (7)): | |
49 | .in | |
68e4db0a | 50 | .PP |
cc4615cc | 51 | .BR get_current_dir_name (): |
a74ec77e | 52 | .RS |
cc4615cc | 53 | _GNU_SOURCE |
a74ec77e | 54 | .RE |
68e4db0a | 55 | .PP |
a89b9a38 MK |
56 | .BR getwd (): |
57 | .ad l | |
58 | .RS 4 | |
59 | .PD 0 | |
60 | .TP 4 | |
61 | Since glibc 2.12: | |
62 | .nf | |
6e0869f7 MK |
63 | (_XOPEN_SOURCE\ >=\ 500) && ! (_POSIX_C_SOURCE\ >=\ 200809L) |
64 | || /* Glibc since 2.19: */ _DEFAULT_SOURCE | |
65 | || /* Glibc versions <= 2.19: */ _BSD_SOURCE | |
a89b9a38 MK |
66 | .TP 4 |
67 | .fi | |
68 | Before glibc 2.12: | |
cf7fa0a1 MK |
69 | _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 |
70 | .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED | |
a89b9a38 MK |
71 | .PD |
72 | .RE | |
73 | .ad b | |
fea681da | 74 | .SH DESCRIPTION |
c359e37d MK |
75 | These functions return a null-terminated string containing an |
76 | absolute pathname that is the current working directory of | |
77 | the calling process. | |
78 | The pathname is returned as the function result and via the argument | |
79 | .IR buf , | |
80 | if present. | |
847e0d88 | 81 | .PP |
fea681da MK |
82 | The |
83 | .BR getcwd () | |
84 | function copies an absolute pathname of the current working directory | |
85 | to the array pointed to by | |
86 | .IR buf , | |
87 | which is of length | |
88 | .IR size . | |
89 | .PP | |
c359e37d MK |
90 | If the length of the absolute pathname of the current working directory, |
91 | including the terminating null byte, exceeds | |
fea681da | 92 | .I size |
c359e37d | 93 | bytes, NULL is returned, and |
fea681da MK |
94 | .I errno |
95 | is set to | |
96 | .BR ERANGE ; | |
97 | an application should check for this error, and allocate a larger | |
98 | buffer if necessary. | |
99 | .PP | |
acd67de4 | 100 | As an extension to the POSIX.1-2001 standard, glibc's |
63aa9df0 | 101 | .BR getcwd () |
fea681da | 102 | allocates the buffer dynamically using |
fb186734 | 103 | .BR malloc (3) |
fea681da MK |
104 | if |
105 | .I buf | |
c359e37d | 106 | is NULL. |
8478ee02 | 107 | In this case, the allocated buffer has the length |
fea681da MK |
108 | .I size |
109 | unless | |
110 | .I size | |
111 | is zero, when | |
112 | .I buf | |
c13182ef | 113 | is allocated as big as necessary. |
c359e37d | 114 | The caller should |
fb186734 | 115 | .BR free (3) |
c359e37d | 116 | the returned buffer. |
847e0d88 | 117 | .PP |
51d0a7e9 | 118 | .BR get_current_dir_name () |
ecad088b | 119 | will |
fea681da | 120 | .BR malloc (3) |
c359e37d MK |
121 | an array big enough to hold the absolute pathname of |
122 | the current working directory. | |
c13182ef | 123 | If the environment |
fea681da MK |
124 | variable |
125 | .B PWD | |
126 | is set, and its value is correct, then that value will be returned. | |
c359e37d MK |
127 | The caller should |
128 | .BR free (3) | |
129 | the returned buffer. | |
847e0d88 | 130 | .PP |
7847d5cd | 131 | .BR getwd () |
ecad088b | 132 | does not |
fea681da | 133 | .BR malloc (3) |
c13182ef | 134 | any memory. |
ecad088b | 135 | The |
fea681da MK |
136 | .I buf |
137 | argument should be a pointer to an array at least | |
138 | .B PATH_MAX | |
139 | bytes long. | |
c359e37d MK |
140 | If the length of the absolute pathname of the current working directory, |
141 | including the terminating null byte, exceeds | |
fea681da | 142 | .B PATH_MAX |
c359e37d MK |
143 | bytes, NULL is returned, and |
144 | .I errno | |
145 | is set to | |
146 | .BR ENAMETOOLONG . | |
b4338c3f MK |
147 | (Note that on some systems, |
148 | .B PATH_MAX | |
e8f4a1c9 | 149 | may not be a compile-time constant; |
9ee4a2b6 | 150 | furthermore, its value may depend on the filesystem, see |
b4338c3f | 151 | .BR pathconf (3).) |
ecad088b | 152 | For portability and security reasons, use of |
e511ffb6 | 153 | .BR getwd () |
fea681da | 154 | is deprecated. |
47297adb | 155 | .SH RETURN VALUE |
c359e37d MK |
156 | On success, these functions return a pointer to a string containing |
157 | the pathname of the current working directory. | |
158 | In the case | |
159 | .BR getcwd () | |
160 | and | |
161 | .BR getwd () | |
162 | this is the same value as | |
fe48639f | 163 | .IR buf . |
847e0d88 | 164 | .PP |
c359e37d | 165 | On failure, these functions return NULL, and |
fea681da | 166 | .I errno |
c359e37d | 167 | is set to indicate the error. |
c13182ef | 168 | The contents of the array pointed to by |
0daa9e92 | 169 | .I buf |
c359e37d | 170 | are undefined on error. |
fea681da MK |
171 | .SH ERRORS |
172 | .TP | |
173 | .B EACCES | |
c13182ef | 174 | Permission to read or search a component of the filename was denied. |
fea681da MK |
175 | .TP |
176 | .B EFAULT | |
0daa9e92 | 177 | .I buf |
fea681da MK |
178 | points to a bad address. |
179 | .TP | |
180 | .B EINVAL | |
c13182ef | 181 | The |
0daa9e92 | 182 | .I size |
fea681da | 183 | argument is zero and |
0daa9e92 | 184 | .I buf |
b437fdd9 | 185 | is not a null pointer. |
fea681da | 186 | .TP |
83685d1e | 187 | .B EINVAL |
c359e37d MK |
188 | .BR getwd (): |
189 | .I buf | |
190 | is NULL. | |
191 | .TP | |
83685d1e | 192 | .B ENAMETOOLONG |
c359e37d MK |
193 | .BR getwd (): |
194 | The size of the null-terminated absolute pathname string exceeds | |
195 | .B PATH_MAX | |
196 | bytes. | |
197 | .TP | |
fea681da MK |
198 | .B ENOENT |
199 | The current working directory has been unlinked. | |
200 | .TP | |
48f9dd59 MK |
201 | .B ENOMEM |
202 | Out of memory. | |
203 | .TP | |
fea681da MK |
204 | .B ERANGE |
205 | The | |
0daa9e92 | 206 | .I size |