Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <dirent.h>
- // DEBUG
- #define DEBUG 1
- int UserPermissions(struct stat f1, struct stat f2)
- {
- if(DEBUG)
- {
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("//////////////////////// \x1B[1m\x1B[33mUser Permissions\x1B[0m ///////////////////////\n");
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("[F1] ► Read Permission: %d\n", (f1.st_mode & S_IRUSR));
- printf("[F1] ► Write Permission: %d\n", (f1.st_mode & S_IWUSR));
- printf("[F1] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXUSR));
- printf("[F2] ► Read Permission: %d\n", (f2.st_mode & S_IRUSR));
- printf("[F2] ► Write Permission: %d\n", (f1.st_mode & S_IWUSR));
- printf("[F2] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXUSR));
- printf("/////////////////////////////////////////////////////////////////\n");
- }
- if(((f1.st_mode & S_IRUSR) == (f2.st_mode & S_IRUSR))
- && ((f1.st_mode & S_IWUSR) == (f2.st_mode & S_IWUSR))
- && ((f1.st_mode & S_IXUSR) == (f2.st_mode & S_IXUSR)))
- {
- return 1;
- }
- return 0;
- }
- int GroupPermissions(struct stat f1, struct stat f2)
- {
- if(DEBUG)
- {
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("//////////////////////// \x1B[1m\x1B[33mGroup Permissions\x1B[0m //////////////////////\n");
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("[F1] ► Read Permission: %d\n", (f1.st_mode & S_IRGRP));
- printf("[F1] ► Write Permission: %d\n", (f1.st_mode & S_IWGRP));
- printf("[F1] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXGRP));
- printf("[F2] ► Read Permission: %d\n", (f2.st_mode & S_IRGRP));
- printf("[F2] ► Write Permission: %d\n", (f1.st_mode & S_IWGRP));
- printf("[F2] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXGRP));
- printf("/////////////////////////////////////////////////////////////////\n");
- }
- if(((f1.st_mode & S_IRGRP) == (f2.st_mode & S_IRGRP))
- && ((f1.st_mode & S_IWGRP) == (f2.st_mode & S_IWGRP))
- && ((f1.st_mode & S_IXGRP) == (f2.st_mode & S_IXGRP)))
- {
- return 1;
- }
- return 0;
- }
- int OtherPermissions(struct stat f1, struct stat f2)
- {
- if(DEBUG)
- {
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("//////////////////////// \x1B[1m\x1B[33mOther Permissions\x1B[0m //////////////////////\n");
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("[F1] ► Read Permission: %d\n", (f1.st_mode & S_IROTH));
- printf("[F1] ► Write Permission: %d\n", (f1.st_mode & S_IWOTH));
- printf("[F1] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXOTH));
- printf("[F2] ► Read Permission: %d\n", (f2.st_mode & S_IROTH));
- printf("[F2] ► Write Permission: %d\n", (f1.st_mode & S_IWOTH));
- printf("[F2] ► Execute/Search Permission: %d\n", (f1.st_mode & S_IXOTH));
- printf("/////////////////////////////////////////////////////////////////\n");
- }
- if(((f1.st_mode & S_IRGRP) == (f2.st_mode & S_IRGRP))
- && ((f1.st_mode & S_IWGRP) == (f2.st_mode & S_IWGRP))
- && ((f1.st_mode & S_IXGRP) == (f2.st_mode & S_IXGRP)))
- {
- return 1;
- }
- return 0;
- }
- // Função principal, para verificar se os ficheiros são iguais!
- int CheckFile(struct stat stat_f1, struct stat stat_f2, int check_file)
- {
- if(DEBUG)
- {
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("/////////////////////////// \x1B[1m\x1B[33mFile Sizes\x1B[0m //////////////////////////\n");
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("[F1] ► Tamanho: %ld\n", stat_f1.st_size);
- printf("[F2] ► Tamanho: %ld\n", stat_f2.st_size);
- printf("/////////////////////////////////////////////////////////////////\n");
- }
- int Tamanho = (stat_f1.st_size == stat_f2.st_size);
- if(DEBUG)
- {
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("/////////////////////////// \x1B[1m\x1B[33mBlocks Used\x1B[0m /////////////////////////\n");
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("[F1] ► Blocks Used: %ld\n", stat_f1.st_blocks);
- printf("[F2] ► Blocks Used: %ld\n", stat_f2.st_blocks);
- printf("/////////////////////////////////////////////////////////////////\n");
- }
- int BlocksUsed = (stat_f1.st_blocks == stat_f2.st_blocks);
- int UserP = UserPermissions(stat_f1, stat_f2);
- int GroupP = GroupPermissions(stat_f1, stat_f2);
- int OtherP = OtherPermissions(stat_f1, stat_f2);
- if(check_file)
- {
- printf("Tamanho %s\n", Tamanho?"Igual":"Diferente");
- printf("Blocks Usados %s\n", BlocksUsed?"Igual":"Diferente");
- printf("User Permissions %s\n", UserP?"Igual":"Diferente");
- printf("Group Permissions %s\n", GroupP?"Igual":"Diferente");
- printf("Other Permissions %s\n", OtherP?"Igual":"Diferente");
- }
- if(UserP == 1 && GroupP == 1 && OtherP == 1 && Tamanho == 1 && BlocksUsed == 1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void CheckDirectory(char *f1, char *f2)
- {
- struct dirent **namelist1;
- struct dirent **namelist2;
- int i = 0;
- int n1 = scandir(f1, &namelist1, NULL, alphasort);
- int n2 = scandir(f2, &namelist2, NULL, alphasort);
- if(DEBUG)
- {
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("////////////////////////// \x1B[1m\x1B[33mDiretorias\x1B[0m ///////////////////////////\n");
- printf("/////////////////////////////////////////////////////////////////\n");
- printf("Diretoria 1: %s\n", f1);
- printf("Diretoria 2: %s\n", f2);
- printf("Número de Ficheiros/Pastas 1: %d\n", n1);
- printf("Número de Ficheiros/Pastas 2: %d\n", n2);
- printf("/////////////////////////////////////////////////////////////////\n");
- }
- if(n1 < 0 || n2 < 0)
- {
- printf("Erro\n");
- return;
- }
- if(n1 != n2)
- {
- printf("Directories Differente\n");
- return;
- }
- struct stat stat_f1;
- struct stat stat_f2;
- for(i = 2; i < n1; i++)
- {
- printf("Ficheiro F1: %s | Ficheiro F2: %s\n", namelist1[i]->d_name, namelist2[i]->d_name);
- if(0 != strcmp(namelist1[i]->d_name, namelist2[i]->d_name))
- {
- printf("Directories Differente\n");
- return;
- }
- stat(namelist1[i]->d_name, &stat_f1);
- stat(namelist2[i]->d_name, &stat_f2);
- if(((stat_f1.st_mode & S_IFREG) == S_IFREG) && ((stat_f2.st_mode & S_IFREG) == S_IFREG))
- {
- if(!CheckFile(stat_f1, stat_f2, 0))
- {
- printf("Directories Differente\n");
- return;
- }
- }
- }
- printf("Directories Igual\n");
- free(namelist1);
- free(namelist2);
- }
- int main()
- {
- char f1[1000];
- char f2[1000];
- if(DEBUG)
- {
- printf("Insere os nomes dos ficheiros ou diretorias: ");
- }
- scanf("%s %s", f1, f2);
- if(0 == strcmp(f1, f2))
- {
- printf("Nomes Iguais\n");
- return 0;
- }
- // Creates 2 files;
- struct stat stat_f1;
- struct stat stat_f2;
- int v1 = stat(f1, &stat_f1);
- int v2 = stat(f2, &stat_f2);
- if(v1 == -1 || v2 == -1)
- {
- printf("Erro\n");
- return 0;
- }
- int IsF1ADir = ((stat_f1.st_mode & S_IFDIR) == S_IFDIR);
- int IsF2ADir = ((stat_f2.st_mode & S_IFDIR) == S_IFDIR);
- if((IsF1ADir && !IsF2ADir) || (!IsF1ADir && IsF2ADir))
- {
- printf("Erro\n");
- return 0;
- }
- if(IsF1ADir && IsF2ADir)
- {
- CheckDirectory(f1, f2);
- }
- else
- {
- if(CheckFile(stat_f1, stat_f2, 1))
- {
- printf("Ficheiros Igual\n");
- }
- else
- {
- printf("Ficheiros Diferente\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement