Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <time.h>
- #define MAXSTRLENGTH 50
- void getFilePath(char *dirPath, char *fileName, char **buf) {
- memset(*buf, 0, MAXSTRLENGTH * sizeof(char));
- strcat(*buf, dirPath);
- if ((*buf)[strlen(*buf) - 1] != '/') {
- (*buf)[strlen(*buf)] = '/';
- }
- strcat(*buf, fileName);
- }
- void printFileInfo(char *filePath, struct stat fileStat, FILE *stream) {
- char fileMode[10] = "----------";
- if (fileStat.st_mode & S_IRUSR) fileMode[1] = 'r';
- if (fileStat.st_mode & S_IWUSR) fileMode[2] = 'w';
- if (fileStat.st_mode & S_IXUSR) fileMode[3] = 'x';
- if (fileStat.st_mode & S_IRGRP) fileMode[4] = 'r';
- if (fileStat.st_mode & S_IWGRP) fileMode[5] = 'w';
- if (fileStat.st_mode & S_IXGRP) fileMode[6] = 'x';
- if (fileStat.st_mode & S_IROTH) fileMode[7] = 'r';
- if (fileStat.st_mode & S_IWOTH) fileMode[8] = 'w';
- if (fileStat.st_mode & S_IXOTH) fileMode[9] = 'x';
- fprintf(stream, "File path: %s\nSize: %d bytes\nCreation date: %sIndex descriptor: %d\nFile permissions: %s\n",
- filePath, fileStat.st_size, ctime(&fileStat.st_ctime), fileStat.st_ino, fileMode);
- }
- void printInfo(char *filePath1, struct stat fileStat1, char *filePath2, struct stat fileStat2, FILE *stream) {
- printFileInfo(filePath1, fileStat1, stream);
- fprintf(stream, "\n");
- printFileInfo(filePath2, fileStat2, stream);
- fprintf(stream, "------------\n");
- }
- void searchFileInDir2(char *filePath1, struct stat fileStat1, char *dirPath, FILE *fout) {
- struct dirent *file2;
- struct stat fileStat2;
- DIR *dir = opendir(dirPath);
- char *filePath2 = malloc(MAXSTRLENGTH * sizeof(char));
- while ((file2 = readdir(dir)) != NULL) {
- getFilePath(dirPath, file2->d_name, &filePath2);
- stat(filePath2, &fileStat2);
- if (S_ISDIR(fileStat2.st_mode) && strcmp(file2->d_name, ".") && strcmp(file2->d_name, "..")) {
- searchFileInDir2(filePath1, fileStat1, filePath2, fout);
- }
- else if (S_ISREG(fileStat2.st_mode) && fileStat1.st_size == fileStat2.st_size && strcmp(filePath2, filePath1)) {
- FILE *f1 = fopen(filePath1, "r");
- FILE *f2 = fopen(filePath2, "r");
- int temp = 0;
- bool isIdentical = true;
- while (isIdentical && temp != EOF) {
- isIdentical = (temp = fgetc(f1)) == fgetc(f2);
- }
- fclose(f1);
- fclose(f2);
- if (isIdentical) {
- printInfo(filePath1, fileStat1, filePath2, fileStat2, stdout);
- printInfo(filePath1, fileStat1, filePath2, fileStat2, fout);
- }
- }
- }
- free(filePath2);
- closedir(dir);
- }
- void searchIdenticalFiles(char *dirPath1, char *dirPath2, FILE *fout) {
- struct dirent *file;
- struct stat fileStat;
- DIR *dir1 = opendir(dirPath1);
- char *filePath = malloc(MAXSTRLENGTH * sizeof(char));
- while ((file = readdir(dir1)) != NULL) {
- getFilePath(dirPath1, file->d_name, &filePath);
- stat(filePath, &fileStat);
- if (S_ISDIR(fileStat.st_mode) && strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
- searchIdenticalFiles(filePath, dirPath2, fout);
- }
- else if (S_ISREG(fileStat.st_mode)) {
- searchFileInDir2(filePath, fileStat, dirPath2, fout);
- }
- }
- free(filePath);
- closedir(dir1);
- }
- int main(int argc, char *argv[]) {
- if (argc != 4) {
- printf("Incorrect number of arguments.\n");
- return 1;
- }
- FILE *fout = fopen(argv[3], "w");
- searchIdenticalFiles(argv[1], argv[2], fout);
- fclose(fout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement