Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

getcwd - get pathname of current working directory

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

char *getcwd(char *buf, int size);

Description

getcwd() returns a pointer to the current directory pathname. The value of size must be at
least one greater than the length of the pathname to be returned.

If buf is not null, the pathname will be stored in the space pointed to by buf.

If buf is a null pointer, getcwd() will obtain size bytes of space by calling malloc(). In this
case, the pointer returned by getcwd() may be used as the argument in a subsequent call
to free().

The current directory will correspond to the home directory so long as no call to chdir() is
made. The home directory can be checked with getpwuid() or getpwnam(). Both
functions return a structure that includes a pointer to the original working directory.

When a C program is started, the current directory is set to the home directory, as defined
in the file SYSSRPM. If the environment variable HOME is defined for a C program, the home
directory is set to that value.

If the directory entered in the file SYSSRPM does not exist, a slash (/) is returned.

BS2000
If an SDF-P variable SYSPOSIX.HOME exists, the HOME variable of the C programming
environment is initialized with the value of the SYSPOSIX.HOME variable. (End)

The current directory can be changed at any time by calling chdir(). The effect of a call
to chdir() extends for the duration of the calling program. The home directory is not
changed by the call.

Return val.

pointer to the current directory pathname



if successful.


0

if size is not large enough or an error occurs in a subordinate function. errno is set to indicate the error.

Errors

getcwd() will fail if:

 

EACCES

The name of a parent directory could not be obtained because the directory could not be read.

 

EINVAL

size is equal to 0.

 

ENOMEM

Insufficient storage space is available.

 

ERANGE

size is less than 0, or is greater than 0, but smaller than the length of the pathname + 1.

Notes

getcwd() is executed only for POSIX files

See also

malloc(), unistd.h.