OSDN Git Service

ea48188515ee69a859c5502425b7263cb4642d97
[linuxjm/LDP_man-pages.git] / original / man3 / getcwd.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
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 .\"
32 .TH GETCWD 3 2010-09-20 "GNU" "Linux Programmer's Manual"
33 .SH NAME
34 getcwd, getwd, get_current_dir_name \- get current working directory
35 .SH SYNOPSIS
36 .nf
37 .B #include <unistd.h>
38 .sp
39 .BI "char *getcwd(char *" buf ", size_t " size );
40 .sp
41 .BI "char *getwd(char *" buf );
42 .sp
43 .B "char *get_current_dir_name(void);"
44 .fi
45 .sp
46 .in -4n
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .sp
50 .in
51 .BR get_current_dir_name ():
52 .RS
53 _GNU_SOURCE
54 .RE
55 .sp
56 .BR getwd ():
57 .ad l
58 .RS 4
59 .PD 0
60 .TP 4
61 Since glibc 2.12:
62 .nf
63 _BSD_SOURCE ||
64     (_XOPEN_SOURCE\ >=\ 500 ||
65         _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
66     !(_POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700)
67 .TP 4
68 .fi
69 Before glibc 2.12:
70 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
71 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
72 .PD
73 .RE
74 .ad b
75 .SH DESCRIPTION
76 These functions return a null-terminated string containing an
77 absolute pathname that is the current working directory of
78 the calling process.
79 The pathname is returned as the function result and via the argument
80 .IR buf ,
81 if present.
82
83 The
84 .BR getcwd ()
85 function copies an absolute pathname of the current working directory
86 to the array pointed to by
87 .IR buf ,
88 which is of length
89 .IR size .
90 .PP
91 If the length of the absolute pathname of the current working directory,
92 including the terminating null byte, exceeds
93 .I size
94 bytes, NULL is returned, and
95 .I errno
96 is set to
97 .BR ERANGE ;
98 an application should check for this error, and allocate a larger
99 buffer if necessary.
100 .PP
101 As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
102 .BR getcwd ()
103 allocates the buffer dynamically using
104 .BR malloc (3)
105 if
106 .I buf
107 is NULL.
108 In this case, the allocated buffer has the length
109 .I size
110 unless
111 .I size
112 is zero, when
113 .I buf
114 is allocated as big as necessary.
115 The caller should
116 .BR free (3)
117 the returned buffer.
118
119 .BR get_current_dir_name ()
120 will
121 .BR malloc (3)
122 an array big enough to hold the absolute pathname of
123 the current working directory.
124 If the environment
125 variable
126 .B PWD
127 is set, and its value is correct, then that value will be returned.
128 The caller should
129 .BR free (3)
130 the returned buffer.
131
132 .BR getwd ()
133 does not
134 .BR malloc (3)
135 any memory.
136 The
137 .I buf
138 argument should be a pointer to an array at least
139 .B PATH_MAX
140 bytes long.
141 If the length of the absolute pathname of the current working directory,
142 including the terminating null byte, exceeds
143 .B PATH_MAX
144 bytes, NULL is returned, and
145 .I errno
146 is set to
147 .BR ENAMETOOLONG .
148 (Note that on some systems,
149 .B PATH_MAX
150 may not be a compile-time constant;
151 furthermore, its value may depend on the filesystem, see
152 .BR pathconf (3).)
153 For portability and security reasons, use of
154 .BR getwd ()
155 is deprecated.
156 .SH RETURN VALUE
157 On success, these functions return a pointer to a string containing
158 the pathname of the current working directory.
159 In the case
160 .BR getcwd ()
161 and
162 .BR getwd ()
163 this is the same value as
164 .IR buf .
165
166 On failure, these functions return NULL, and
167 .I errno
168 is set to indicate the error.
169 The contents of the array pointed to by
170 .I buf
171 are undefined on error.
172 .SH ERRORS
173 .TP
174 .B EACCES
175 Permission to read or search a component of the filename was denied.
176 .TP
177 .B EFAULT
178 .I buf
179 points to a bad address.
180 .TP
181 .B EINVAL
182 The
183 .I size
184 argument is zero and
185 .I buf
186 is not a null pointer.
187 .TP
188 EINVAL
189 .BR getwd ():
190 .I buf
191 is NULL.
192 .TP
193 ENAMETOOLONG
194 .BR getwd ():
195 The size of the null-terminated absolute pathname string exceeds
196 .B PATH_MAX
197 bytes.
198 .TP
199 .B ENOENT
200 The current working directory has been unlinked.
201 .TP
202 .B ERANGE
203 The
204 .I size
205 argument is less than the length of the absolute pathname of the
206 working directory, including the terminating null byte.
207 You need to allocate a bigger array and try again.
208 .SH CONFORMING TO
209 .BR getcwd ()
210 conforms to POSIX.1-2001.
211 Note however that POSIX.1-2001 leaves the behavior of
212 .BR getcwd ()
213 unspecified if
214 .I buf
215 is NULL.
216
217 .BR getwd ()
218 is present in POSIX.1-2001, but marked LEGACY.
219 POSIX.1-2008 removes the specification of
220 .BR getwd ().
221 Use
222 .BR getcwd ()
223 instead.
224 POSIX.1-2001
225 does not define any errors for
226 .BR getwd ().
227
228 .BR get_current_dir_name ()
229 is a GNU extension.
230 .SH NOTES
231 Under Linux, the function
232 .BR getcwd ()
233 is a system call (since 2.1.92).
234 On older systems it would query
235 .IR /proc/self/cwd .
236 If both system call and proc filesystem are missing, a
237 generic implementation is called.
238 Only in that case can
239 these calls fail under Linux with
240 .BR EACCES .
241 .LP
242 These functions are often used to save the location of the current working
243 directory for the purpose of returning to it later.
244 Opening the current
245 directory (".") and calling
246 .BR fchdir (2)
247 to return is usually a faster and more reliable alternative when sufficiently
248 many file descriptors are available, especially on platforms other than Linux.
249 .SH SEE ALSO
250 .BR chdir (2),
251 .BR fchdir (2),
252 .BR open (2),
253 .BR unlink (2),
254 .BR free (3),
255 .BR malloc (3)
256 .SH COLOPHON
257 This page is part of release 3.67 of the Linux
258 .I man-pages
259 project.
260 A description of the project,
261 information about reporting bugs,
262 and the latest version of this page,
263 can be found at
264 \%http://www.kernel.org/doc/man\-pages/.