]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getcwd.3
s/filesystem/file system/
[thirdparty/man-pages.git] / man3 / getcwd.3
1 .\" (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 2007-07-26 "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 The
56 .BR getcwd ()
57 function copies an absolute pathname of the current working directory
58 to the array pointed to by
59 .IR buf ,
60 which is of length
61 .IR size .
62 .PP
63 If the current absolute pathname would require a buffer longer than
64 .I size
65 elements, NULL is returned, and
66 .I errno
67 is set to
68 .BR ERANGE ;
69 an application should check for this error, and allocate a larger
70 buffer if necessary.
71 .PP
72 If
73 .I buf
74 is NULL, the behavior of
75 .BR getcwd ()
76 is undefined.
77 .PP
78 As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc)
79 .BR getcwd ()
80 allocates the buffer dynamically using
81 .BR malloc (3)
82 if
83 .I buf
84 is NULL on call.
85 In this case, the allocated buffer has the length
86 .I size
87 unless
88 .I size
89 is zero, when
90 .I buf
91 is allocated as big as necessary.
92 It is possible (and, indeed,
93 advisable) to
94 .BR free (3)
95 the buffers if they have been obtained this way.
96
97 .BR get_current_dir_name (),
98 will
99 .BR malloc (3)
100 an array big enough to hold the current directory name.
101 If the environment
102 variable
103 .B PWD
104 is set, and its value is correct, then that value will be returned.
105
106 .BR getwd (),
107 does not
108 .BR malloc (3)
109 any memory.
110 The
111 .I buf
112 argument should be a pointer to an array at least
113 .B PATH_MAX
114 bytes long.
115 .BR getwd ()
116 does only return the first
117 .B PATH_MAX
118 bytes of the actual pathname.
119 Note that
120 .B PATH_MAX
121 need not be a compile-time constant; it may depend on the file system
122 and may even be unlimited.
123 For portability and security reasons, use of
124 .BR getwd ()
125 is deprecated.
126 .SH "RETURN VALUE"
127 NULL
128 on failure with
129 .I errno
130 set accordingly, and
131 .I buf
132 on success.
133 The contents of the array pointed to by
134 .I buf
135 is undefined on error.
136 .SH ERRORS
137 .TP
138 .B EACCES
139 Permission to read or search a component of the filename was denied.
140 .TP
141 .B EFAULT
142 .I buf
143 points to a bad address.
144 .TP
145 .B EINVAL
146 The
147 .I size
148 argument is zero and
149 .I buf
150 is not a null pointer.
151 .TP
152 .B ENOENT
153 The current working directory has been unlinked.
154 .TP
155 .B ERANGE
156 The
157 .I size
158 argument is less than the length of the working directory name.
159 You need to allocate a bigger array and try again.
160 .SH "CONFORMING TO"
161 .BR getcwd ()
162 conforms to POSIX.1-2001.
163 .BR getwd ()
164 is present in POSIX.1-2001, but marked LEGACY.
165 .BR get_current_dir_name ()
166 is a GNU extension.
167 .SH NOTES
168 Under Linux, the function
169 .BR getcwd ()
170 is a system call (since 2.1.92).
171 On older systems it would query
172 .IR /proc/self/cwd .
173 If both system call and proc file system are missing, a
174 generic implementation is called.
175 Only in that case can
176 these calls fail under Linux with
177 .BR EACCES .
178 .LP
179 These functions are often used to save the location of the current working
180 directory for the purpose of returning to it later.
181 Opening the current
182 directory (".") and calling
183 .BR fchdir (2)
184 to return is usually a faster and more reliable alternative when sufficiently
185 many file descriptors are available, especially on platforms other than Linux.
186 .SH "SEE ALSO"
187 .BR chdir (2),
188 .BR fchdir (2),
189 .BR open (2),
190 .BR unlink (2),
191 .BR free (3),
192 .BR malloc (3)