kamilosxd678

Moj Eles

Jan 8th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <pwd.h>
  10. #include <grp.h>
  11. #include <time.h>
  12. //Moja kolejka
  13. struct s_MyDeque{
  14.     struct s_MyDeque *next;
  15.     struct s_MyDeque *prev;
  16.     char *dirPath;
  17. };
  18.  
  19. struct s_MyDeque *first;
  20. struct s_MyDeque *last;
  21. //Variables
  22. DIR*            pDIR;
  23. struct dirent*  pDirEnt;
  24. struct stat st;
  25. struct passwd*  pwd;
  26. struct group*   grp;
  27. char*           presentDir = ".";
  28. char*           fullDir = NULL;
  29. char*           useDate;
  30. int             aFlag = 0;
  31. int             RFlag = 0;
  32. int             lFlag = 0;
  33. int             mFlag = 0;
  34. int             AFlag = 0;
  35. int             GFlag = 0;
  36. int             firstDir = 0;
  37. //Functions
  38. void process_record_long(char* targetDir){
  39.     pDIR = opendir(targetDir);
  40.     pDirEnt = readdir(pDIR);
  41.     if(RFlag)
  42.         printf("In dir: %s\n",targetDir);
  43.     while(pDirEnt != NULL){
  44.         if(aFlag == 1 || pDirEnt->d_name[0] != '.'){
  45.             if(firstDir && mFlag){
  46.                 printf(", ");
  47.             }
  48.             firstDir = 1;
  49.             asprintf(&fullDir,"%s/%s",targetDir,pDirEnt->d_name);
  50.             lstat(fullDir, &st);
  51.             pwd = getpwuid(st.st_uid);
  52.             grp = getgrgid(st.st_gid);
  53.             if(lFlag){
  54.                 printf((S_ISDIR(st.st_mode)) ? "d" : "-");
  55.                 printf((st.st_mode & S_IRUSR) ? "r" : "-");
  56.                 printf((st.st_mode & S_IWUSR) ? "w" : "-");
  57.                 printf((st.st_mode & S_IXUSR) ? "x" : "-");
  58.                 printf((st.st_mode & S_IRGRP) ? "r" : "-");
  59.                 printf((st.st_mode & S_IWGRP) ? "w" : "-");
  60.                 printf((st.st_mode & S_IXGRP) ? "x" : "-");
  61.                 printf((st.st_mode & S_IROTH) ? "r" : "-");
  62.                 printf((st.st_mode & S_IWOTH) ? "w" : "-");
  63.                 printf((st.st_mode & S_IXOTH) ? "x" : "-");
  64.                 printf(" %d", (int)st.st_nlink);
  65.                 printf(" %s", pwd->pw_name);
  66.                 if(!GFlag)
  67.                      printf(" %s", grp->gr_name);
  68.                 printf(" %d", (int)st.st_size);
  69.                 useDate = ctime(&st.st_mtime);
  70.                 memmove (useDate,useDate+4,12);
  71.                 useDate[12] = NULL;
  72.                 printf( " %s ", useDate);
  73.             }
  74.             printf("%s", pDirEnt->d_name);
  75.             if(lFlag)
  76.                 printf("\n");
  77.             else if(mFlag){ }
  78.             else
  79.                 printf(" ");
  80.             if(RFlag){
  81.                 if(S_ISDIR(st.st_mode)){
  82.                     last->next = (struct s_MyDeque*)malloc(sizeof(struct s_MyDeque));
  83.                     last->next->dirPath = fullDir;
  84.                     last = last->next;
  85.                     last->next = NULL;
  86.                 }
  87.             }
  88.         }
  89.         pDirEnt=readdir(pDIR);
  90.     }
  91.     first = first->next;
  92.     /*if(first->prev != NULL){
  93.         free(first->prev);
  94.         first->prev = NULL;
  95.     }*/
  96.     if(first != NULL)
  97.         process_record_long(first->dirPath);
  98.     if(!lFlag)
  99.         printf("\n");
  100. }
  101.  
  102. int main(int argc, char *argv[])
  103. {
  104.     int option;
  105.     int index;
  106.  
  107.     while((option = getopt(argc, argv, "GlaRm")) != -1)
  108.     {
  109.         switch(option)
  110.         {
  111.         case 'l':
  112.             lFlag = 1;
  113.             break;
  114.         case 'a':
  115.             aFlag = 1;
  116.             break;
  117.         case 'R':
  118.             RFlag = 1;
  119.             break;
  120.         case 'm':
  121.             mFlag = 1;
  122.             break;
  123.         case 'G':
  124.             GFlag = 1;
  125.             break;
  126.         case '?':
  127.             printf("%c", optopt);
  128.             break;
  129.         }
  130.     }
  131.     first = (struct s_MyDeque*)malloc(sizeof *first);
  132.     first->next = NULL;
  133.     first->prev = NULL;
  134.     last = first;
  135.  
  136.     for(index = optind; index < argc; index++)
  137.     {
  138.         presentDir = argv[index];
  139.     }
  140.     first->dirPath = presentDir;
  141.     process_record_long(first->dirPath);   
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment