Thursday, February 6, 2014

DIRECTORY ACCESS WITH SYSTEM CALLS

ACCESSING THE CONTENTS

We have seen earlier that directory is a special file that contain the directory objects who contain a mapping of file-names to the actual files. We shall write a small program to check the contents of a directory specified via command line.
First we have a look at the functions that we would use:
opendir(3)
readdir(3)
closedir(3)
	  

So let us have a look at opendir:
man 2  opendir

Library:
    #include <sys/types.h>
    #include <dirent.h>
Prototype:
       DIR* opendir(const char* name);
Description:
     opens a directory stream corresponding to the directory name, and
     returns a pointer to the directory stream. On an error NULL is
     returned and errno is set.
	  

and a peek at readdir:
man 2 readdir

Library:
    #include <dirent.h>
Prototype:
       struct dirent *readdir(DIR *dirp);
Description:
    returns next item in the directory stream dirp.
Returns:
    returns a struct dirent that represent the next entry in directory
    stream dirp. If there is an error or end of directory is reached
    it will return NULL.
	  

It will be useful to know the composition of dirent struct:
         struct dirent {
               ino_t          d_ino;       /* inode number */
               off_t          d_off;       /* not an offset; see NOTES */
               unsigned short d_reclen;    /* length of this record */
               unsigned char  d_type;      /* type of file; not supported
                                              by all filesystem types */
               char           d_name[256]; /* filename */
           };
	  

and a quick look at closedir:
man closedir

Library:
    #include <sys/types.h>
    #include <dirent.h>
Prototype:
       int closedir(DIR *dirp);
Description:
    close directory stream associated with dirp.
Returns:
    On Success: The value zero is returned.
    On Failure: The value -1 is resturned, errno is set accordingly.
	  
;

Now is the time to dive into the actual program.
/*
Created By: Anil Singh, NIU, IL-60115
Description: Receive name of directory, and list contents.
*/

#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
static void scanDir ( char* dir ){

  printf("Directory : %s\n",dir);
  // pointers for structures for directory processing
  DIR* dp;
  struct dirent* dirp;
  // Open directory for reading.
  errno=0;
  dp = opendir ( dir );
  if ( !dp ) {
    fprintf(stderr,"Can't open %s: %s\n",dir,strerror(errno));
    return;
  };

  // Read and display contents of directory.
  errno=0;
  while ( ( dirp = readdir ( dp ) ) )
    printf ( "%s\n", dirp->d_name );
  if(!dirp && errno){
    fprintf(stderr,"can't read %s: %s\n","name",strerror(errno));
    return;
  }
  // Close directory.
  if ( (closedir(dp)<0)){
    fprintf(stderr,"can't close %s: %s\n","name",strerror(errno));
    return;
  }
}

int main(int argc, char* argv[]){

  /*If no argument is supplied, set current directory*/
  int i;
  if(argc==1){
    scanDir(".");
  }
  else
    for( i=1; i<argc; i++){
      scanDir(argv[i]);
    }
  return 0;
}

	  

SUMMARY

We saw how to open, read and close directories under LINUX and also a bit on how to deal with the errors.

No comments:

Post a Comment