Advertisement
Tobiasz931

My Little ls v0.1

Dec 11th, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <sys/stat.h>
  4. #include <pwd.h>
  5. #include <grp.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8.  
  9. char *prawa_dost(int tryb) {
  10.      char *bufor;
  11.      int i;
  12.      bufor=(char *) malloc (19);
  13.      for (i=9;i>=0;i--){
  14.          if (((tryb >> i) % 2) == 1) {
  15.              if ((i % 3) == 0)
  16.                 bufor[9 - i] = 'x';
  17.              if ((i % 3) == 1)
  18.                 bufor[9 - i] = 'w';
  19.              if ((i % 3) == 2)
  20.                 bufor[9 - i] = 'r';
  21.          }
  22.          else
  23.             bufor[9 - i] = '-';        
  24.      }
  25.      bufor[10] = 0;
  26.      if (S_ISDIR (tryb)) {
  27.          bufor[0] = 'd';
  28.          return bufor;
  29.        }
  30.      if (S_ISBLK (tryb)) {
  31.          bufor[0] = 'b';
  32.          return bufor;
  33.        }
  34.      if (S_ISSOCK (tryb)) {
  35.          bufor[0] = 's';
  36.          return bufor;
  37.       }
  38.      if (S_ISCHR (tryb)) {
  39.          bufor[0] = 'c';
  40.          return bufor;
  41.        }
  42.      if (S_ISFIFO (tryb)) {
  43.          bufor[0] = 'p';
  44.          return bufor;
  45.        }
  46.      if (S_ISLNK (tryb)) {
  47.          bufor[0] = 'l';
  48.          return bufor;
  49.        }
  50.      bufor[0]='-'; // w przec. wypadku
  51.      return bufor;
  52. }
  53.  
  54.  
  55. int main(int argc,char *argv[]){
  56.  
  57. DIR *folder;
  58. struct dirent  *wpis;  
  59. struct stat    info;
  60. struct tm      *czas;
  61. struct passwd  *pw;
  62. struct group   *gr;
  63.  
  64. if((folder = opendir("."))==NULL)
  65.   printf("Blad odczytu katalogu");
  66. else
  67.     {
  68.   while((wpis=readdir(folder))!=NULL)
  69.     {
  70.         if(wpis->d_name[0] == '.') continue;
  71.         lstat(wpis->d_name,&info);
  72.             pw   = getpwuid(info.st_uid);
  73.             gr   = getgrgid(info.st_gid);
  74.             czas = gmtime(&info.st_mtime);
  75.         printf("%s ",prawa_dost(info.st_mode));
  76.         printf("% 10s ", pw->pw_name);
  77.         printf("% 10s ", gr->gr_name);
  78.         printf("%02d %02d %04d %02d:%02d",czas->tm_mday,((czas->tm_mon)+1),((czas->tm_year)+1900), czas->tm_hour+2, czas->tm_min);
  79.         printf("% 8d ", (int)info.st_size);
  80.         printf("%s ", wpis->d_name);
  81.         puts("");
  82.         }
  83.     }
  84.   return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement