chdir()--Change Current Directory


  Syntax
 #include <unistd.h>

 int chdir(const char *path);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The chdir() function makes the directory named by path the new current directory. If the last component of path is a symbolic link, chdir() resolves the contents of the symbolic link. If the chdir() function fails, the current directory is unchanged.


Parameters

path
(Input) A pointer to the null-terminated path name of the directory that should become the current directory.

This parameter is assumed to be represented 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.

See QlgChdir()--Change Current Directory for a description and an example of supplying the path in any CCSID.


Authorities

Note: Adopted authority is not used.

Authorization Required for chdir()

Object Referred to Authority Required errno
Each directory of the path name *X EACCES


Return Value

0
chdir() was successful.
-1
chdir() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If chdir() 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]  
[EFAULT]  
[EFILECVT]  
[EINTR]  
[EINVAL]  
[EIO]  
[ELOOP]  
[ENAMETOOLONG]  
[ENOENT]  
[ENOSPC]  
[ENOTAVAIL]  
[ENOTDIR]  
[ENOTSAFE]  
[ENOTSUP]  
[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:

Message ID Error Message Text
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 all the following conditions are true:

    The chdir() API operates on two objects: the previous current working directory and the new one. If either of these objects is managed by a file system that is not threadsafe, chdir() fails with the ENOTSAFE error code.


  2. QOPT File System Differences

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


Related Information


Example

The following example uses chdir().

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

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

main() {
  if (chdir("/tmp") != 0)
    perror("chdir() to /tmp failed");
  if (chdir("/chdir/error") != 0)
    perror("chdir() to /chdir/error failed");
}

Output:

chdir() to /chdir/error failed: No such path or directory.


API introduced: V3R1

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