Thanks to phihag, this code is designed to perform binary operations on 2 files.
Example 1. You have two files and you want to compare them, so you make binary XOR.
Example 2. You downloaded a file with jdownloader or sth like, and you transferred the incomplete download to another folder, and then the download manager continues the incomplete parts and creates another file. So you have two seprate files that can complement each other. Now, if you are doing binary OR code for these two files, you have the full file.
WARNING: a larger file will be overwritten with the result of the operation.
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> #include <string.h> int main(int argc, char* argv[]) { int FP1 = 0, FP2 = 0; struct stat St1, St2; char *File1 = NULL, *File2 = NULL; int Rn = 0; if (argc != 4) { printf("Usage: %s File1 File2 Operator\n", argv[0]); exit(1); } //Opening and mapping File1 FP1 = open(argv[1], O_RDWR); if (FP1 == -1) { perror("Error opening file1 for writing"); exit(2); } if (fstat(FP1, &St1) == -1) { perror("Can't determine file1 size"); exit(3); } File1 = (char*) mmap(NULL, St1.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, FP1, 0); if (File1 == MAP_FAILED) { perror("Can't map file1"); exit(4); } //====================== //Opening and mapping File2 FP2 = open(argv[2], O_RDWR); if (FP2 == -1) { perror("Error opening file2 for writing"); exit(2); } if (fstat(FP2, &St2) == -1) { perror("Can't determine file2 size"); exit(3); } File2 = (char*) mmap(NULL, St2.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, FP2, 0); if (File2 == MAP_FAILED) { perror("Can't map file2"); exit(4); } //====================== //Binary operations ssize_t i = 0; switch (*(argv[3])) { case '|': if (St1.st_size <= St2.st_size) for (i = 0; i < St1.st_size; i ++) File2[i] = File1[i] | File2[i]; else for (i = 0; i < St2.st_size; i ++) File1[i] = File1[i] | File2[i]; break; case '&': if (St1.st_size <= St2.st_size) for (i = 0; i < St1.st_size; i ++) File2[i] = File1[i] & File2[i]; else for (i = 0; i < St2.st_size; i ++) File1[i] = File1[i] & File2[i]; break; case '^': if (St1.st_size <= St2.st_size) for (i = 0; i < St1.st_size; i ++) File2[i] = File1[i] ^ File2[i]; else for (i = 0; i < St2.st_size; i ++) File1[i] = File1[i] ^ File2[i]; break; default: perror("Unknown binary operator"); exit(5); } //====================== munmap(File1, St1.st_size); munmap(File2, St2.st_size); close(FP1); close(FP2); //Renaming the changed file and make output char Buffer[1024]; if (St1.st_size <= St2.st_size) { Rn = system(strcat(strcat(strcat(strcat(strcpy(Buffer, "mv \""), argv[2]), "\" \""), argv[2]),"-Mapped\"")); if (Rn == -1) { perror("Unable to rename the new file."); exit(6); } else printf("%s is mapped.\n", argv[2]); } else { Rn = system(strcat(strcat(strcat(strcat(strcpy(Buffer, "mv \""), argv[1]), "\" \""), argv[1]),"-Mapped\"")); if (Rn == -1) { perror("Unable to rename the new file."); exit(6); } else printf("%s is mapped.\n", argv[1]); } //====================== return 0; }
Ashkanvz
source share