diff --git a/mycp.c b/mycp.c new file mode 100644 index 0000000..d026e10 --- /dev/null +++ b/mycp.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include + +#define BUFSIZE 1024 + +int copy(const char *file1, const char *file2){ + /*copy contents of file1 to file2, ignore the hole*/ + int fd1, fd2, num, i; + int base, probe; + char buf[BUFSIZE]; + /*open the source and target files*/ + if((fd1 = open(file1, O_RDWR)) == -1){ + printf("Error opening %s for copying\n", file1); + return -1; + } + if((fd2 = open(file2, O_RDWR|O_APPEND|O_CREAT|O_TRUNC)) == -1){ + printf("Error creating %s to copy\n", file2); + return -1; + } + + do{ + memset(buf, '\0', BUFSIZE); + + if((num = read(fd1, buf, BUFSIZE)) == -1){ + printf("Error reading %s for copying\n", file1); + return -1; + } + /*clear all "holes"('\0') in the buf array*/ + base =0; + for(i=0; i0); + + close(fd1); + close(fd2); + + return 0; +} + +int main(int argc, char *argv[]){ + char *file1, *file2; + + if(argc != 3){ + printf("parameter error!\n"); + return -1; + } + + file1 = argv[1]; + file2 = argv[2]; + return copy(file1, file2); +}