xref: /titanic_50/usr/src/man/man3c/getcwd.3c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
te
Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
Copyright 1989 AT&T
Portions Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
http://www.opengroup.org/bookstore/.
The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
This notice shall appear on any product containing this material.
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
GETCWD 3C "Oct 18, 2004"
NAME
getcwd - get pathname of current working directory
SYNOPSIS

#include <unistd.h>

char *getcwd(char *buf, size_t size);
DESCRIPTION

The getcwd() function places an absolute pathname of the current working directory in the array pointed to by buf, and returns buf. The pathname copied to the array contains no components that are symbolic links. The size argument is the size in bytes of the character array pointed to by buf and must be at least one greater than the length of the pathname to be returned.

If buf is not a null pointer, the pathname is stored in the space pointed to by buf.

If buf is a null pointer, getcwd() obtains size bytes of space using malloc(3C). The pointer returned by getcwd() can be used as the argument in a subsequent call to free().

RETURN VALUES

Upon successful completion, getcwd() returns the buf argument. If buf is an invalid destination buffer address, NULL is returned and errno is set to EFAULT. Otherwise, a null pointer is returned and errno is set to indicate the error.

ERRORS

The getcwd() function will fail if: EFAULT

The buf argument is an invalid destination buffer address.

EINVAL

The size argument is equal to 0.

ERANGE

The size argument is greater than 0 and less than the length of the pathname plus 1.

The getcwd() function may fail if: EACCES

A parent directory cannot be read to get its name.

ENOMEM

Insufficient storage space is available.

EXAMPLES

Example 1 Determine the absolute pathname of the current working directory.

The following example returns a pointer to an array that holds the absolute pathname of the current working directory. The pointer is returned in the ptr variable, which points to the buf array where the pathname is stored.

#include <stdlib.h>
#include <unistd.h>
...
long size;
char *buf;
char *ptr;
size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL)
 ptr = getcwd(buf, (size_t)size);
...

Example 2 Print the current working directory.

The following example prints the current working directory.

#include <unistd.h>
#include <stdio.h>

main(\|)
{
 char *cwd;
 if ((cwd = getcwd(NULL, 64)) == NULL) {
 perror("pwd");
 exit(2);
 }
 (void)printf("%s\en", cwd);
 free(cwd); /* free memory allocated by getcwd() */
 return(0);
}
USAGE

Applications should exercise care when using chdir(2) in conjunction with getcwd(). The current working directory is global to all threads within a process. If more than one thread calls chdir() to change the working directory, a subsequent call to getcwd() could produce unexpected results.

ATTRIBUTES

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE ATTRIBUTE VALUE
Interface Stability Standard
MT-Level MT-Safe
SEE ALSO

chdir(2), malloc(3C), attributes(5), standards(5)