Advertisement
hallucinogenic

Ex_D V1

May 24th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <dirent.h>
  8.  
  9. // DEBUG
  10. #define DEBUG 1
  11.  
  12. int UserPermissions(struct stat f1, struct stat f2)
  13. {
  14.   if(DEBUG)
  15.   {
  16.     printf("/////////////////////////////////////////////////////////////////\n");
  17.     printf("//////////////////////// \x1B[1m\x1B[33mUser Permissions\x1B[0m ///////////////////////\n");
  18.     printf("/////////////////////////////////////////////////////////////////\n");
  19.     printf("[F1] ► Read Permission: %d\n", (f1.st_mode & S_IRUSR));
  20.     printf("[F1] ► Write Permission: %d\n", (f1.st_mode & S_IWUSR));
  21.     printf("[F1] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXUSR));
  22.     printf("[F2] ► Read Permission: %d\n", (f2.st_mode & S_IRUSR));
  23.     printf("[F2] ► Write Permission: %d\n", (f1.st_mode & S_IWUSR));
  24.     printf("[F2] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXUSR));
  25.     printf("/////////////////////////////////////////////////////////////////\n");
  26.   }
  27.  
  28.   if(((f1.st_mode & S_IRUSR) == (f2.st_mode & S_IRUSR))
  29.     && ((f1.st_mode & S_IWUSR) == (f2.st_mode & S_IWUSR))
  30.       && ((f1.st_mode & S_IXUSR) == (f2.st_mode & S_IXUSR)))
  31.   {
  32.     return 1;
  33.   }
  34.  
  35.   return 0;
  36. }
  37.  
  38. int GroupPermissions(struct stat f1, struct stat f2)
  39. {
  40.   if(DEBUG)
  41.   {
  42.     printf("/////////////////////////////////////////////////////////////////\n");
  43.     printf("//////////////////////// \x1B[1m\x1B[33mGroup Permissions\x1B[0m //////////////////////\n");
  44.     printf("/////////////////////////////////////////////////////////////////\n");
  45.     printf("[F1] ► Read Permission: %d\n", (f1.st_mode & S_IRGRP));
  46.     printf("[F1] ► Write Permission: %d\n", (f1.st_mode & S_IWGRP));
  47.     printf("[F1] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXGRP));
  48.     printf("[F2] ► Read Permission: %d\n", (f2.st_mode & S_IRGRP));
  49.     printf("[F2] ► Write Permission: %d\n", (f1.st_mode & S_IWGRP));
  50.     printf("[F2] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXGRP));
  51.     printf("/////////////////////////////////////////////////////////////////\n");
  52.   }
  53.  
  54.   if(((f1.st_mode & S_IRGRP) == (f2.st_mode & S_IRGRP))
  55.     && ((f1.st_mode & S_IWGRP) == (f2.st_mode & S_IWGRP))
  56.       && ((f1.st_mode & S_IXGRP) == (f2.st_mode & S_IXGRP)))
  57.   {
  58.     return 1;
  59.   }
  60.  
  61.   return 0;
  62. }
  63.  
  64. int OtherPermissions(struct stat f1, struct stat f2)
  65. {
  66.   if(DEBUG)
  67.   {
  68.     printf("/////////////////////////////////////////////////////////////////\n");
  69.     printf("//////////////////////// \x1B[1m\x1B[33mOther Permissions\x1B[0m //////////////////////\n");
  70.     printf("/////////////////////////////////////////////////////////////////\n");
  71.     printf("[F1] ► Read Permission: %d\n", (f1.st_mode & S_IROTH));
  72.     printf("[F1] ► Write Permission: %d\n", (f1.st_mode & S_IWOTH));
  73.     printf("[F1] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXOTH));
  74.     printf("[F2] ► Read Permission: %d\n", (f2.st_mode & S_IROTH));
  75.     printf("[F2] ► Write Permission: %d\n", (f1.st_mode & S_IWOTH));
  76.     printf("[F2] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXOTH));
  77.     printf("/////////////////////////////////////////////////////////////////\n");
  78.   }
  79.  
  80.   if(((f1.st_mode & S_IRGRP) == (f2.st_mode & S_IRGRP))
  81.     && ((f1.st_mode & S_IWGRP) == (f2.st_mode & S_IWGRP))
  82.       && ((f1.st_mode & S_IXGRP) == (f2.st_mode & S_IXGRP)))
  83.   {
  84.     return 1;
  85.   }
  86.  
  87.   return 0;
  88. }
  89.  
  90. // Função principal, para verificar se os ficheiros são iguais!
  91. int CheckFile(struct stat stat_f1, struct stat stat_f2, int check_file)
  92. {
  93.   if(DEBUG)
  94.   {
  95.     printf("/////////////////////////////////////////////////////////////////\n");
  96.     printf("/////////////////////////// \x1B[1m\x1B[33mFile Sizes\x1B[0m //////////////////////////\n");
  97.     printf("/////////////////////////////////////////////////////////////////\n");
  98.     printf("[F1] ► Tamanho: %ld\n", stat_f1.st_size);
  99.     printf("[F2] ► Tamanho: %ld\n", stat_f2.st_size);
  100.     printf("/////////////////////////////////////////////////////////////////\n");
  101.   }
  102.  
  103.   int Tamanho = (stat_f1.st_size == stat_f2.st_size);
  104.  
  105.   if(DEBUG)
  106.   {
  107.     printf("/////////////////////////////////////////////////////////////////\n");
  108.     printf("/////////////////////////// \x1B[1m\x1B[33mBlocks Used\x1B[0m /////////////////////////\n");
  109.     printf("/////////////////////////////////////////////////////////////////\n");
  110.     printf("[F1] ► Blocks Used: %ld\n", stat_f1.st_blocks);
  111.     printf("[F2] ► Blocks Used: %ld\n", stat_f2.st_blocks);
  112.     printf("/////////////////////////////////////////////////////////////////\n");
  113.   }
  114.  
  115.   int BlocksUsed = (stat_f1.st_blocks == stat_f2.st_blocks);
  116.   int UserP = UserPermissions(stat_f1, stat_f2);
  117.   int GroupP = GroupPermissions(stat_f1, stat_f2);
  118.   int OtherP = OtherPermissions(stat_f1, stat_f2);
  119.  
  120.   if(check_file)
  121.   {
  122.     printf("Tamanho %s\n", Tamanho?"Igual":"Diferente");
  123.     printf("Blocks Usados %s\n", BlocksUsed?"Igual":"Diferente");
  124.     printf("User Permissions %s\n", UserP?"Igual":"Diferente");
  125.     printf("Group Permissions %s\n", GroupP?"Igual":"Diferente");
  126.     printf("Other Permissions %s\n", OtherP?"Igual":"Diferente");
  127.   }
  128.  
  129.   if(UserP == 1 && GroupP == 1 && OtherP == 1 && Tamanho == 1 && BlocksUsed == 1)
  130.   {
  131.     return 1;
  132.   }
  133.   else
  134.   {
  135.     return 0;
  136.   }
  137. }
  138.  
  139. void CheckDirectory(char *f1, char *f2)
  140. {
  141.   struct dirent **namelist1;
  142.   struct dirent **namelist2;
  143.  
  144.   int i = 0;
  145.  
  146.   int n1 = scandir(f1, &namelist1, NULL, alphasort);
  147.   int n2 = scandir(f2, &namelist2, NULL, alphasort);
  148.  
  149.   if(DEBUG)
  150.   {
  151.     printf("/////////////////////////////////////////////////////////////////\n");
  152.     printf("////////////////////////// \x1B[1m\x1B[33mDiretorias\x1B[0m ///////////////////////////\n");
  153.     printf("/////////////////////////////////////////////////////////////////\n");
  154.     printf("Diretoria 1: %s\n", f1);
  155.     printf("Diretoria 2: %s\n", f2);
  156.     printf("Número de Ficheiros/Pastas 1: %d\n", n1);
  157.     printf("Número de Ficheiros/Pastas 2: %d\n", n2);
  158.     printf("/////////////////////////////////////////////////////////////////\n");
  159.   }
  160.  
  161.   if(n1 < 0 || n2 < 0)
  162.   {
  163.     printf("Erro\n");
  164.     return;
  165.   }
  166.  
  167.   if(n1 != n2)
  168.   {
  169.     printf("Directories Differente\n");
  170.     return;
  171.   }
  172.  
  173.   struct stat stat_f1;
  174.   struct stat stat_f2;
  175.  
  176.   for(i = 2; i < n1; i++)
  177.   {
  178.     printf("Ficheiro F1: %s | Ficheiro F2: %s\n", namelist1[i]->d_name, namelist2[i]->d_name);
  179.     if(0 != strcmp(namelist1[i]->d_name, namelist2[i]->d_name))
  180.     {
  181.       printf("Directories Differente\n");
  182.       return;
  183.     }
  184.  
  185.     stat(namelist1[i]->d_name, &stat_f1);
  186.     stat(namelist2[i]->d_name, &stat_f2);
  187.  
  188.     if(((stat_f1.st_mode & S_IFREG) == S_IFREG) && ((stat_f2.st_mode & S_IFREG) == S_IFREG))
  189.     {
  190.       if(!CheckFile(stat_f1, stat_f2, 0))
  191.       {
  192.         printf("Directories Differente\n");
  193.         return;
  194.       }
  195.     }
  196.   }
  197.  
  198.   printf("Directories Igual\n");
  199.  
  200.   free(namelist1);
  201.   free(namelist2);
  202. }
  203.  
  204. int main()
  205. {
  206.   char f1[1000];
  207.   char f2[1000];
  208.  
  209.   if(DEBUG)
  210.   {
  211.     printf("Insere os nomes dos ficheiros ou diretorias: ");
  212.   }
  213.   scanf("%s %s", f1, f2);
  214.  
  215.   if(0 == strcmp(f1, f2))
  216.   {
  217.     printf("Nomes Iguais\n");
  218.     return 0;
  219.   }
  220.  
  221.   // Creates 2 files;
  222.   struct stat stat_f1;
  223.   struct stat stat_f2;
  224.  
  225.   int v1 = stat(f1, &stat_f1);
  226.   int v2 = stat(f2, &stat_f2);
  227.  
  228.   if(v1 == -1 || v2 == -1)
  229.   {
  230.     printf("Erro\n");
  231.     return 0;
  232.   }
  233.  
  234.   int IsF1ADir = ((stat_f1.st_mode & S_IFDIR) == S_IFDIR);
  235.   int IsF2ADir = ((stat_f2.st_mode & S_IFDIR) == S_IFDIR);
  236.  
  237.   if((IsF1ADir && !IsF2ADir) || (!IsF1ADir && IsF2ADir))
  238.   {
  239.     printf("Erro\n");
  240.     return 0;
  241.   }
  242.  
  243.   if(IsF1ADir && IsF2ADir)
  244.   {
  245.     CheckDirectory(f1, f2);
  246.   }
  247.   else
  248.   {
  249.     if(CheckFile(stat_f1, stat_f2, 1))
  250.     {
  251.       printf("Ficheiros Igual\n");
  252.     }
  253.     else
  254.     {
  255.       printf("Ficheiros Diferente\n");
  256.     }
  257.   }
  258.  
  259.  
  260.   return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement