Illustration of readdir and opendir GNU/LINUX system calls

//Program to print the contents of the Directory whose path is given as Input
/* system calls used
   1)opendir         prototype:DIR *opendir(path string)   //returns pointer to datatype DIR
   2)readdir         prototype:dirent *readdir(DIR *dp)    //info abt readdir is given below in the code
int main()
    DIR *dp;
    char *path;
    path=(char *)malloc(400);
    printf(“\nEnter the path: “);//Enter the path of the directory to be scanned
    scanf(“%s”,path);//Recieving a string using scanf has a potential buffer overflow vulnerabilty
    //use fgets instead of scanf
    struct dirent* folder;
        printf(“\nError Opening the directory”);
    printf(“\nThe contents of this directory are “);
    /*The readdir system call returns a pointer to the built in structure “dirent” (found in dirent.h)
      on succesive calls to readdir it returns pointer to the next entry in the directory
      readdir returns a NULL on reaching end of the directory after printing all its entries*/

        printf(“\n\t%s “,folder->d_name);//name is the member of the structure dirent.
        //folder now contains the pointer to the structure dirent, so its to access the members of the structure
    printf(“\nThank you ***************”);
    return 0;

//mail me at

2 thoughts on “Illustration of readdir and opendir GNU/LINUX system calls

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s