gtpc2m0oC/C++ Language Support User's Guide

chdir-Change the Working Directory

This function changes the working directory.

Format

#include <unistd.h>
int chdir(const char *pathname);

pathname
The name of the directory that you want to be the new working directory.

This function makes pathname your new working directory.

Normal Return

If successful, the chdir function changes the current working directory and returns 0.

Error Return

If unsuccessful, the chdir function does not change the working directory. Instead, it returns -1, and sets errno to one of the following:

EACCES
The process does not have search permission on one of the components of pathname.

ELOOP
A loop exists in symbolic links. This error is issued if the number of symbolic links detected in the resolution of pathname is greater than POSIX_SYMLOOP (a value defined in the limits.h header file).

ENAMETOOLONG
pathname is longer than PATH_MAX characters, or some component of pathname is longer than NAME_MAX characters. For symbolic links, the length of the pathname string substituted for a symbolic link exceeds PATH_MAX.

ENOENT
pathname is an empty string or the specified directory does not exist.

ENOTDIR
Some component of pathname is not a directory.

Programming Considerations

None.

Examples

The following example shows the use of the chdir function.

#include <unistd.h>
#include <stdio.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 file or directory

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.