Advertisement
nblknn

OS 6

Nov 26th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. #define MAXSTRLENGTH 50
  10.  
  11. void getFilePath(char *dirPath, char *fileName, char **buf) {
  12.     memset(*buf, 0, MAXSTRLENGTH * sizeof(char));
  13.     strcat(*buf, dirPath);
  14.     if ((*buf)[strlen(*buf) - 1] != '/') {
  15.         (*buf)[strlen(*buf)] = '/';
  16.     }
  17.     strcat(*buf, fileName);
  18. }
  19.  
  20. void printFileInfo(char *filePath, struct stat fileStat, FILE *stream) {
  21.     char fileMode[10] = "----------";
  22.     if (fileStat.st_mode & S_IRUSR) fileMode[1] = 'r';
  23.     if (fileStat.st_mode & S_IWUSR) fileMode[2] = 'w';
  24.     if (fileStat.st_mode & S_IXUSR) fileMode[3] = 'x';
  25.     if (fileStat.st_mode & S_IRGRP) fileMode[4] = 'r';
  26.     if (fileStat.st_mode & S_IWGRP) fileMode[5] = 'w';
  27.     if (fileStat.st_mode & S_IXGRP) fileMode[6] = 'x';
  28.     if (fileStat.st_mode & S_IROTH) fileMode[7] = 'r';
  29.     if (fileStat.st_mode & S_IWOTH) fileMode[8] = 'w';
  30.     if (fileStat.st_mode & S_IXOTH) fileMode[9] = 'x';
  31.     fprintf(stream, "File path: %s\nSize: %d bytes\nCreation date: %sIndex descriptor: %d\nFile permissions: %s\n",
  32.     filePath, fileStat.st_size, ctime(&fileStat.st_ctime), fileStat.st_ino, fileMode);
  33. }
  34.  
  35. void printInfo(char *filePath1, struct stat fileStat1, char *filePath2, struct stat fileStat2, FILE *stream) {
  36.     printFileInfo(filePath1, fileStat1, stream);
  37.     fprintf(stream, "\n");
  38.     printFileInfo(filePath2, fileStat2, stream);
  39.     fprintf(stream, "------------\n");
  40. }
  41.  
  42. void searchFileInDir2(char *filePath1, struct stat fileStat1, char *dirPath, FILE *fout) {
  43.     struct dirent *file2;
  44.     struct stat fileStat2;
  45.     DIR *dir = opendir(dirPath);
  46.     char *filePath2 = malloc(MAXSTRLENGTH * sizeof(char));
  47.     while ((file2 = readdir(dir)) != NULL) {
  48.         getFilePath(dirPath, file2->d_name, &filePath2);
  49.         stat(filePath2, &fileStat2);
  50.         if (S_ISDIR(fileStat2.st_mode) && strcmp(file2->d_name, ".") && strcmp(file2->d_name, "..")) {
  51.             searchFileInDir2(filePath1, fileStat1, filePath2, fout);
  52.         }
  53.         else if (S_ISREG(fileStat2.st_mode) && fileStat1.st_size == fileStat2.st_size && strcmp(filePath2, filePath1)) {
  54.             FILE *f1 = fopen(filePath1, "r");
  55.             FILE *f2 = fopen(filePath2, "r");
  56.             int temp = 0;
  57.             bool isIdentical = true;
  58.             while (isIdentical && temp != EOF) {
  59.                 isIdentical = (temp = fgetc(f1)) == fgetc(f2);
  60.             }
  61.             fclose(f1);
  62.             fclose(f2);
  63.             if (isIdentical) {
  64.                 printInfo(filePath1, fileStat1, filePath2, fileStat2, stdout);
  65.                 printInfo(filePath1, fileStat1, filePath2, fileStat2, fout);
  66.             }
  67.         }
  68.     }
  69.     free(filePath2);
  70.     closedir(dir);
  71. }
  72.  
  73. void searchIdenticalFiles(char *dirPath1, char *dirPath2, FILE *fout) {
  74.     struct dirent *file;
  75.     struct stat fileStat;
  76.     DIR *dir1 = opendir(dirPath1);
  77.     char *filePath = malloc(MAXSTRLENGTH * sizeof(char));
  78.     while ((file = readdir(dir1)) != NULL) {
  79.         getFilePath(dirPath1, file->d_name, &filePath);
  80.         stat(filePath, &fileStat);
  81.         if (S_ISDIR(fileStat.st_mode) && strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  82.             searchIdenticalFiles(filePath, dirPath2, fout);
  83.         }
  84.         else if (S_ISREG(fileStat.st_mode)) {
  85.             searchFileInDir2(filePath, fileStat, dirPath2, fout);
  86.         }
  87.     }
  88.     free(filePath);
  89.     closedir(dir1);
  90. }
  91.  
  92. int main(int argc, char *argv[]) {
  93.     if (argc != 4) {
  94.         printf("Incorrect number of arguments.\n");
  95.         return 1;
  96.     }
  97.     FILE *fout = fopen(argv[3], "w");
  98.     searchIdenticalFiles(argv[1], argv[2], fout);
  99.     fclose(fout);
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement