Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<fcntl.h>
  3. #include<pwd.h>
  4. #include<grp.h>
  5. #include<stdlib.h>
  6. #include<unistd.h>
  7. #include<sys/stat.h>
  8. #include<sys/types.h>
  9. #include<dirent.h>
  10. #include<libgen.h>
  11. #include<string.h>
  12. #include<errno.h>
  13. void printAttributes(struct stat st)
  14. {
  15.     char atr[10] = "----------";
  16.     if (S_ISLNK(st.st_mode)) atr[0] = 'l';
  17.     if (S_ISREG(st.st_mode)) atr[0] = '-';
  18.     if (S_ISDIR(st.st_mode)) atr[0] = 'd';
  19.     if (S_ISBLK(st.st_mode)) atr[0] = 'b';
  20.     if (S_ISCHR(st.st_mode)) atr[0] = 'c';
  21.     if (S_ISFIFO(st.st_mode)) atr[0] = 'p';
  22.     if (S_ISSOCK(st.st_mode)) atr[0] = 's';
  23.     if (S_IRUSR & st.st_mode) atr[1] = 'r';
  24.     if (S_IWUSR & st.st_mode) atr[2] = 'w';
  25.     if (S_IXUSR & st.st_mode) atr[3] = 'x';
  26.     if (S_IRGRP & st.st_mode) atr[4] = 'r';
  27.     if (S_IWGRP & st.st_mode) atr[5] = 'w';
  28.     if (S_IXGRP & st.st_mode) atr[6] = 'x';
  29.     if (S_IROTH & st.st_mode) atr[7] = 'r';
  30.     if (S_IWOTH & st.st_mode) atr[8] = 'w';
  31.     if (S_IXOTH & st.st_mode) atr[9] = 'x';
  32.     printf("%s ", atr);
  33. }
  34.  
  35. void printUsr(const struct stat* st)
  36. {
  37.     struct passwd* pw = getpwuid(st->st_uid);
  38.     if (pw == NULL) printf("%d ", st->st_uid);
  39.     else printf("%s ", pw->pw_name);
  40. }
  41. void printGroup(const struct stat* st)
  42. {
  43.     struct group* gr = getgrgid(st->st_gid);
  44.     if (gr == NULL) printf("%d ", st->st_uid);
  45.     else printf("%s ", gr->gr_name);
  46. }
  47.  
  48.  
  49.  
  50. void printStat(struct stat* st, char* filename)
  51. {
  52.     if (strcmp(basename(filename), ".") == 0 || strcmp(basename(filename), "..") == 0)
  53.     {
  54.         return;
  55.     }
  56.     if (!S_ISLNK(st->st_mode))
  57.     {
  58.         printAttributes(*st);
  59.         printf("%ld ", st->st_nlink);
  60.         printGroup(st);
  61.         printGroup(st);
  62.         printf("%ld ", st->st_size);
  63.         printf("%s\n", basename(filename));
  64.     }
  65.     if (S_ISLNK(st->st_mode))
  66.     {
  67.         printAttributes(*st);
  68.         printf("%ld ", st->st_nlink);
  69.         printGroup(st);
  70.         printGroup(st);
  71.         printf("%ld ", st->st_size);
  72.         char tmp[PATH_MAX] = "";
  73.         ssize_t r = readlink(filename, tmp, sizeof(tmp));
  74.         if (r == -1)
  75.         {
  76.             fprintf(stderr, "%s\n", strerror(errno));
  77.             exit(1);
  78.         }
  79.  
  80.         tmp[r] = '\0';
  81.         printf("%s -> ", basename(filename));
  82.         printf("%s\n", tmp);
  83.     }
  84. }
  85.  
  86. int main(int argc, char* argv[])
  87. {
  88.     struct stat st;
  89.     if(lstat(argv[1], &st) == -1)
  90.     {
  91.         fprintf(stderr, "%s\n", strerror(errno));
  92.         exit(1);
  93.     }
  94.     if (!S_ISDIR(st.st_mode))
  95.     {
  96.         if (!S_ISLNK(st.st_mode))
  97.         {
  98.             printAttributes(st);
  99.             printf("%ld ", st.st_nlink);
  100.             printGroup(&st);
  101.             printGroup(&st);
  102.             printf("%ld ", st.st_size);
  103.             printf("%s\n", argv[1]);
  104.         }
  105.         if (S_ISLNK(st.st_mode))
  106.         {
  107.             printAttributes(st);
  108.             printf("%ld ", st.st_nlink);
  109.             printGroup(&st);
  110.             printGroup(&st);
  111.             printf("%ld ", st.st_size);
  112.             char tmp[PATH_MAX] = "";
  113.             ssize_t r = readlink(argv[1], tmp, sizeof(tmp));
  114.             if (r == -1)
  115.             {
  116.                 fprintf(stderr, "%s\n", strerror(errno));
  117.                 exit(1);
  118.             }
  119.    
  120.             tmp[r] = '\0';
  121.             printf("%s -> ", argv[1]);
  122.             printf("%s\n", tmp);
  123.         }
  124.     }
  125.     else
  126.     {
  127.         int dir_fd = open(argv[1], O_RDONLY | O_DIRECTORY);
  128.         if (dir_fd == -1)
  129.         {
  130.             fprintf(stderr, "%s\n", strerror(errno));
  131.             exit(1);
  132.  
  133.         }
  134.         DIR* dir = fdopendir(dir_fd);
  135.         if (dir == NULL)
  136.         {
  137.             fprintf(stderr, "%s\n", strerror(errno));
  138.             exit(1);
  139.  
  140.         }
  141.         struct dirent* file;
  142.         while(file = readdir(dir))
  143.         {
  144.             char path[PATH_MAX] = "";
  145.             strcat(path, argv[1]);
  146.             strcat(path, "/");
  147.             strcat(path, file->d_name);
  148.             if (lstat(path, &st) != -1)
  149.             {
  150.                 printStat(&st, path);//file->d_name);
  151.             }
  152.             else
  153.             {
  154.                 fprintf(stderr, "%s\n", strerror(errno));
  155.                 exit(1);
  156.             }
  157.  
  158.         }
  159.         closedir(dir);
  160.     }
  161.     return 0;
  162.    
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement