2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
43 .\" Copyright 1989 AT&T
44 .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved.
45 .\" Portions Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
47 .TH GETCWD 3C "Oct 18, 2004"
49 getcwd \- get pathname of current working directory
55 \fBchar *\fR\fBgetcwd\fR(\fBchar *\fR\fIbuf\fR, \fBsize_t\fR \fIsize\fR);
61 The \fBgetcwd()\fR function places an absolute pathname of the current working
62 directory in the array pointed to by \fIbuf\fR, and returns \fIbuf\fR. The
63 pathname copied to the array contains no components that are symbolic links.
64 The \fIsize\fR argument is the size in bytes of the character array pointed to
65 by \fIbuf\fR and must be at least one greater than the length of the pathname
69 If \fIbuf\fR is not a null pointer, the pathname is stored in the space pointed
73 If \fIbuf\fR is a null pointer, \fBgetcwd()\fR obtains \fIsize\fR bytes of
74 space using \fBmalloc\fR(3C). The pointer returned by \fBgetcwd()\fR can be
75 used as the argument in a subsequent call to \fBfree()\fR.
79 Upon successful completion, \fBgetcwd()\fR returns the \fIbuf\fR argument. If
80 \fIbuf\fR is an invalid destination buffer address, \fINULL\fR is returned and
81 \fBerrno\fR is set to \fBEFAULT\fR. Otherwise, a null pointer is returned and
82 \fBerrno\fR is set to indicate the error.
86 The \fBgetcwd()\fR function will fail if:
93 The \fIbuf\fR argument is an invalid destination buffer address.
102 The \fIsize\fR argument is equal to 0.
111 The \fIsize\fR argument is greater than 0 and less than the length of the
117 The \fBgetcwd()\fR function may fail if:
124 A parent directory cannot be read to get its name.
133 Insufficient storage space is available.
138 \fBExample 1 \fRDetermine the absolute pathname of the current working
142 The following example returns a pointer to an array that holds the absolute
143 pathname of the current working directory. The pointer is returned in the
144 \fIptr\fR variable, which points to the \fIbuf\fR array where the pathname is
156 size = pathconf(".", _PC_PATH_MAX);
157 if ((buf = (char *)malloc((size_t)size)) != NULL)
158 ptr = getcwd(buf, (size_t)size);
164 \fBExample 2 \fRPrint the current working directory.
167 The following example prints the current working directory.
178 if ((cwd = getcwd(NULL, 64)) == NULL) {
182 (void)printf("%s\en", cwd);
183 free(cwd); /* free memory allocated by getcwd() */
192 Applications should exercise care when using \fBchdir\fR(2) in conjunction with
193 \fBgetcwd()\fR. The current working directory is global to all threads within a
194 process. If more than one thread calls \fBchdir()\fR to change the working
195 directory, a subsequent call to \fBgetcwd()\fR could produce unexpected
200 See \fBattributes\fR(5) for descriptions of the following attributes:
208 ATTRIBUTE TYPE ATTRIBUTE VALUE
210 Interface Stability Standard
218 \fBchdir\fR(2), \fBmalloc\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)