getcwd()--Get Current Directory


  Syntax
 #include <unistd.h>

 char *getcwd(char *buf, size_t size);  
  Service Program Name: QP0LLIB2

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The getcwd() function determines the absolute path name of the current directory and stores it in buf. The components of the returned path name are not symbolic links.

The access time of each directory in the absolute path name of the current directory (excluding the current directory itself) is updated.

If buf is a NULL pointer, getcwd() returns a NULL pointer and the [EINVAL] error.


Parameters

buf
(Output) A pointer to a buffer that will be used to hold the absolute path name of the current directory. The buffer must be large enough to contain the full pathname including the terminating NULL character. The current directory is returned in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.
size
(Input) The number of bytes in the buffer buf.

Authorities

Note: Adopted authority is not used.

Authorization Required for getcwd()


Return Value

value
getcwd() was successful. The value returned is a pointer to buf.
NULL
getcwd() was not successful. The errno global variable is set to indicate the error. After an error, the contents of buf are not defined.

Note: If buf is a NULL pointer, getcwd() returns a NULL pointer.


Error Conditions

If getcwd() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

If interaction with a file server is required to access the object, errno could indicate one of the following errors:



Error Messages

The following messages may be sent from this function:


Usage Notes

  1. This function will fail with error code [ENOTSAFE] when both of the following conditions occur:

    • Where multiple threads exist in the job.
    • The object this function is operating on resides in a file system that is not threadsafe. Only the following file systems are threadsafe for this function:

      • "Root" (/)
      • QOpenSys
      • User-defined
      • QNTC
      • QSYS.LIB
      • Independent ASP QSYS.LIB
      • QOPT
      • Network File System
      • QFileSvr.400

  2. QOPT File System Differences

    If the directory exists on a volume formatted in Universal Disk Format (UDF), the authorization that is checked for the directory and preceding directories in the path name follows the rules described in Authorization Required for getcwd(). If the directory exists on a volume formatted in some other media format, no authorization checks are made on the directory or preceding directories. The volume authorization list is checked for *USE authority regardless of the volume media format.

  3. If the absolute path name of the current directory is larger than 16 megabytes, then error code [ERANGE] will be returned.

Related Information


Example

The following example determines the current directory.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

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

main()
{
  char cwd[1024];

  if (chdir("/tmp") != 0)
    perror("chdir() error()");
  else
  {
    if (getcwd(cwd, sizeof(cwd)) == NULL)
      perror("getcwd() error");
    else
      printf("current working directory is: %s\n", cwd);
  }
}

Output:

current working directory is: /tmp


API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]