getcwd

Returns the pathname of the current working directory.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <unistd.h> 
   
  char *getcwd (
     char    *path,   
     size_t   len); 
  

Parameters

path

(OUT) Specifies the buffer in which to place the name of the current working directory. If passed as NULL, getcwd allocates a buffer the size of len and returns a pointer to that buffer. You must free the allocated buffer.

len

(IN) Specifies the length of the buffer (including space for the null-terminating character). If len is not large enough to hold the pathname, getcwd truncates the path. The truncated string placed in the buffer is not null terminated. If path is NULL, len must be set to at least PATH_MAX.

Return Values

If successful, returns the address of the string containing the name of the current working directory. Otherwise, returns a NULL pointer and sets errno to one of the following:

Decimal

Constant

Description

5

ENOMEM

Insufficient storage space is available.

6

EACCES

Read or search permission was denied for a component of the path.

9

EINVAL

The len parameter is 0.

Remarks

The getcwd function returns the absolute pathname of the current working directory in the array pointed to by path.

The type of string returned, whether ASCII or UTF-8, depends upon your application's link flags.

On NetWare, the current working directory does not include the volume name.

  • If the current working directory is the root of a volume, getcwd returns "\".

  • If the current working directory is a subdirectory of the volume, for example sys:\public, getcwd returns "\public".

If you have set the current working directory for the current thread by calling setcwd2 or chdir2, the current working directory of the thread is returned. Otherwise, the current working directory of the VM is returned.

See Also