Unleashed v1.4
[unleashed.git] / share / man / man3c / getcwd.3c
blobf1114bc6dd315197586f2f9b505b96d24ebb44ec
1 .\"
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/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
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.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
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.
30 .\"
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.
35 .\"
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]
41 .\"
42 .\"
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.
46 .\"
47 .TH GETCWD 3C "Oct 18, 2004"
48 .SH NAME
49 getcwd \- get pathname of current working directory
50 .SH SYNOPSIS
51 .LP
52 .nf
53 #include <unistd.h>
55 \fBchar *\fR\fBgetcwd\fR(\fBchar *\fR\fIbuf\fR, \fBsize_t\fR \fIsize\fR);
56 .fi
58 .SH DESCRIPTION
59 .sp
60 .LP
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
66 to be returned.
67 .sp
68 .LP
69 If \fIbuf\fR is not a null pointer, the pathname is stored in the space pointed
70 to by \fIbuf\fR.
71 .sp
72 .LP
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.
76 .SH RETURN VALUES
77 .sp
78 .LP
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.
83 .SH ERRORS
84 .sp
85 .LP
86 The \fBgetcwd()\fR function will fail if:
87 .sp
88 .ne 2
89 .na
90 \fB\fBEFAULT\fR\fR
91 .ad
92 .RS 10n
93 The \fIbuf\fR argument is an invalid destination buffer address.
94 .RE
96 .sp
97 .ne 2
98 .na
99 \fB\fBEINVAL\fR\fR
101 .RS 10n
102 The \fIsize\fR argument is equal to 0.
106 .ne 2
108 \fB\fBERANGE\fR\fR
110 .RS 10n
111 The \fIsize\fR argument is greater than 0 and less than the length of the
112 pathname plus 1.
117 The \fBgetcwd()\fR function may fail if:
119 .ne 2
121 \fB\fBEACCES\fR\fR
123 .RS 10n
124 A parent directory cannot be read to get its name.
128 .ne 2
130 \fB\fBENOMEM\fR\fR
132 .RS 10n
133 Insufficient storage space is available.
136 .SH EXAMPLES
138 \fBExample 1 \fRDetermine the absolute pathname of the current working
139 directory.
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
145 stored.
148 .in +2
150 #include <stdlib.h>
151 #include <unistd.h>
152 \&...
153 long size;
154 char *buf;
155 char *ptr;
156 size = pathconf(".", _PC_PATH_MAX);
157 if ((buf = (char *)malloc((size_t)size)) != NULL)
158        ptr = getcwd(buf, (size_t)size);
159 \&...
161 .in -2
164 \fBExample 2 \fRPrint the current working directory.
167 The following example prints the current working directory.
170 .in +2
172 #include <unistd.h>
173 #include <stdio.h>
175 main(\|)
177     char *cwd;
178     if ((cwd = getcwd(NULL, 64)) == NULL) {
179         perror("pwd");
180         exit(2);
181     }
182     (void)printf("%s\en", cwd);
183     free(cwd); /* free memory allocated by getcwd() */
184     return(0);
187 .in -2
189 .SH USAGE
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
196 results.
197 .SH ATTRIBUTES
200 See \fBattributes\fR(5) for descriptions of the following attributes:
205 box;
206 c | c
207 l | l .
208 ATTRIBUTE TYPE  ATTRIBUTE VALUE
210 Interface Stability     Standard
212 MT-Level        MT-Safe
215 .SH SEE ALSO
218 \fBchdir\fR(2), \fBmalloc\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)