Name

chdir, fchdir
- change working directory

Library

libc.lib

Synopsis

  #include <unistd.h>
  int chdir (const char *path);
  int fchdir (int fd);

Return values

Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Detailed description

The path argument points to the pathname of a directory. The chdir system call causes the named directory to become the current working directory, that is, the starting point for path searches of pathnames not beginning with a slash, /.

The fchdir system call causes the directory referenced by fd to become the current working directory, the starting point for path searches of pathnames not beginning with a slash, /.


Examples

/**
*  Detailed description   : This test code demonstrates usage of chdir system call
*
*  Preconditions : "Example" directory should be present in current working
   directory.
**/
#include <unistd.h>
int main()
{
  if(chdir("Example") < 0 )  
  {
     printf("Failed to change working directory \n");
     return -1 ;
  }
  printf("Working directory changed \n");
  return 0 ;
}

         

Output

Working directory changed

         

#include<unistd.h>
#include<stdio.h>
int test_fchdir()
{
   int retVal;
   int rmdr;
   int mkdr = mkdir("test_dir", S_IWUSR);
   if( !mkdr )
   {
     int opn = open("test_dir", O_RDONLY);
     if( opn != -1 )
     {
       retVal = fchdir( opn );
       if( !retVal )
       {
         printf("Fchdir passed");
       }
       else
       {
         printf("Failed");
       }
       int cls = close(opn);
     }
     else
     {
       printf("Failed");
     }
     rmdr = rmdir("test_dir");  
   }
   else
   {
     printf("Failed");
   }
}

         

Output

Fchdir passed

         

Errors

The fchdir system call will fail and the current working directory will be unchanged if one or more of the following are true:
[ENOTDIR]
  The file descriptor does not reference a directory.
[EBADF]
  The argument fd is not a valid file descriptor.

See also


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top