OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man3 / getcwd.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Wed Jul 21 22:35:42 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified 18 Mar 1996 by Martin Schulze (joey@infodrom.north.de):
25 .\"   Corrected description of getwd().
26 .\" Modified Sat Aug 21 12:32:12 MET 1999 by aeb - applied fix by aj
27 .\" Modified Mon Dec 11 13:32:51 MET 2000 by aeb
28 .\" Modified Thu Apr 22 03:49:15 CEST 2002 by Roger Luethi <rl@hellgate.ch>
29 .\"
30 .TH GETCWD 3 2009-03-31 "GNU" "Linux Programmer's Manual"
31 .SH NAME
32 getcwd, getwd, get_current_dir_name \- Get current working directory
33 .SH SYNOPSIS
34 .nf
35 .B #include <unistd.h>
36 .sp
37 .BI "char *getcwd(char *" buf ", size_t " size );
38 .sp
39 .BI "char *getwd(char *" buf );
40 .sp
41 .B "char *get_current_dir_name(void);"
42 .fi
43 .sp
44 .in -4n
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .in
48 .sp
49 .BR getcwd ():
50 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
51 .br
52 .BR get_current_dir_name ():
53 _GNU_SOURCE
54 .SH DESCRIPTION
55 These functions return a null-terminated string containing an
56 absolute pathname that is the current working directory of
57 the calling process.
58 The pathname is returned as the function result and via the argument
59 .IR buf ,
60 if present.
61
62 The
63 .BR getcwd ()
64 function copies an absolute pathname of the current working directory
65 to the array pointed to by
66 .IR buf ,
67 which is of length
68 .IR size .
69 .PP
70 If the length of the absolute pathname of the current working directory,
71 including the terminating null byte, exceeds
72 .I size
73 bytes, NULL is returned, and
74 .I errno
75 is set to
76 .BR ERANGE ;
77 an application should check for this error, and allocate a larger
78 buffer if necessary.
79 .PP
80 As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
81 .BR getcwd ()
82 allocates the buffer dynamically using
83 .BR malloc (3)
84 if
85 .I buf
86 is NULL.
87 In this case, the allocated buffer has the length
88 .I size
89 unless
90 .I size
91 is zero, when
92 .I buf
93 is allocated as big as necessary.
94 The caller should
95 .BR free (3)
96 the returned buffer.
97
98 .BR get_current_dir_name ()
99 will
100 .BR malloc (3)
101 an array big enough to hold the absolute pathname of
102 the current working directory.
103 If the environment
104 variable
105 .B PWD
106 is set, and its value is correct, then that value will be returned.
107 The caller should
108 .BR free (3)
109 the returned buffer.
110
111 .BR getwd ()
112 does not
113 .BR malloc (3)
114 any memory.
115 The
116 .I buf
117 argument should be a pointer to an array at least
118 .B PATH_MAX
119 bytes long.
120 If the length of the absolute pathname of the current working directory,
121 including the terminating null byte, exceeds
122 .B PATH_MAX
123 bytes, NULL is returned, and
124 .I errno
125 is set to
126 .BR ENAMETOOLONG .
127 (Note that on some systems,
128 .B PATH_MAX
129 may not be a compile-time constant;
130 furthermore, its value may depend on the file system, see
131 .BR pathconf (3).)
132 For portability and security reasons, use of
133 .BR getwd ()
134 is deprecated.
135 .SH "RETURN VALUE"
136 On success, these functions return a pointer to a string containing
137 the pathname of the current working directory.
138 In the case
139 .BR getcwd ()
140 and
141 .BR getwd ()
142 this is the same value as
143 .IR buf.
144
145 On failure, these functions return NULL, and
146 .I errno
147 is set to indicate the error.
148 The contents of the array pointed to by
149 .I buf
150 are undefined on error.
151 .SH ERRORS
152 .TP
153 .B EACCES
154 Permission to read or search a component of the filename was denied.
155 .TP
156 .B EFAULT
157 .I buf
158 points to a bad address.
159 .TP
160 .B EINVAL
161 The
162 .I size
163 argument is zero and
164 .I buf
165 is not a null pointer.
166 .TP
167 EINVAL
168 .BR getwd ():
169 .I buf
170 is NULL.
171 .TP
172 ENAMETOOLONG
173 .BR getwd ():
174 The size of the null-terminated absolute pathname string exceeds
175 .B PATH_MAX
176 bytes.
177 .TP
178 .B ENOENT
179 The current working directory has been unlinked.
180 .TP
181 .B ERANGE
182 The
183 .I size
184 argument is less than the length of the absolute pathname of the
185 working directory, including the terminating null byte.
186 You need to allocate a bigger array and try again.
187 .SH "CONFORMING TO"
188 .BR getcwd ()
189 conforms to POSIX.1-2001.
190 Note however that POSIX.1-2001 leaves the behavior of
191 .BR getcwd ()
192 unspecified if
193 .I buf
194 is NULL.
195
196 .BR getwd ()
197 is present in POSIX.1-2001, but marked LEGACY.
198 POSIX.1-2008 removes the specification of
199 .BR getwd ().
200 Use
201 .BR getcwd ()
202 instead.
203 POSIX.1-2001
204 does not define any errors for
205 .BR getwd ().
206
207 .BR get_current_dir_name ()
208 is a GNU extension.
209 .SH NOTES
210 Under Linux, the function
211 .BR getcwd ()
212 is a system call (since 2.1.92).
213 On older systems it would query
214 .IR /proc/self/cwd .
215 If both system call and proc file system are missing, a
216 generic implementation is called.
217 Only in that case can
218 these calls fail under Linux with
219 .BR EACCES .
220 .LP
221 These functions are often used to save the location of the current working
222 directory for the purpose of returning to it later.
223 Opening the current
224 directory (".") and calling
225 .BR fchdir (2)
226 to return is usually a faster and more reliable alternative when sufficiently
227 many file descriptors are available, especially on platforms other than Linux.
228 .SH "SEE ALSO"
229 .BR chdir (2),
230 .BR fchdir (2),
231 .BR open (2),
232 .BR unlink (2),
233 .BR free (3),
234 .BR malloc (3)