Sun Microsystems, Inc.
spacerspacer
spacer   www.sun.com docs.sun.com | | |  
spacer
black dot
   
A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z
    
 
Standard C Library Functionsgetcwd(3C)


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 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. Otherwise, the function returns a null pointer and sets errno to indicate the error.

ERRORS

 

The getcwd() function will fail if:

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.

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.

EXAMPLES

 Example 1. Printing 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\n", cwd);
    return(0);
}

ATTRIBUTES

 

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

ATTRIBUTE TYPEATTRIBUTE VALUE
MT-LevelMT-Safe

SEE ALSO

 

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


SunOS 5.9Go To TopLast Changed 16 Aug 2001

 
      
      
Copyright 2002 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.