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()

Object Referred to Authority Required errno
Each directory in the path name preceding the current directory *RX EACCES
Current directory *X EACCES
Note: QDLS File System Differences

If the current directory is an immediate subdirectory of /QDLS (that is, at the next level below /QDLS in the directory hierarchy), the user must have *RX (*USE) authority to the directory. Otherwise, the QDLS authority requirements are the same as shown above.


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.

Error condition Additional information
[EACCES]

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

[EAGAIN]  
[EBADFID]  
[EBADNAME]  
[EBUSY]  
[ECONVERT]  
[EDAMAGE]  
[EEXIST]  
[EFAULT]  
[EINTR]  
[EINVAL]

For QlgGetcwd(), the size may be less than or equal to the size of a Qlg_Path_Name_T structure.

[EIO]  
[EMFILE]  
[ENAMETOOLONG]  
[ENFILE]  
[ENOENT]  
[ENOMEM]  
[ENOSPC]  
[ENOTAVAIL]  
[ENOTSAFE]  
[ENOTSUP]  
[ERANGE]

The value of an argument is too small, or a result too large. For example, the size argument is too small. For getcwd(), it is greater than zero but smaller than the length of the path name plus a NULL character. For QlgGetcwd(), it is greater than the size of a Qlg_Path_Name_T structure, but less than the size need to receive the entire current working directory path name.

[EROOBJ]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[EUNKNOWN]  

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

Error condition Additional information
[EADDRNOTAVAIL]  
[ECONNABORTED]  
[ECONNREFUSED]  
[ECONNRESET]  
[EHOSTDOWN]  
[EHOSTUNREACH]  
[ENETDOWN]  
[ENETRESET]  
[ENETUNREACH]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[ETIMEDOUT]  
[EUNATCH]  


Error Messages

The following messages may be sent from this function:

CPE3418 E Possible APAR condition or hardware failure.
CPFA0D4 E File system error occurred. Error number &1.
CPF3CF2 E Error(s) occurred during running of &1 API.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.

Usage Notes

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


  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 ]