XAgent-Smith

Listing Directory files or single files with there info

Oct 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. /*Author -XAgent Smith*/
  2.  
  3. /*Date:Oct 21,2018*/
  4.  
  5.  
  6. /*Take singel argument Name of file or name of directory*/
  7.  
  8.  
  9. /*Listing Directeries files in
  10.  [name]-[LNK]-[PERM]-[SIZ]-[DATE OF LAST    MODIFICTION]or singel  file info
  11. */
  12. #include<stdio.h>
  13. #include <time.h>
  14. #include <stdtool.c>
  15. #include<sys/fcntl.h>
  16. #include<sys/types.h>
  17. #include <dirent.h>
  18. #include<sys/stat.h>
  19. #include<unistd.h>
  20. /*******************************************/
  21. void print_info(char * dir,struct stat *stbuf)
  22. {
  23. printf("File:%s\t",dir);
  24.  
  25. printf("LNK:%d\t",stbuf->st_nlink);
  26. printf((stbuf->st_mode&S_IFMT)==S_IFDIR?"d":"-");
  27. printf((stbuf->st_mode&S_IFBLK)?"b":"-");
  28. printf((stbuf->st_mode&S_IRUSR)?"r":"-");
  29. printf((stbuf->st_mode&S_IWUSR)?"w":"-");
  30. printf((stbuf->st_mode&S_IXUSR)?"x":"-");
  31.  
  32. printf((stbuf->st_mode&S_IRGRP)?"r":"-");
  33. printf((stbuf->st_mode&S_IWGRP)?"w":"-");
  34. printf((stbuf->st_mode&S_IXGRP)?"x":"-");
  35.  
  36. printf((stbuf->st_mode&S_IROTH)?"r":"-");
  37. printf((stbuf->st_mode&S_IWOTH)?"w":"-");
  38. printf((stbuf->st_mode&S_IXOTH)?"x":"-");
  39.  
  40. printf("\tSize=[%lld]\t\t",stbuf->st_size);
  41. printf("%s",ctime(&stbuf->st_mtim));
  42.  
  43.  
  44. printf("\nFile %s symbolic link\n\n************************\n",(stbuf->st_mode&S_IFMT)==S_IFLNK?"is":"is not");
  45. return;
  46.  
  47. }
  48. /*****************************************/
  49. /*lsdir:listing directory "dir"*/
  50. void lsdir(char *dir)
  51. {
  52.  
  53.  DIR *dfp;
  54. struct dirent *red;
  55.  dfp=opendir(dir);
  56.  
  57.   while((red=readdir(dfp))!=NULL)
  58. {
  59.   struct stat stbuf;
  60.  if(strcmp(".",red->d_name)==0||strcmp("..",red->d_name)==0||red->d_ino==0)
  61.     {
  62.         continue;
  63.     }
  64.  
  65.     stat(red->d_name,&stbuf);
  66.     if((stbuf.st_mode&S_IFMT)==S_IFDIR)
  67.     lsdir(red->d_name);
  68.  
  69.     print_info(red->d_name,&stbuf);
  70. }
  71.  return;
  72. }
  73.  
  74. /*
  75.  *start of main
  76.  *
  77.  */
  78.  
  79. int main(int argc,char **argv)
  80. {
  81.  
  82. if(argc!=2)
  83. {
  84.    errno=EINVAL;
  85.    perror("ERROR");
  86.     return 1;
  87. }
  88. else
  89. {
  90.     struct stat stbuf;
  91.     if(stat(*(++argv),&stbuf)<0)
  92. {
  93.      perror(*argv);
  94.     exit(-1);
  95. }
  96.  
  97. if((stbuf.st_mode&S_IFMT)==S_IFDIR){
  98.  
  99. lsdir(*argv);
  100. return 0;
  101. }
  102. else
  103. goto startx;
  104.      
  105. }
  106.  
  107. struct stat stbuf;
  108. startx:
  109. stat(*argv,&stbuf);
  110. print_info(*argv,&stbuf);
  111.  
  112. return 0;
  113.  
  114. }
Add Comment
Please, Sign In to add comment