7.4. Switching your current directory

Just as there is a "current library" in the traditional file system, there is also a "current directory" in the IFS. When you supply a path name to an API, and that path name does not start with a slash character, it tells the API that you want to use the "current directory."

You can change your current directory by calling the chdir() "Change Directory" API.

The prototype for chdir() looks like this:

      int chdir(const char *path)                               

     D chdir           PR            10I 0 ExtProc('chdir')       
     D   path                          *   Value Options(*string) 
   

Not much to it, is there? You just call chdir() with one argument, the path to change to. The chdir() API will return a -1 if it fails, or a 0 if it was successful.

In this example, we will create and delete the ShoeTongue directory, just as we did in the code examples for mkdir() and rmdir(). However, we will use a "relative" directory name this time. We will switch our current directory to /ifstest, and then we won't have to specify it on the mkdir() and rmdir() APIs.

     c                   if        chdir('/ifstest') < 0        
     c                   callp     EscErrno(errno)              
     c                   endif                                  
                                                                
     c                   if        mkdir('ShoeTongue':          
     c                               S_IRUSR+S_IWUSR+S_IXUSR+   
     c                               S_IRGRP+S_IWGRP+S_IXGRP+   
     c                               S_IROTH+S_IXOTH) < 0       
     c                   callp     EscErrno(errno)              
     c                   endif                                  
                                                                
     c                   if        rmdir('ShoeTongue') < 0      
     c                   callp     EscErrno(errno)              
     c                   endif