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



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.


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 all the following conditions are true:

    • Where multiple threads exist in the job.
    • The object on which this function is operating 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

    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 ]