Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- filexorer by xerpi
- compile with:
- gcc -O3 filexorer.c -o filexorer
- usage:
- filexorer <input file 1> <input file 2>
- the output will be <input file 1>.out
- *file 1 and file 2 sizes must be equal*
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- #define BUF_SIZE (4096)
- void print_usage();
- int main(int argc, char *argv[])
- {
- int ret_val = EXIT_SUCCESS;
- if (argc < 3) {
- print_usage();
- ret_val = EXIT_FAILURE;
- goto exit_fail_1;
- }
- FILE *fd_in1, *fd_in2, *fd_out;
- if (!(fd_in1 = fopen(argv[1], "rb"))) {
- printf("Error opening input file 1\n");
- ret_val = EXIT_FAILURE;
- goto exit_fail_1;
- }
- if (!(fd_in2 = fopen(argv[2], "rb"))) {
- printf("Error opening input file 2\n");
- ret_val = EXIT_FAILURE;
- goto exit_fail_2;
- }
- fseek(fd_in1, 0, SEEK_END);
- long int in_size1 = ftell(fd_in1);
- fseek(fd_in1, 0, SEEK_SET);
- fseek(fd_in2, 0, SEEK_END);
- long int in_size2 = ftell(fd_in2);
- fseek(fd_in2, 0, SEEK_SET);
- if (in_size1 != in_size2) {
- printf("ERROR: input files size are different\n");
- ret_val = EXIT_FAILURE;
- goto exit_fail_3;
- }
- char *out_name = malloc(strlen(argv[1]) + strlen(".out") + 1);
- sprintf(out_name, "%s.out", argv[1]);
- if (!(fd_out = fopen(out_name, "wb+"))) {
- printf("Cannot create output file\n");
- ret_val = EXIT_FAILURE;
- goto exit_fail_4;
- }
- unsigned char data_buf1[BUF_SIZE];
- unsigned char data_buf2[BUF_SIZE];
- #define BAR_LEN 50
- long int temp_read = 0;
- long int step_bytes = in_size1/100;
- long int percent = 1;
- size_t bytes_read;
- while ((bytes_read = fread(data_buf1, 1, BUF_SIZE, fd_in1)) &&
- fread(data_buf2, 1, BUF_SIZE, fd_in2)) {
- size_t i;
- for (i = 0; i < bytes_read; i++) {
- data_buf1[i] ^= data_buf2[i];
- }
- fwrite(data_buf1, 1, bytes_read, fd_out);
- //Progress bar
- temp_read += bytes_read;
- if (temp_read >= step_bytes) {
- percent++;
- temp_read = 0;
- printf("%3li%% [", percent);
- int j;
- int bar_size = (BAR_LEN*percent)/100;
- for (j = 0; j < BAR_LEN; j++) {
- if (j < bar_size) printf("=");
- else if (j == bar_size) printf(">");
- else printf(" ");
- }
- printf("]\r");
- fflush(stdout);
- }
- }
- fflush(fd_out);
- fclose(fd_out);
- printf("\nFinished!\n");
- exit_fail_4:
- free(out_name);
- exit_fail_3:
- fclose(fd_in2);
- exit_fail_2:
- fclose(fd_in1);
- exit_fail_1:
- return ret_val;
- }
- void print_usage()
- {
- printf("filexorer by xerpi\n"
- "usage: filexorer <input file 1> <input file 2>\n"
- "the output will be <input file 1>.out\n"
- "file 1 and file 2 sizes must be equal\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement