[Previous]
[Contents]
[Next]

getcwd()

get the name of the current working directory

Synopsis:

#include <unistd.h>
char *getcwd( char *buffer, size_t size );

Description:

The getcwd() function returns the name of the current working directory. The buffer address is either NULL, or is the location at which a string containing the name of the current working directory is to be placed. In the latter case, the value of size is the length (including the delimiting '\0' character) that can be be used to store this name.

The maximum size that might be required for buffer is PATH_MAX + 1 bytes.


Note: As an extension to POSIX 1003.1, when buffer has a value of NULL, a string is allocated using malloc() to contain the name of the current working directory. This string may be freed using the free() function.

Returns:

The address of the string containing the name of the current working directory, unless an error occurs, in which case NULL is returned, and the global variable errno is set.

Errors:

EINVAL
The argument size is negative.
ENOMEM
Not enough memory to allocate a buffer.
ERANGE
The buffer is too small (as specified by size) to contain the name of the current working directory.

Examples:

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

void main()
  {
    char *cwd;

    cwd = getcwd( NULL, 0 );
    if( cwd != NULL ) {
      printf( "My working directory is %s\n", cwd );
      free( cwd );
    }
  }

produces the output:

My working directory is /home/bill

Classification:

POSIX 1003.1 with extensions

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

chdir(), chmod(), errno, mkdir(), rmdir()


[Previous]
[Contents]
[Next]