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