Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cat cp.c
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<errno.h>
- #define N 1024
- int main(int argc, char* argv[]) {
- char buf[N];
- int sf, df, size;
- if ((sf = open(argv[1], O_RDONLY)) == -1) {
- perror(argv[1]);
- exit(1);
- }
- if ((df = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0755)) == -1) {
- perror(argv[2]);
- close(sf);
- exit(2);
- }
- while((size = read(sf, buf, N)) > 0) {
- write(df, buf, size);
- }
- close(df);
- close(sf);
- return 0;
- }
- cat cmp.c
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<stdio.h>
- #define N 1024
- int main(int argc, char *argv[]) {
- int fd1, fd2;
- char buf1[N], buf2[N];
- int n1, n2, i;
- if ((fd1 = open(argv[1], O_RDONLY)) == -1) {
- perror(argv[1]);
- return 2;
- }
- if ((fd2 = open(argv[2], O_RDONLY)) == -1) {
- perror(argv[2]);
- return 3;
- }
- while((n1 = read(fd1, buf1, N)) > 0 && (n2 = read(fd2, buf2, N)) > 0) {
- if (n1 != n2) {
- return 1;
- }
- for (i = 0; i < n1; i++) {
- if (buf1[i] != buf2[i]) {
- return 1;
- }
- }
- }
- return 0;
- close(fd2);
- close(fd1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement