Wednesday 8 July 2015

LINUX5

Image description
//5) write a c program which lists all  the files of the current working directory which contains hard link files
/*
THEORY:
PRE_WORK:
1)CREATE A DIRECTORY USING mkdir COMMAND
2)CHANGE INTO THE DIRECTORY CREATED
3)CREATE FILE'S IN THE DIRECTORY USING cat  COMMAND
4)CREATE THE HARD LINK's FOR THE FILES (eg:ln filename(created file name in 3))
 
 PROGRAM:
*/
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/stat.h>
main(int argc,char* argv[])
{
     DIR *dp;
     struct dirent *dirp;
     struct stat sb;
     int i;
     char str[40];
     if((dp=opendir(" "))==NULL)
     {
           printf("directory open error\n");
           exit(2);
     }
     while((dirp=readdir(dp))!=NULL)
     {
           if(stat(dirp->d_name,&sb)==-1)
           {
                perror("stat");
                exit(2);
           }   
           if(strcmp(".",dirp->d_name)!=0&&
strcmp("..",dirp->d_name)!=0&&sb.st_nlink>=2)
           {
                printf("%ld--%s\n",dirp->d_ino,dirp->d_name);
           }
     }
     exit(0);
}

/*
compilation:
gcc 5.c

execution:
 ./a.out


output:








No comments:

Post a Comment