Created
August 4, 2017 18:29
-
-
Save sudikrt/5ef8c59f983d1f45871fa246927c370a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <fcntl.h> | |
| #include <errno.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #define ORGINAL_FILE "/root/original_file" | |
| #define DUPLICATE_FILE "/root/deplicate_file" | |
| #define NUMBER_OF_VALUES 200000 | |
| #define RANGE 200 | |
| int main () | |
| { | |
| int i = 0 ; | |
| int fd = -1; | |
| int fd_1 = -1; | |
| int integer = 0; | |
| int integer_1 = 0; | |
| int ret = -1; | |
| int are_both_file_equal = 0; | |
| /* | |
| * Creating #ORGINAL_FILE | |
| * i.e generating #NUMBER_OF_VALUES of random integers | |
| * ranging from 0 to #RANGE-1 | |
| * */ | |
| fd = open (ORGINAL_FILE, O_WRONLY | O_CREAT, 0644); | |
| if (fd < 0) { | |
| fprintf (stderr, "Failed to open %s for write\n", ORGINAL_FILE); | |
| goto out; | |
| } | |
| for (i = 0; i < NUMBER_OF_VALUES; i++) { | |
| integer = rand() % RANGE; | |
| ret = write (fd, &integer, sizeof (int)); | |
| if (ret < 0) { | |
| fprintf (stderr, "Failed to write to file %s\n", ORGINAL_FILE); | |
| goto out; | |
| } | |
| } | |
| close (fd); | |
| fd = -1; | |
| /* | |
| * Copying the contents of #ORGINAL_FILE to #DUPLICATE_FILE | |
| * i.e one integer at a time. This is slow way of doing a copy | |
| * as we read and write only 4 bytes at a time. We can have a bigger | |
| * read and write but the point of this program is to demonstrate | |
| * int read and write. | |
| * | |
| * */ | |
| fd = open (ORGINAL_FILE, O_RDONLY); | |
| if (fd < 0) { | |
| fprintf (stderr, "Failed to open %s for read\n", ORGINAL_FILE); | |
| goto out; | |
| } | |
| fd_1 = open (DUPLICATE_FILE, O_WRONLY | O_CREAT, 0644); | |
| if (fd_1 < 0) { | |
| fprintf (stderr, "Failed to open %s for write\n", DUPLICATE_FILE); | |
| goto out; | |
| } | |
| while ((ret = read (fd, &integer, sizeof (int))) != 0) { | |
| if (ret < 0) { | |
| fprintf (stderr, "Failed to read from file %s\n", ORGINAL_FILE); | |
| goto out; | |
| } | |
| ret = write (fd_1, &integer, sizeof (int)); | |
| if (ret < 0) { | |
| fprintf (stderr, "Failed to write to file %s\n", DUPLICATE_FILE); | |
| goto out; | |
| } | |
| } | |
| close (fd); | |
| fd = -1; | |
| close (fd_1); | |
| fd_1 = -1; | |
| /* | |
| * Verifying both the files are same or not. | |
| * Again this is slower way of doing things. | |
| * We could use md5 or sha1 hashes to compare | |
| * files, instead of comparing 4 bytes at a time. | |
| * But again the point of the program is to show | |
| * int read and write. | |
| * | |
| * */ | |
| fd = open (ORGINAL_FILE, O_RDONLY); | |
| if (fd < 0) { | |
| fprintf (stderr, "Failed to open %s for read\n", ORGINAL_FILE); | |
| goto out; | |
| } | |
| fd_1 = open (DUPLICATE_FILE, O_RDONLY); | |
| if (fd_1 < 0) { | |
| fprintf (stderr, "Failed to open %s for write\n", DUPLICATE_FILE); | |
| goto out; | |
| } | |
| while ((ret = read (fd, &integer, sizeof (int))) != 0) { | |
| if (ret < 0) { | |
| fprintf (stderr, "Failed to read from file %s\n", ORGINAL_FILE); | |
| goto out; | |
| } | |
| ret = read (fd_1, &integer_1, sizeof (int)); | |
| if (ret < 0) { | |
| fprintf (stderr, "Failed to read from file %s\n", DUPLICATE_FILE); | |
| goto out; | |
| } | |
| /* This means fd_1 has reached end of file before fd*/ | |
| if (ret == 0) { | |
| goto out; | |
| } | |
| if (integer != integer_1) { | |
| goto out; | |
| } | |
| } | |
| are_both_file_equal = 1; | |
| ret = 0; | |
| out: | |
| if (ret >= 0) { | |
| if (are_both_file_equal) { | |
| fprintf (stdout, "%s is same as %s\n", DUPLICATE_FILE, ORGINAL_FILE); | |
| } else { | |
| fprintf (stdout, "%s is NOT same as %s\n", DUPLICATE_FILE, ORGINAL_FILE); | |
| } | |
| } | |
| if (fd >= 0) { | |
| close (fd); | |
| } | |
| if (fd_1 >= 0) { | |
| close (fd_1); | |
| } | |
| return ret; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment